-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): adding middleware to protect against csrf attacks
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,24 @@ | ||
package middleware | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"net/http" | ||
) | ||
|
||
func CSRFMiddleware(next http.HandlerFunc, isValidToken func(string) bool) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
csrfToken := r.Header.Get("X-CSRF-Token") | ||
if csrfToken == "" || !isValidToken(csrfToken) { | ||
http.Error(w, "Invalid CSRF token", http.StatusForbidden) | ||
return | ||
} | ||
next(w, r) | ||
} | ||
} | ||
|
||
func GenerateCSRFToken() string { | ||
b := make([]byte, 32) | ||
rand.Read(b) | ||
return base64.StdEncoding.EncodeToString(b) | ||
} |
This file contains 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,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCSRFMiddleware(t *testing.T) { | ||
|
||
validToken := GenerateCSRFToken() | ||
isValidToken := func(token string) bool { | ||
return token == validToken | ||
} | ||
|
||
handler := CSRFMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}), isValidToken) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("X-CSRF-Token", validToken) | ||
respRec := httptest.NewRecorder() | ||
handler.ServeHTTP(respRec, req) | ||
|
||
if respRec.Code != http.StatusOK { | ||
t.Errorf("Expected status 200, got %d", respRec.Code) | ||
} | ||
|
||
invalidReq := httptest.NewRequest(http.MethodGet, "/", nil) | ||
invalidReq.Header.Set("X-CSRF-Token", "invalid-token") | ||
invalidRespRec := httptest.NewRecorder() | ||
handler.ServeHTTP(invalidRespRec, invalidReq) | ||
|
||
if invalidRespRec.Code != http.StatusForbidden { | ||
t.Errorf("Expected status 403, got %d", invalidRespRec.Code) | ||
} | ||
} |