-
Notifications
You must be signed in to change notification settings - Fork 106
Bypass JWT signature validation via a build-time flag instead of config #3212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| import "testing" | ||
|
|
||
| // parseSkipJWTValidation backs the ldflags-stamped build var. It is true only for | ||
| // the literal "true" (case-insensitive, surrounding space trimmed) — the empty | ||
| // value every normal build carries, and any other value, keep strict validation. | ||
| func TestParseSkipJWTValidation(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| val string | ||
| want bool | ||
| }{ | ||
| {"empty default is strict", "", false}, | ||
| {"explicit false", "false", false}, | ||
| {"arbitrary value is strict", "1", false}, | ||
| {"true enables bypass", "true", true}, | ||
| {"uppercase TRUE", "TRUE", true}, | ||
| {"padded true", " true ", true}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if got := parseSkipJWTValidation(tc.val); got != tc.want { | ||
| t.Errorf("parseSkipJWTValidation(%q) = %v, want %v", tc.val, got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
rg -n -C 6 'AuthMode|buildAuthenticator\(' platform-apiRepository: wso2/api-platform Length of output: 39694 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '--- skip flag implementation and tests ---'
rg -n -C 10 'SkipJWTValidation|parseSkipJWTValidation|SkipValidation' platform-api/config platform-api/internal platform-api --glob '*.go' --glob '*.yaml' --glob '*.yml' --glob '*.toml' --glob '*.json'
printf '%s\n' '--- server authentication wiring ---'
sed -n '550,590p' platform-api/internal/server/server.go
sed -n '671,735p' platform-api/internal/server/server.go
printf '%s\n' '--- relevant config test file ---'
sed -n '1,90p' platform-api/config/skip_jwt_validation_test.go
fd -i 'skip_jwt_validation' platform-apiRepository: wso2/api-platform Length of output: 49220 🏁 Script executed: #!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
for p in Path("platform-api").rglob("*.go"):
text = p.read_text(errors="replace")
if "SkipValidation" in text or "parseSkipJWTValidation" in text or "SkipJWTValidation" in text:
print(f"\n--- {p} ---")
for i, line in enumerate(text.splitlines(), 1):
if any(x in line for x in ("SkipValidation", "parseSkipJWTValidation", "SkipJWTValidation")):
lo, hi = max(1, i-4), min(len(text.splitlines()), i+8)
lines = text.splitlines()
for n in range(lo, hi+1):
print(f"{n}:{lines[n-1]}")
print()
PYRepository: wso2/api-platform Length of output: 15115 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '--- local JWT validation branch ---'
sed -n '160,245p' platform-api/internal/middleware/auth.go
printf '%s\n' '--- all buildAuthenticator call sites ---'
rg -n -C 8 'buildAuthenticator\(' platform-api --glob '*.go'
printf '%s\n' '--- mode-specific server wiring ---'
sed -n '555,585p' platform-api/internal/server/server.go
printf '%s\n' '--- focused source invariant probe ---'
python3 - <<'PY'
from pathlib import Path
server = Path("platform-api/internal/server/server.go").read_text()
auth = Path("platform-api/internal/middleware/auth.go").read_text()
file_branch = server[server.index('if cfg.Auth.Mode == config.AuthModeFile'):server.index('// Resolve the organization claim')]
assert 'SkipValidation: false' in file_branch
assert 'buildAuthenticator(cfg' in file_branch
# The call is in the else branch, so file mode uses the explicit false local-JWT path.
assert 'if cfg.Auth.Mode == config.AuthModeFile' in file_branch
assert 'else {' in file_branch
bypass = auth[auth.index('if config.SkipValidation'):auth.index('//', auth.index('if config.SkipValidation')) if '//' in auth[auth.index('if config.SkipValidation'):] else len(auth)]
assert 'ParseUnverified' in bypass
assert 'WithoutClaimsValidation' in bypass
print("file-mode wiring uses SkipValidation:false; bypass uses ParseUnverified and WithoutClaimsValidation")
PYRepository: wso2/api-platform Length of output: 8434 Do not merge the unsigned-token bypass without an approved exception
🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/golang-jwt/jwt/v5" | ||
| ) | ||
|
|
||
| // unsignedToken builds an alg:none JWT with the given claims, mirroring the | ||
| // unsigned internal token a trusted mediation layer forwards. | ||
| func unsignedToken(t *testing.T, claims jwt.MapClaims) string { | ||
| t.Helper() | ||
| tok := jwt.NewWithClaims(jwt.SigningMethodNone, claims) | ||
| s, err := tok.SignedString(jwt.UnsafeAllowNoneSignatureType) | ||
| if err != nil { | ||
| t.Fatalf("sign unsigned token: %v", err) | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func serve(mw func(http.Handler) http.Handler, token string, next http.HandlerFunc) *httptest.ResponseRecorder { | ||
| req := httptest.NewRequest(http.MethodGet, "/api/v0.9/environments", nil) | ||
| if token != "" { | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| mw(next).ServeHTTP(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| // With SkipValidation, an unsigned token is accepted and its organization claim | ||
| // is enriched into the request context for GetOrganizationFromRequest. | ||
| func TestLocalJWTAuthMiddleware_SkipValidation_AcceptsUnsignedAndResolvesOrg(t *testing.T) { | ||
| mw := LocalJWTAuthMiddleware(AuthConfig{SkipValidation: true}) | ||
| token := unsignedToken(t, jwt.MapClaims{"organization": "org-uuid-123", "sub": "system"}) | ||
|
dakshina99 marked this conversation as resolved.
|
||
|
|
||
| var gotOrg string | ||
| var called bool | ||
| rec := serve(mw, token, func(w http.ResponseWriter, r *http.Request) { | ||
| called = true | ||
| gotOrg, _ = GetOrganizationFromRequest(r) | ||
| }) | ||
|
|
||
| if !called { | ||
| t.Fatalf("next handler not called; status=%d body=%s", rec.Code, rec.Body.String()) | ||
| } | ||
| if gotOrg != "org-uuid-123" { | ||
| t.Errorf("organization = %q, want org-uuid-123", gotOrg) | ||
| } | ||
| } | ||
|
|
||
| // Even with SkipValidation, a token missing the organization claim is rejected. | ||
| func TestLocalJWTAuthMiddleware_SkipValidation_RejectsMissingOrgClaim(t *testing.T) { | ||
| mw := LocalJWTAuthMiddleware(AuthConfig{SkipValidation: true}) | ||
| token := unsignedToken(t, jwt.MapClaims{"sub": "system"}) | ||
|
|
||
| called := false | ||
| rec := serve(mw, token, func(http.ResponseWriter, *http.Request) { called = true }) | ||
|
|
||
| if called { | ||
| t.Fatal("next handler should not be called when the org claim is absent") | ||
| } | ||
| if rec.Code != http.StatusUnauthorized { | ||
| t.Errorf("status = %d, want 401", rec.Code) | ||
| } | ||
|
dakshina99 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Without SkipValidation, an unsigned (alg:none) token is rejected — signature | ||
| // enforcement stays strict by default. | ||
| func TestLocalJWTAuthMiddleware_StrictByDefault_RejectsUnsigned(t *testing.T) { | ||
| mw := LocalJWTAuthMiddleware(AuthConfig{SkipValidation: false}) | ||
| token := unsignedToken(t, jwt.MapClaims{"organization": "org-uuid-123"}) | ||
|
|
||
| called := false | ||
| rec := serve(mw, token, func(http.ResponseWriter, *http.Request) { called = true }) | ||
|
|
||
| if called { | ||
| t.Fatal("next handler should not be called for an unsigned token under strict validation") | ||
| } | ||
| if rec.Code != http.StatusUnauthorized { | ||
| t.Errorf("status = %d, want 401", rec.Code) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.