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

Reject requets with no tokenized data #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewTokenizer(openKey string) *tokenizer {
proxy := goproxy.NewProxyHttpServer()
tkz := &tokenizer{ProxyHttpServer: proxy, priv: priv, pub: pub}

tkz.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "I'm not that kind of server")
})

Expand Down Expand Up @@ -156,6 +156,13 @@ func (t *tokenizer) HandleRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*ht
processors = append(processors, reqProcessors...)
}

if len(processors) == 0 {
pud.reqLog.Warn("no processors")
return nil, errorResponse(ErrBadRequest)
}

pud.reqLog = pud.reqLog.WithField("processors", len(processors))

for _, processor := range processors {
if err := processor(req); err != nil {
pud.reqLog.WithError(err).Warn("run processor")
Expand Down
25 changes: 11 additions & 14 deletions tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,30 @@ func TestTokenizer(t *testing.T) {
tkzServer := httptest.NewServer(tkz)
defer tkzServer.Close()

client, err := Client(tkzServer.URL)
req, err := http.NewRequest(http.MethodPost, appURL+"/foo", nil)
assert.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, appURL+"/foo", nil)
auth := "trustno1"
token := "supersecret"
secret, err := (&Secret{AuthConfig: NewBearerAuthConfig(auth), ProcessorConfig: &InjectProcessorConfig{Token: token}}).Seal(sealKey)
assert.NoError(t, err)

// TLS error (proxy doesn't trust upstream)
client, err := Client(tkzServer.URL, WithAuth(auth), WithSecret(secret, nil))
assert.NoError(t, err)
resp, err := client.Get(appURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadGateway, resp.StatusCode)

// make proxy trust upstream
UpstreamTrust.AddCert(appServer.Certificate())

// no proxy auth or secrets (good)
assert.Equal(t, &echoResponse{
Headers: http.Header{},
Body: "",
}, doEcho(t, client, req))

// bad proxy auth doesn't matter if no secrets are used
client, err = Client(tkzServer.URL, WithAuth("bogus"))
// error if no secrets
client, err = Client(tkzServer.URL, WithAuth(auth))
assert.NoError(t, err)
resp, err = client.Get(appURL)
assert.NoError(t, err)
assert.Equal(t, &echoResponse{
Headers: http.Header{}, // proxy auth isn't leaked downstream
Body: "",
}, doEcho(t, client, req))
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)

t.Run("inject processor", func(t *testing.T) {
auth := "trustno1"
Expand Down
Loading