|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package cel |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/elastic/elastic-agent-libs/logp/logptest" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCheckRedirectSensitiveHeaders(t *testing.T) { |
| 16 | + log := logptest.NewTestingLogger(t, "") |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + prevURL string |
| 21 | + reqURL string |
| 22 | + sensitiveHeaders []string |
| 23 | + wantAuth bool |
| 24 | + wantProxyAuth bool |
| 25 | + wantCookie bool |
| 26 | + wantCustom bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "same_origin_preserves_all_headers", |
| 30 | + prevURL: "https://api.example.com/v1/data", |
| 31 | + reqURL: "https://api.example.com/v1/other", |
| 32 | + sensitiveHeaders: []string{"Authorization", "Proxy-Authorization", "Cookie"}, |
| 33 | + wantAuth: true, |
| 34 | + wantProxyAuth: true, |
| 35 | + wantCookie: true, |
| 36 | + wantCustom: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "cross-origin_strips_sensitive_headers", |
| 40 | + prevURL: "https://api.example.com/v1/data", |
| 41 | + reqURL: "https://evil.example.net/capture", |
| 42 | + sensitiveHeaders: []string{"Authorization", "Proxy-Authorization", "Cookie"}, |
| 43 | + wantAuth: false, |
| 44 | + wantProxyAuth: false, |
| 45 | + wantCookie: false, |
| 46 | + wantCustom: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "scheme_downgrade_strips_sensitive_headers", |
| 50 | + prevURL: "https://api.example.com/v1/data", |
| 51 | + reqURL: "http://api.example.com/v1/data", |
| 52 | + sensitiveHeaders: []string{"Authorization", "Proxy-Authorization", "Cookie"}, |
| 53 | + wantAuth: false, |
| 54 | + wantProxyAuth: false, |
| 55 | + wantCookie: false, |
| 56 | + wantCustom: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty_sensitive_headers_preserves_all_cross-origin", |
| 60 | + prevURL: "https://api.example.com/v1/data", |
| 61 | + reqURL: "https://other.example.net/resource", |
| 62 | + sensitiveHeaders: []string{}, |
| 63 | + wantAuth: true, |
| 64 | + wantProxyAuth: true, |
| 65 | + wantCookie: true, |
| 66 | + wantCustom: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "scheme_upgrade_same_host_preserves_headers", |
| 70 | + prevURL: "http://api.example.com/v1/data", |
| 71 | + reqURL: "https://api.example.com/v1/secure", |
| 72 | + sensitiveHeaders: []string{"Authorization", "Proxy-Authorization", "Cookie"}, |
| 73 | + wantAuth: true, |
| 74 | + wantProxyAuth: true, |
| 75 | + wantCookie: true, |
| 76 | + wantCustom: true, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, test := range tests { |
| 81 | + t.Run(test.name, func(t *testing.T) { |
| 82 | + cfg := &ResourceConfig{ |
| 83 | + RedirectForwardHeaders: true, |
| 84 | + RedirectSensitiveHeaders: test.sensitiveHeaders, |
| 85 | + RedirectMaxRedirects: 10, |
| 86 | + } |
| 87 | + |
| 88 | + prev := &http.Request{ |
| 89 | + URL: mustParseURL(t, test.prevURL), |
| 90 | + Header: http.Header{ |
| 91 | + "Authorization": {"Bearer secret"}, |
| 92 | + "Proxy-Authorization": {"Basic proxy-creds"}, |
| 93 | + "Cookie": {"session=abc123"}, |
| 94 | + "X-Custom": {"keep-me"}, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + req := &http.Request{ |
| 99 | + URL: mustParseURL(t, test.reqURL), |
| 100 | + Header: http.Header{}, |
| 101 | + } |
| 102 | + |
| 103 | + fn := checkRedirect(cfg, log) |
| 104 | + err := fn(req, []*http.Request{prev}) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("checkRedirect returned error: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + check(t, req.Header, "Authorization", test.wantAuth) |
| 110 | + check(t, req.Header, "Proxy-Authorization", test.wantProxyAuth) |
| 111 | + check(t, req.Header, "Cookie", test.wantCookie) |
| 112 | + check(t, req.Header, "X-Custom", test.wantCustom) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func check(t *testing.T, h http.Header, key string, wantPresent bool) { |
| 118 | + t.Helper() |
| 119 | + _, got := h[key] |
| 120 | + if got != wantPresent { |
| 121 | + if wantPresent { |
| 122 | + t.Errorf("expected header %s to be present, but it was stripped", key) |
| 123 | + } else { |
| 124 | + t.Errorf("expected header %s to be stripped, but it is present with value %q", key, h.Get(key)) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func mustParseURL(t *testing.T, raw string) *url.URL { |
| 130 | + t.Helper() |
| 131 | + u, err := url.Parse(raw) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("failed to parse URL %q: %v", raw, err) |
| 134 | + } |
| 135 | + return u |
| 136 | +} |
0 commit comments