Skip to content

net/http: DefaultServeMux incorrectly redirect (301) to path if path includes %2F%2F #21955

Description

@kalbasit

What version of Go are you using (go version)?

go version go1.9 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/kalbasit/code/personal/base"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build643957992=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

import "net/http"

func main() {
	http.ListenAndServe(":8878", nil)
}
curl 'http://localhost:8878/path:a%2F%2Fgoogle.com%2F'

What did you expect to see?

404 page not found

What did you see instead?

<a href="/path:a/google.com/">Moved Permanently</a>.

NOTE: this does not happen if alice was used instead of the http.DefaultServeMux

Activity

  1. changed the title [-]net/http incorrectly redirect (301) to path if path includes `%2F%2F`[/-] [+]net/http.DefaultServeMux incorrectly redirect (301) to path if path includes `%2F%2F`[/+] on Sep 21, 2017
  2. ericlagergren commented on Sep 21, 2017

    @ericlagergren
    Contributor

    FWIW it happens even with

    • http://localhost:8878//
    • http://localhost:8878/fdsfklasdjfldsajfldksajf/sdfssdf//sdfdsaf

    The key aspect being two consecutive /s. It's redirected to a path without two /s.

  3. crvv commented on Sep 22, 2017

    @crvv
    Contributor

    Redirecting http://localhost:8878// to http://localhost:8878/ is correct.
    But redirecting http://localhost:8878/%2F to http://localhost:8878/ is not correct.
    I think this is a bug to be fixed.

  4. changed the title [-]net/http.DefaultServeMux incorrectly redirect (301) to path if path includes `%2F%2F`[/-] [+]net/http: DefaultServeMux incorrectly redirect (301) to path if path includes `%2F%2F`[/+] on Mar 30, 2018
  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Mar 30, 2018
  6. added this to the Unplanned milestone on Mar 30, 2018
  7. ianlancetaylor commented on Mar 30, 2018

    @ianlancetaylor
    Contributor
  8. namusyaka commented on Mar 30, 2018

    @namusyaka
    Member

    I fixed similar bug in the past, so I will confirm this.

  9. self-assigned this
    on Mar 30, 2018
  10. gopherbot commented on Mar 30, 2018

    @gopherbot
    Contributor

    Change https://golang.org/cl/103696 mentions this issue: net/http: avoid incorrect redirection on paths containing encoded slashes

  11. tombergan commented on Apr 5, 2018

    @tombergan
    Contributor

    I'm not sure if this actually a bug in the first place. The URL reported in the original comment is:

    http://localhost:8878/path:a%2F%2Fgoogle.com%2F
    

    The decoded path is:

    /path:a//google.com/
    

    ServerMux.Handler uses path.Clean. That path clearly has a double slash and path.Clean translates double slashes to single slashes. Also see here. The godoc for ServerMux.Handler says:

    // If the path is not in its canonical form, the
    // handler will be an internally-generated handler that redirects
    // to the canonical path.
    

    If there's an open question here, the question is around the definition of "canonical path". Does the canonical path convert double slashes to single slashes? If so, does it also convert %2F%2F to "/"? If yes to both questions, there's no bug. Otherwise, there's a bug. AFAICT, these questions are not answered in any documentation. In net/http/server_tests.go, serverMuxTests does not have a case for double slashes, so I suspect this question hasn't come up until now.

  12. namusyaka commented on Apr 23, 2018

    @namusyaka
    Member

    @tombergan Thank you for the clear explanation. I'll investigate this problem again.

  13. namusyaka commented on Apr 28, 2018

    @namusyaka
    Member

    @tombergan @bradfitz

    As a countermeasure against this similar problem, there is nginx merge_slashes syntax.
    Even with nginx, the above options are used for cases where you do not want to integrate encoded duplicate slashes into one.

    And then, I have take a look at RFC-3986 for confirming the encoded double slashes validity.
    According to it, the encoded string pchar is considered valid.

       path          = path-abempty    ; begins with "/" or is empty
                     / path-absolute   ; begins with "/" but not "//"
                     / path-noscheme   ; begins with a non-colon segment
                     / path-rootless   ; begins with a segment
                     / path-empty      ; zero characters
    
       path-abempty  = *( "/" segment )
       path-absolute = "/" [ segment-nz *( "/" segment ) ]
       path-noscheme = segment-nz-nc *( "/" segment )
       path-rootless = segment-nz *( "/" segment )
       path-empty    = 0<pchar>
    
       segment       = *pchar
       segment-nz    = 1*pchar
       segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
                     ; non-zero-length segment without any colon ":"
    
       pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
    
       query         = *( pchar / "/" / "?" )
    
       fragment      = *( pchar / "/" / "?" )
    
       pct-encoded   = "%" HEXDIG HEXDIG
    
    

    Therefore, I think, if we follow RFC-3986, the current behavior is buggy.

  14. namusyaka commented on May 4, 2018

    @namusyaka
    Member

    @bradfitz ping? If my survey is reasonable, I'm going to try to improve the patch based on this survey result.
    Do you have any opinion?

  15. namusyaka commented on Jun 24, 2018

    @namusyaka
    Member
  16. added a commit that references this issue on May 20, 2020
    a7b1801
  17. gopherbot commented on May 20, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/234657 mentions this issue: net/http: prevent incorrect redirections when the path contains %2F%2F

  18. neild commented on Oct 19, 2020

    @neild
    Contributor

    To expand on @tombergan's comment (#21955 (comment)), the ServeMux documentation states:

    ServeMux also takes care of sanitizing the URL request path and the Host header, stripping the port number and redirecting any request containing . or .. elements or repeated slashes to an equivalent, cleaner URL.

    The documentation is not explicit on whether %2f is considered a slash, but the implementation consistently treats it as such. ServeMux.Handle("/foo/", f) matches the request /foo%2fbar.

    I believe the current behavior is consistent with the documentation and probably can't be changed without violating compatibility. Perhaps the documentation should explicitly mention the treatment of escaped path components.

  19. jfcalvo commented on Apr 6, 2021

    @jfcalvo

    I'm suffering the same problem trying to use an encoded full URL in the path.

    For example I'm trying to use this URL (an image from Wikimedia):

    https://upload.wikimedia.org/wikipedia/commons/4/49/Mosuo_Woman_-_42723465920.jpg
    

    Once encoded we get the following URL:

    https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F4%2F49%2FMosuo_Woman_-_42723465920.jpg
    

    And this is the request to my server using the previous URL as path:

    http://localhost:8080/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F4%2F49%2FMosuo_Woman_-_42723465920.jpg
    

    I'm receiving a 301 status code with a Moved Permanently body message linking to:

    http://localhost:8080/https:/upload.wikimedia.org/wikipedia/commons/4/49/Mosuo_Woman_-_42723465920.jpg
    

    I understand that ServeMux is cleaning paths and redirecting in the case of differences but what I don't understand it's why that is done for escaped characters like %2F%2F.

    Apart from the possible bug, anybody knows any solution/workaround that doesn't involve creating from scratch a custom serve mux or using another one available? Any possible way to avoid that cleaning?

  20. crvv commented on Apr 7, 2021

    @crvv
    Contributor

    This is clearly a bug.

    The encoding method can encode arbitrary data in a Uniform Resource Identifier (URI), which is the point of the encoding.

    "/" is the reserved character slash. "%2f" is an arbitrary string.
    "/" and "%2f" are two different things.

    Is "%2f" is a slash?
    No, of course it is not a slash. and "%25" is not a percent sign
    Otherwise "https://www.google.com/" will be equivalent to "https:%252f%252525252fwww.google.com%2f".

  21. kayrus commented on Jun 15, 2022

    @kayrus

    Just faced this issue in the latest go 1.18.3

    curl http://localhost/%2Fhello is decoded internally to //hello (r.URL.Path) and the decision for a 301 redirect based on r.URL.Path instead of r.URL.RawPath.

    A dirty hack that fixed useless 301 redirect issue for me:

    --- go/src/net/http/server.go   2022-06-01 18:44:51.000000000 +0200
    +++ go_/src/net/http/server.go  2022-06-15 23:44:02.602151436 +0200
    @@ -2412,7 +2412,7 @@
            // All other requests have any port stripped and path cleaned
            // before passing to mux.handler.
            host := stripHostPort(r.Host)
    -       path := cleanPath(r.URL.Path)
    +       path := cleanPath(r.URL.RawPath)
     
            // If the given path is /tree and its handler is not registered,
            // redirect for /tree/.
    @@ -2420,7 +2420,7 @@
                    return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
            }
     
    -       if path != r.URL.Path {
    +       if path != r.URL.RawPath {
                    _, pattern = mux.handler(host, path)
                    u := &url.URL{Path: path, RawQuery: r.URL.RawQuery}
                    return RedirectHandler(u.String(), StatusMovedPermanently), pattern
  22. gopherbot commented on Sep 13, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/528356 mentions this issue: net/http: ServeMux matches escaped paths

  23. gopherbot commented on Sep 22, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/530575 mentions this issue: net/http: unescape paths and patterns by segment

  24. locked and limited conversation to collaborators on Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions