Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get host from req.header #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

liaojianqi
Copy link

No description provided.

@liaojianqi liaojianqi force-pushed the bugfix/bypass-when-same-origin branch from 0a93a5e to 3093ab4 Compare August 28, 2018 03:15
@appleboy
Copy link
Member

ref: https://golang.org/pkg/net/http/#Request

        // Header contains the request header fields either received
        // by the server or to be sent by the client.
        //
        // If a server received a request with header lines,
        //
        //	Host: example.com
        //	accept-encoding: gzip, deflate
        //	Accept-Language: en-us
        //	fOO: Bar
        //	foo: two
        //
        // then
        //
        //	Header = map[string][]string{
        //		"Accept-Encoding": {"gzip, deflate"},
        //		"Accept-Language": {"en-us"},
        //		"Foo": {"Bar", "two"},
        //	}
        //
        // For incoming requests, the Host header is promoted to the
        // Request.Host field and removed from the Header map.
        //
        // HTTP defines that header names are case-insensitive. The
        // request parser implements this by using CanonicalHeaderKey,
        // making the first character and any characters following a
        // hyphen uppercase and the rest lowercase.
        //
        // For client requests, certain headers such as Content-Length
        // and Connection are automatically written when needed and
        // values in Header may be ignored. See the documentation
        // for the Request.Write method.
        Header Header

@appleboy
Copy link
Member

appleboy commented Aug 28, 2018

// Constants for readRequest's deleteHostHeader parameter.
const (
	deleteHostHeader = true
	keepHostHeader   = false
)

and in readRequest func

	// RFC 7230, section 5.3: Must treat
	//	GET /index.html HTTP/1.1
	//	Host: www.google.com
	// and
	//	GET http://www.google.com/index.html HTTP/1.1
	//	Host: doesntmatter
	// the same. In the second case, any Host line is ignored.
	req.Host = req.URL.Host
	if req.Host == "" {
		req.Host = req.Header.get("Host")
	}
	if deleteHostHeader {
		delete(req.Header, "Host")
	}

@thinkerou
Copy link
Member

ref: request.go

 218         // For server requests Host specifies the host on which the URL
 219         // is sought. Per RFC 7230, section 5.4, this is either the value
 220         // of the "Host" header or the host name given in the URL itself.
 221         // It may be of the form "host:port". For international domain
 222         // names, Host may be in Punycode or Unicode form. Use
 223         // golang.org/x/net/idna to convert it to either format if
 224         // needed.
 225         // To prevent DNS rebinding attacks, server Handlers should
 226         // validate that the Host header has a value for which the
 227         // Handler considers itself authoritative. The included
 228         // ServeMux supports patterns registered to particular host
 229         // names and thus protects its registered Handlers.
 230         //
 231         // For client requests Host optionally overrides the Host
 232         // header to send. If empty, the Request.Write method uses
 233         // the value of URL.Host. Host may contain an international
 234         // domain name.
 235         Host string

I think you should use Host of Header

@liaojianqi
Copy link
Author

ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin
Origin: <scheme> "://" <hostname> [ ":" <port> ]

// Per RFC 7230, section 5.4, this is either the value
// of the "Host" header or the host name given in the URL itself.
// It may be of the form "host:port".

Our target is to determine whether the request is same origin. So I think just determine whether the
host is same as origin(remove scheme+"://").
Maybe we should use req.Host?
BTW, how to use Host of Header in go? it has been removed@thinkerou

@appleboy
Copy link
Member

@liaojianqi please fix the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants