Skip to content
Merged
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
10 changes: 7 additions & 3 deletions challenges/jwt-alg-none-bypass/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (
)

func generateToken(vulnerable bool) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodNone, jwt.MapClaims{
claims := jwt.MapClaims{
"sub": "2cb307ba-bb46-4194-854f-4774046d9c9b",
"name": "John Doe",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour).Unix(),
})
return token.SignedString(jwt.UnsafeAllowNoneSignatureType)
}
if vulnerable {
token := jwt.NewWithClaims(jwt.SigningMethodNone, claims)
return token.SignedString(jwt.UnsafeAllowNoneSignatureType)
}
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(serve.Secret))
}

func main() {
Expand Down
5 changes: 4 additions & 1 deletion challenges/jwt-alg-none-bypass/serve/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/golang-jwt/jwt/v5"
)

// Secret is the HMAC secret used when the server runs in its fixed, non-vulnerable mode.
const Secret = "my_secret_key"

func RunServer(port string, vulnerable bool) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -28,7 +31,7 @@ func RunServer(port string, vulnerable bool) {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}

return []byte("my_secret_key"), nil
return []byte(Secret), nil
})

if token != nil && token.Valid {
Expand Down
Loading