-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Remove default port from host when calculating signature #1618
Changes from 8 commits
3546215
e8ed4e4
3fb7ab9
78d18fd
d7f78ff
b67f4f0
7de5504
a8b4020
832938e
de117e8
30c89b7
a903462
1b6f008
a2e4df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,6 +504,8 @@ func (ctx *signingCtx) build(disableHeaderHoisting bool) { | |
|
||
ctx.buildBodyDigest() | ||
|
||
ctx.sanitizeHost() | ||
|
||
unsignedHeaders := ctx.Request.Header | ||
if ctx.isPresign { | ||
if !disableHeaderHoisting { | ||
|
@@ -696,6 +698,81 @@ func (ctx *signingCtx) removePresign() { | |
ctx.Query.Del("X-Amz-SignedHeaders") | ||
} | ||
|
||
// Set ctx.Request.Host to host:port for non-standard ports, and to host for explicit standart ports | ||
func (ctx *signingCtx) sanitizeHost() { | ||
if ctx.Request == nil || ctx.Request.URL == nil { | ||
return | ||
} | ||
|
||
var port string | ||
if ctx.Request.Host != "" { | ||
port = portOnly(ctx.Request.Host) | ||
} else { | ||
port = portOnly(ctx.Request.URL.Host) | ||
} | ||
|
||
if isUsingNonDefaultPort(ctx.Request.URL.Scheme, port) { | ||
ctx.Request.Host = hostname(ctx.Request) + ":" + port | ||
} else if port != "" { // remove standard port, if set explicitly | ||
ctx.Request.Host = hostname(ctx.Request) | ||
} | ||
} | ||
|
||
// Returns host without port number | ||
func hostname(req *http.Request) string { | ||
if req == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as above in |
||
return "" | ||
} | ||
|
||
if req.Host != "" { | ||
return stripPort(req.Host) | ||
} | ||
|
||
return aws.URLHostname(req.URL) | ||
} | ||
|
||
// Copy of url.stripPort() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you update this to mention directly that this was copied for the Go standard library |
||
func stripPort(hostport string) string { | ||
colon := strings.IndexByte(hostport, ':') | ||
if colon == -1 { | ||
return hostport | ||
} | ||
if i := strings.IndexByte(hostport, ']'); i != -1 { | ||
return strings.TrimPrefix(hostport[:i], "[") | ||
} | ||
return hostport[:colon] | ||
} | ||
|
||
// Copy of url.portOnly() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto as above |
||
func portOnly(hostport string) string { | ||
colon := strings.IndexByte(hostport, ':') | ||
if colon == -1 { | ||
return "" | ||
} | ||
if i := strings.Index(hostport, "]:"); i != -1 { | ||
return hostport[i+len("]:"):] | ||
} | ||
if strings.Contains(hostport, "]") { | ||
return "" | ||
} | ||
return hostport[colon+len(":"):] | ||
} | ||
|
||
// Returns true if the specified URI is using a non-standard port | ||
// (i.e. any port other than 80 for HTTP URIs or any port other than 443 for HTTPS URIs) | ||
func isUsingNonDefaultPort(scheme, port string) bool { | ||
if port == "" { | ||
return false | ||
} | ||
|
||
lowerCaseScheme := strings.ToLower(scheme) | ||
if (lowerCaseScheme == "http" && port == "80") || (lowerCaseScheme == "https" && port == "443") { | ||
return false | ||
} | ||
|
||
return true; | ||
} | ||
|
||
func makeHmac(key []byte, data []byte) []byte { | ||
hash := hmac.New(sha256.New, key) | ||
hash.Write(data) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you run into situations where the
ctx.Request
value was nil? I think this value is required for the signer to function. In that case i think it would be better to fail loudly instead of silently bypass the nil value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasdel, I assume it's always set, but I don't know :) Removing the check.
What do you think about
ctx.Request.URL
? Should be also always set?