-
Notifications
You must be signed in to change notification settings - Fork 0
SEPE-1165: Fix defects inherited from the upstream beta #11
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zaptest/observer" | ||
| ) | ||
|
|
||
| // newAuthTest builds the auth middleware around a handler that records whether | ||
| // it was reached, which is the only thing that matters: a request that gets | ||
| // through has been authorised. | ||
| func newAuthTest(t *testing.T, secret string) (http.Handler, *bool, *observer.ObservedLogs) { | ||
| t.Helper() | ||
| core, logs := observer.New(zap.DebugLevel) | ||
| s := &Server{secret: secret, logger: zap.New(core).Sugar()} | ||
|
|
||
| reached := false | ||
| inner := http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true }) | ||
| return authMiddleware(s)(inner), &reached, logs | ||
| } | ||
|
|
||
| func do(h http.Handler, authHeader string) *httptest.ResponseRecorder { | ||
| req := httptest.NewRequest(http.MethodPost, "/send", nil) | ||
| if authHeader != "" { | ||
| req.Header.Set("Authorization", authHeader) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| h.ServeHTTP(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| func TestAuthAcceptsMatchingToken(t *testing.T) { | ||
| h, reached, _ := newAuthTest(t, "s3cret") | ||
| rec := do(h, "token s3cret") | ||
|
|
||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.True(t, *reached) | ||
| } | ||
|
|
||
| func TestAuthRejects(t *testing.T) { | ||
| cases := map[string]string{ | ||
| "wrong token": "token wrong", | ||
| "missing header": "", | ||
| "no token prefix": "s3cret", | ||
| "empty token": "token ", | ||
| "prefix only": "token", | ||
| "bearer scheme": "Bearer s3cret", | ||
| } | ||
|
|
||
| for name, header := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| h, reached, _ := newAuthTest(t, "s3cret") | ||
| rec := do(h, header) | ||
|
|
||
| assert.Equal(t, http.StatusUnauthorized, rec.Code) | ||
| assert.False(t, *reached, "the handler must not be reached") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestAuthRefusesWhenNoSecretConfigured covers the behaviour change. An empty | ||
| // secret used to allow every request through, which turned a misconfiguration | ||
| // into an unauthenticated enqueue endpoint -- and enqueued events page people. | ||
| func TestAuthRefusesWhenNoSecretConfigured(t *testing.T) { | ||
| h, reached, logs := newAuthTest(t, "") | ||
|
|
||
| for _, header := range []string{"", "token ", "token anything"} { | ||
| rec := do(h, header) | ||
| assert.Equal(t, http.StatusInternalServerError, rec.Code, | ||
| "an unconfigured server must refuse, not serve") | ||
| assert.False(t, *reached) | ||
| } | ||
|
|
||
| // And it must say why, since the symptom is otherwise indistinguishable | ||
| // from a wrong token. | ||
| assert.NotZero(t, logs.FilterMessageSnippet("no secret is configured").Len()) | ||
| } | ||
|
|
||
| // TestAuthDoesNotLogCredentials: the header is a bearer credential and the log | ||
| // is world-readable, so a near-miss written there is a leak in its own right. | ||
| func TestAuthDoesNotLogCredentials(t *testing.T) { | ||
| h, _, logs := newAuthTest(t, "s3cret") | ||
| do(h, "token almost-the-real-secret") | ||
|
|
||
| for _, e := range logs.All() { | ||
| assert.NotContains(t, e.Message, "almost-the-real-secret") | ||
| for _, v := range e.ContextMap() { | ||
| if s, ok := v.(string); ok { | ||
| assert.NotContains(t, s, "almost-the-real-secret", | ||
| "the presented credential must not reach the log") | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.