-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_test.go
More file actions
185 lines (150 loc) · 4.87 KB
/
secure_test.go
File metadata and controls
185 lines (150 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// ── WithSecure ──────────────────────────────────────────────────────────
func TestWithSecure_Good_SetsHSTSHeader(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
sts := w.Header().Get("Strict-Transport-Security")
if sts == "" {
t.Fatal("expected Strict-Transport-Security header to be set")
}
if !strings.Contains(sts, "max-age=31536000") {
t.Fatalf("expected max-age=31536000 in STS header, got %q", sts)
}
if !strings.Contains(strings.ToLower(sts), "includesubdomains") {
t.Fatalf("expected includeSubdomains in STS header, got %q", sts)
}
}
func TestWithSecure_Good_SetsFrameOptionsDeny(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
xfo := w.Header().Get("X-Frame-Options")
if xfo != "DENY" {
t.Fatalf("expected X-Frame-Options=%q, got %q", "DENY", xfo)
}
}
func TestWithSecure_Good_SetsContentTypeNosniff(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
cto := w.Header().Get("X-Content-Type-Options")
if cto != "nosniff" {
t.Fatalf("expected X-Content-Type-Options=%q, got %q", "nosniff", cto)
}
}
func TestWithSecure_Good_SetsReferrerPolicy(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
rp := w.Header().Get("Referrer-Policy")
if rp != "strict-origin-when-cross-origin" {
t.Fatalf("expected Referrer-Policy=%q, got %q", "strict-origin-when-cross-origin", rp)
}
}
func TestWithSecure_Good_AllHeadersPresent(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Verify all security headers are present on a regular route.
checks := map[string]string{
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "strict-origin-when-cross-origin",
}
for header, want := range checks {
got := w.Header().Get(header)
if got != want {
t.Errorf("header %s: expected %q, got %q", header, want, got)
}
}
sts := w.Header().Get("Strict-Transport-Security")
if sts == "" {
t.Error("expected Strict-Transport-Security header to be set")
}
}
func TestWithSecure_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(
api.WithSecure(),
api.WithRequestID(),
)
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Both secure headers and request ID should be present.
if w.Header().Get("X-Frame-Options") != "DENY" {
t.Fatal("expected X-Frame-Options header from WithSecure")
}
if w.Header().Get("X-Request-ID") == "" {
t.Fatal("expected X-Request-ID header from WithRequestID")
}
}
func TestWithSecure_Bad_NoSSLRedirect(t *testing.T) {
// SSL redirect is not enabled — the middleware runs behind a TLS-terminating
// reverse proxy. Verify plain HTTP requests are not redirected.
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithSecure())
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
// Should get 200, not a 301/302 redirect.
if w.Code != http.StatusOK {
t.Fatalf("expected 200 (no SSL redirect), got %d", w.Code)
}
}
func TestWithSecure_Ugly_DoubleSecureDoesNotPanic(t *testing.T) {
// Applying WithSecure twice should not panic or cause issues.
gin.SetMode(gin.TestMode)
e, _ := api.New(
api.WithSecure(),
api.WithSecure(),
)
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Headers should still be correctly set.
if w.Header().Get("X-Frame-Options") != "DENY" {
t.Fatal("expected X-Frame-Options=DENY after double WithSecure")
}
}