-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow/encourage dst/fmt to be specified in secret
Also, allow the secret to allowlist these if they are to be put in request-time params. The dst/fmt will usually be knowable at the time the secret is created and they usually wont need to vary betweeen requests. Allowing them to be specified at request-time gives room for attackers to discover ways to get the target service to reflect back the plaintext secret in a response.
- Loading branch information
Showing
4 changed files
with
273 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,105 @@ | ||
package tokenizer | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestApplyFmt(t *testing.T) { | ||
val, err := applyParamFmt("", true, []byte{1, 2, 3}) | ||
func TestFmtProcessor(t *testing.T) { | ||
p := FmtProcessor{} | ||
|
||
val, err := p.ApplyFmt(map[string]string{}, true, []byte{1, 2, 3}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "Bearer 010203", val) | ||
|
||
val, err = applyParamFmt("", false, "123") | ||
val, err = p.ApplyFmt(map[string]string{}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "Bearer 123", val) | ||
|
||
val, err = applyParamFmt("%x", true, []byte{1, 2, 3}) | ||
val, err = p.ApplyFmt(map[string]string{ParamFmt: "%x"}, true, []byte{1, 2, 3}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "010203", val) | ||
|
||
val, err = applyParamFmt("%X", true, []byte{1, 2, 3}) | ||
val, err = p.ApplyFmt(map[string]string{ParamFmt: "%X"}, true, []byte{1, 2, 3}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "010203", val) | ||
|
||
val, err = applyParamFmt("%s", false, "123") | ||
val, err = p.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123", val) | ||
|
||
_, err = applyParamFmt("%d", false, "123") | ||
_, err = p.ApplyFmt(map[string]string{ParamFmt: "%d"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
_, err = p.ApplyFmt(map[string]string{ParamFmt: "%.3s"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
_, err = applyParamFmt("%.3s", false, "123") | ||
_, err = p.ApplyFmt(map[string]string{ParamFmt: "%s%s"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
_, err = applyParamFmt("%s%s", false, "123") | ||
_, err = p.ApplyFmt(map[string]string{ParamFmt: "asdf%"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
_, err = applyParamFmt("asdf%", false, "123") | ||
_, err = p.ApplyFmt(map[string]string{ParamFmt: "asdf"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
val, err = FmtProcessor{AllowedFmt: []string{"%s"}}.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123", val) | ||
|
||
_, err = FmtProcessor{AllowedFmt: []string{"x %s"}}.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
_, err = applyParamFmt("asdf", false, "123") | ||
val, err = FmtProcessor{Fmt: "%s"}.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123", val) | ||
|
||
_, err = FmtProcessor{Fmt: "x %s"}.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.Error(t, err) | ||
|
||
val, err = FmtProcessor{Fmt: "%s", AllowedFmt: []string{"%s"}}.ApplyFmt(map[string]string{ParamFmt: "%s"}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123", val) | ||
|
||
val, err = FmtProcessor{Fmt: "%s", AllowedFmt: []string{"%s"}}.ApplyFmt(map[string]string{}, false, "123") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123", val) | ||
|
||
_, err = FmtProcessor{Fmt: "%s", AllowedFmt: []string{"x %s"}}.ApplyFmt(map[string]string{}, false, "123") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestDstProcessor(t *testing.T) { | ||
assertResult := func(expected string, dp DstProcessor, params map[string]string) { | ||
t.Helper() | ||
|
||
r := http.Request{Header: make(http.Header)} | ||
err := dp.ApplyDst(params, &r, "123") | ||
if expected == "error" { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
buf := new(bytes.Buffer) | ||
assert.NoError(t, r.Header.Write(buf)) | ||
assert.Equal(t, expected, strings.TrimSpace(buf.String())) | ||
} | ||
} | ||
|
||
assertResult("Authorization: 123", DstProcessor{}, map[string]string{}) | ||
assertResult("Authorization: 123", DstProcessor{}, map[string]string{ParamDst: "Authorization"}) | ||
assertResult("Authorization: 123", DstProcessor{}, map[string]string{ParamDst: "AuThOriZaTiOn"}) | ||
assertResult("Foo: 123", DstProcessor{}, map[string]string{ParamDst: "Foo"}) | ||
assertResult("Foo: 123", DstProcessor{Dst: "Foo", AllowedDst: []string{"Foo"}}, map[string]string{ParamDst: "Foo"}) | ||
assertResult("Foo: 123", DstProcessor{Dst: "Foo", AllowedDst: []string{"fOo"}}, map[string]string{ParamDst: "foO"}) | ||
assertResult("Foo: 123", DstProcessor{AllowedDst: []string{"fOo"}}, map[string]string{ParamDst: "foO"}) | ||
assertResult("Foo: 123", DstProcessor{Dst: "Foo"}, map[string]string{ParamDst: "foO"}) | ||
assertResult("Foo: 123", DstProcessor{Dst: "Foo", AllowedDst: []string{"fOo"}}, map[string]string{}) | ||
assertResult("error", DstProcessor{Dst: "Foo", AllowedDst: []string{"Bar"}}, map[string]string{ParamDst: "Foo"}) | ||
assertResult("error", DstProcessor{Dst: "Foo", AllowedDst: []string{"Bar"}}, map[string]string{}) | ||
assertResult("error", DstProcessor{AllowedDst: []string{"Bar"}}, map[string]string{ParamDst: "Foo"}) | ||
assertResult("error", DstProcessor{Dst: "Bar"}, map[string]string{ParamDst: "Foo"}) | ||
} |
Oops, something went wrong.