From 283e87d8145ac5dd61f86f61e347ffa684ac5684 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 5 Feb 2018 16:56:30 +0200 Subject: [PATCH] Fix rendering of wiki page list if wiki repo contains other files (#3454) * Fix rendering of wiki page list if wiki repo contains other files * Improve wiki filename tests --- models/error.go | 15 +++++++++++++++ models/wiki.go | 2 +- models/wiki_test.go | 5 ++++- routers/repo/wiki.go | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/models/error.go b/models/error.go index cd96fa925..a1c24f4ec 100644 --- a/models/error.go +++ b/models/error.go @@ -216,6 +216,21 @@ func (err ErrWikiReservedName) Error() string { return fmt.Sprintf("wiki title is reserved: %s", err.Title) } +// ErrWikiInvalidFileName represents an invalid wiki file name. +type ErrWikiInvalidFileName struct { + FileName string +} + +// IsErrWikiInvalidFileName checks if an error is an ErrWikiInvalidFileName. +func IsErrWikiInvalidFileName(err error) bool { + _, ok := err.(ErrWikiInvalidFileName) + return ok +} + +func (err ErrWikiInvalidFileName) Error() string { + return fmt.Sprintf("Invalid wiki filename: %s", err.FileName) +} + // __________ ___. .__ .__ ____ __. // \______ \__ _\_ |__ | | |__| ____ | |/ _|____ ___.__. // | ___/ | \ __ \| | | |/ ___\ | <_/ __ < | | diff --git a/models/wiki.go b/models/wiki.go index 3a39daf0f..3e0d1b9ce 100644 --- a/models/wiki.go +++ b/models/wiki.go @@ -45,7 +45,7 @@ func WikiNameToFilename(name string) string { // WikiFilenameToName converts a wiki filename to its corresponding page name. func WikiFilenameToName(filename string) (string, error) { if !strings.HasSuffix(filename, ".md") { - return "", fmt.Errorf("Invalid wiki filename: %s", filename) + return "", ErrWikiInvalidFileName{filename} } basename := filename[:len(filename)-3] unescaped, err := url.QueryUnescape(basename) diff --git a/models/wiki_test.go b/models/wiki_test.go index 80f6df55e..a142a58f3 100644 --- a/models/wiki_test.go +++ b/models/wiki_test.go @@ -77,11 +77,14 @@ func TestWikiFilenameToName(t *testing.T) { for _, badFilename := range []string{ "nofileextension", "wrongfileextension.txt", - "badescaping%%.md", } { _, err := WikiFilenameToName(badFilename) assert.Error(t, err) + assert.True(t, IsErrWikiInvalidFileName(err)) } + _, err := WikiFilenameToName("badescaping%%.md") + assert.Error(t, err) + assert.False(t, IsErrWikiInvalidFileName(err)) } func TestWikiNameToFilenameToName(t *testing.T) { diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index 7befe7c68..87cb9f303 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -128,6 +128,9 @@ func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *gi } wikiName, err := models.WikiFilenameToName(entry.Name()) if err != nil { + if models.IsErrWikiInvalidFileName(err) { + continue + } ctx.ServerError("WikiFilenameToName", err) return nil, nil } else if wikiName == "_Sidebar" || wikiName == "_Footer" { @@ -262,6 +265,9 @@ func WikiPages(ctx *context.Context) { } wikiName, err := models.WikiFilenameToName(entry.Name()) if err != nil { + if models.IsErrWikiInvalidFileName(err) { + continue + } ctx.ServerError("WikiFilenameToName", err) return }