Skip to content

Commit

Permalink
@request-target should include params, fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronf committed Apr 2, 2022
1 parent 1671f83 commit e3113e1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 3 additions & 2 deletions http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ var wantFields = `"kuku": my awesome header
"@query": ?k1=v1&k2
"@method": GET
"@target-uri": {{.Scheme}}://127.0.0.1:{{.Port}}/path?k1=v1&k2
"@request-target": /path?k1=v1&k2
"@authority": 127.0.0.1:{{.Port}}
"@scheme": {{.Scheme}}
"@target-uri": {{.Scheme}}://127.0.0.1:{{.Port}}/path?k1=v1&k2
"@path": /path
"@query": ?k1=v1&k2
"@query-param";name="k1": v1
"@query-param";name="k2":
"@signature-params": ("kuku" "@query" "@method" "@target-uri" "@authority" "@scheme" "@target-uri" "@path" "@query" "@query-param";name="k1" "@query-param";name="k2");alg="hmac-sha256";keyid="key1"`
"@signature-params": ("kuku" "@query" "@method" "@target-uri" "@request-target" "@authority" "@scheme" "@target-uri" "@path" "@query" "@query-param";name="k1" "@query-param";name="k2");alg="hmac-sha256";keyid="key1"`

func execTemplate(t template.Template, name string, data interface{}) (string, error) {
buf := &bytes.Buffer{}
Expand Down Expand Up @@ -116,7 +117,7 @@ func simpleClient(t *testing.T, proto string, simpleHandler func(w http.Response

signer, err := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{0x03}, 64),
NewSignConfig().SignCreated(false),
*NewFields().AddHeaders("kuku", "@query", "@method", "@target-uri", "@authority", "@scheme", "@target-uri",
*NewFields().AddHeaders("kuku", "@query", "@method", "@target-uri", "@request-target", "@authority", "@scheme", "@target-uri",
"@path", "@query").AddQueryParam("k1").AddQueryParam("k2"))
if err != nil {
t.Errorf("failed to create signer")
Expand Down
6 changes: 5 additions & 1 deletion httpparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func scQuery(url *url.URL) string {
}

func scRequestTarget(url *url.URL) string {
return url.Path
if url.RawQuery == "" {
return url.Path
} else {
return url.Path + "?" + url.RawQuery
}
}

func scScheme(url *url.URL) string {
Expand Down
32 changes: 32 additions & 0 deletions signatures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,14 @@ Example-Dict: a=1, b=2;x=1;y=2, c=(a b c)
`

var httpreq5 = `GET /foo HTTP/1.1
Host: www.example.com
Date: Tue, 20 Apr 2021 02:07:56 GMT
Cache-Control: max-age=60
Cache-Control: must-revalidate
`

func Test_signRequestDebug(t *testing.T) {
type args struct {
signatureName string
Expand Down Expand Up @@ -1443,6 +1451,30 @@ func Test_signRequestDebug(t *testing.T) {
wantSignatureBase: "\"cache-control\": max-age=60, must-revalidate\n\"example-dict\";key=\"a\": 1\n\"example-dict\";key=\"b\": 2;x=1;y=2\n\"example-dict\";key=\"c\": (a b c)\n\"@signature-params\": (\"cache-control\" \"example-dict\";key=\"a\" \"example-dict\";key=\"b\" \"example-dict\";key=\"c\");alg=\"hmac-sha256\";keyid=\"test-key-hmac\"",
wantErr: false,
},
{
name: "issue #1, @request-target",
args: args{
signatureName: "sig1",
signer: makeHMACSigner(*NewSignConfig().SignCreated(false), Headers("@request-target")),
req: readRequest(httpreq2),
},
wantSignatureInput: "sig1=(\"@request-target\");alg=\"hmac-sha256\";keyid=\"test-key-hmac\"",
wantSignature: "sig1=:z8fRhDS1tbmNinLWIUKLGgUT7e/Kk4lda3zwGVxJJGA=:",
wantSignatureBase: "\"@request-target\": /foo?param=value&pet=dog&pet=snake&bar=baz\n\"@signature-params\": (\"@request-target\");alg=\"hmac-sha256\";keyid=\"test-key-hmac\"",
wantErr: false,
},
{
name: "issue #1, @request-target, no path params",
args: args{
signatureName: "sig1",
signer: makeHMACSigner(*NewSignConfig().SignCreated(false), Headers("@request-target")),
req: readRequest(httpreq5),
},
wantSignatureInput: "sig1=(\"@request-target\");alg=\"hmac-sha256\";keyid=\"test-key-hmac\"",
wantSignature: "sig1=:QH4dlxNv1P4mbPlWE3PwOc3sp1oeC2rE/OESjve4JJQ=:",
wantSignatureBase: "\"@request-target\": /foo\n\"@signature-params\": (\"@request-target\");alg=\"hmac-sha256\";keyid=\"test-key-hmac\"",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit e3113e1

Please sign in to comment.