jakdept / goki

A markdown based wiki
MIT License
2 stars 0 forks source link

Thread crash when loading a restricted page in markdown handler #13

Closed jakdept closed 9 years ago

jakdept commented 9 years ago

When loading a restricted page in the markdown handler, I'm getting a stack dump that looks like:

Jul 28 04:10:04 wiki webserver: 2015/07/28 04:10:04 request [ /jak_dump ] was against a page [ jak_dump.md ] with a restricted tag
Jul 28 04:10:04 wiki webserver: 2015/07/28 04:10:04 http: panic serving 107.205.249.7:50126: runtime error: invalid memory address or nil pointer dereference
Jul 28 04:10:04 wiki webserver: goroutine 60 [running]:
Jul 28 04:10:04 wiki webserver: net/http.func·011()
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1130 +0xbb
Jul 28 04:10:04 wiki webserver: github.com/JackKnifed/gnosis.MarkdownHandler(0x7f8c1642e2e0, 0xc208031400, 0xc2080f8680, 0xc208029220, 0xf, 0xc20802922f, 0x1, 0xc208029230, 0x6, 0xc208029240, ...)
Jul 28 04:10:04 wiki webserver: /home/jack/working/golang/src/github.com/JackKnifed/gnosis/handlers.go:53 +0x85e
Jul 28 04:10:04 wiki webserver: github.com/JackKnifed/gnosis.func·001(0x7f8c1642e2e0, 0xc208031400, 0xc2080f8680)
Jul 28 04:10:04 wiki webserver: /home/jack/working/golang/src/github.com/JackKnifed/gnosis/handlers.go:166 +0xce
Jul 28 04:10:04 wiki webserver: net/http.HandlerFunc.ServeHTTP(0xc208029460, 0x7f8c1642e2e0, 0xc208031400, 0xc2080f8680)
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1265 +0x41
Jul 28 04:10:04 wiki webserver: net/http.(*ServeMux).ServeHTTP(0xc20803ad50, 0x7f8c1642e2e0, 0xc208031400, 0xc2080f8680)
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1541 +0x17d
Jul 28 04:10:04 wiki webserver: net/http.serverHandler.ServeHTTP(0xc208068240, 0x7f8c1642e2e0, 0xc208031400, 0xc2080f8680)
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1703 +0x19a
Jul 28 04:10:04 wiki webserver: net/http.(*conn).serve(0xc208031360)
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1204 +0xb57
Jul 28 04:10:04 wiki webserver: created by net/http.(*Server).Serve
Jul 28 04:10:04 wiki webserver: /usr/local/go/src/net/http/server.go:1751 +0x35e

looking into it.

jakdept commented 9 years ago

I confirmed this is begin kicked by line 52 of handlers.go:

func MarkdownHandler(responsePipe http.ResponseWriter, request *http.Request, serverConfig ServerSection) {

    var err error

    requestPath := strings.TrimPrefix(request.URL.Path, serverConfig.Prefix)

    // If the request is empty, set it to the default.
    if request.URL.Path == "" || request.URL.Path == "/" {
        request.URL.Path = serverConfig.Default
    }

    // If the request doesn't end in .md, add that
    if !strings.HasSuffix(requestPath, ".md") {
        requestPath = requestPath + ".md"
    }

    pdata := new(PageMetadata)
    err = pdata.LoadPage(serverConfig.Path + requestPath)
    if err != nil {
        log.Printf("request [ %s ] points to an bad file target [ %s ] sent to server %s", request.URL.Path, requestPath, serverConfig.Prefix)
        http.Error(responsePipe, err.Error(), http.StatusNotFound)
        return
    }

    if pdata.MatchedTag(serverConfig.Restricted) {
        log.Printf("request [ %s ] was against a page [ %s ] with a restricted tag", request.URL.Path, requestPath)
        http.Error(responsePipe, err.Error(), http.StatusNotFound)
        //http.Error(responsePipe, err.Error(), http.StatusForbidden)
        return
    }

    // parse any markdown in the input
    body := template.HTML(bodyParseMarkdown(pdata.Page))
    toc := template.HTML(tocParseMarkdown(pdata.Page))
    keywords := pdata.PrintKeywords()
    topics := pdata.PrintTopics(serverConfig.TopicURL)

    // ##TODO## put this template right in the function call
    // Then remove the Page Struct above
    response := Page{Title: "", ToC: toc, Body: body, Keywords: keywords, Topics: topics}
    err = allTemplates.ExecuteTemplate(responsePipe, serverConfig.Template, response)
    if err != nil {
        http.Error(responsePipe, err.Error(), 500)
    }
}

The specific line ikicking the error is:

        http.Error(responsePipe, err.Error(), http.StatusNotFound)

The third argument to this function call can be replaced without causing the error. You can request a page that is not present without issue (above). You can comment out this line, and it works fine. I copied the line from the 404 check, and compiled with that - and it's the same error.

jakdept commented 9 years ago

Turns out that err is nil when you get a page that exists, but has tags. Thus, you cannot call err.Error(). Herpity derpity.

Fixed.