Skip to content

Commit d97e15d

Browse files
authored
Merge pull request #22 from hsiaoairplane/fix/restrict-post-method
2 parents 6011f48 + fd0de95 commit d97e15d

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func handleAdmissionReview(w http.ResponseWriter, r *http.Request) {
6262
// Start measuring the request duration
6363
start := time.Now()
6464

65+
// The apiserver always sends admission requests as POST; reject anything else.
66+
if r.Method != http.MethodPost {
67+
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
68+
return
69+
}
70+
6571
var admissionReviewReq admissionv1.AdmissionReview
6672
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)
6773
body, err := io.ReadAll(r.Body)

main_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ func TestHandleAdmissionReview_NilRequest(t *testing.T) {
9292
}
9393
}
9494

95+
func TestHandleAdmissionReview_MethodNotAllowed(t *testing.T) {
96+
// Only POST is accepted; any other method must be rejected.
97+
for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodDelete} {
98+
t.Run(method, func(t *testing.T) {
99+
req := httptest.NewRequest(method, "/validate", nil)
100+
w := httptest.NewRecorder()
101+
102+
handleAdmissionReview(w, req)
103+
104+
resp := w.Result()
105+
defer resp.Body.Close()
106+
107+
if resp.StatusCode != http.StatusMethodNotAllowed {
108+
t.Errorf("Expected status code 405, got %d", resp.StatusCode)
109+
}
110+
})
111+
}
112+
}
113+
95114
func TestHandleAdmissionReview_BodyTooLarge(t *testing.T) {
96115
// A body exceeding maxRequestBodyBytes must be rejected rather than
97116
// read fully into memory.

0 commit comments

Comments
 (0)