Skip to content

Commit 6e6d43d

Browse files
eschuthoclaude
andauthored
fix(security): return 400 instead of 500 for guest_token requests with wrong Content-Type (#46529)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent e81334c commit 6e6d43d

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎superset/security/api.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,15 @@ def guest_token(self) -> Response:
239239
$ref: '#/components/responses/500'
240240
"""
241241
try:
242-
body = guest_token_create_schema.load(request.json)
242+
request_json = request.get_json(silent=True)
243+
if request_json is None:
244+
return self.response_400(
245+
message=(
246+
"Request is not JSON. Ensure the Content-Type header "
247+
"is set to 'application/json'."
248+
)
249+
)
250+
body = guest_token_create_schema.load(request_json)
243251
self.appbuilder.sm.validate_guest_token_resources(
244252
body["resources"], datasets=body.get("datasets")
245253
)

‎tests/integration_tests/security/api_tests.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ def test_post_guest_token_unauthorized(self):
175175
response = self.client.post(self.uri)
176176
self.assert403(response)
177177

178+
def test_post_guest_token_wrong_content_type(self):
179+
"""
180+
Security API: Posting a guest token request with a non-JSON
181+
Content-Type (or no body) should return a clean 400, not an
182+
unhandled 500.
183+
"""
184+
self.login(ADMIN_USERNAME)
185+
response = self.client.post(
186+
self.uri, data="not-json", content_type="text/plain"
187+
)
188+
self.assert400(response)
189+
178190
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
179191
def test_post_guest_token_authorized(self):
180192
self.dash = db.session.query(Dashboard).filter_by(slug="births").first()

0 commit comments

Comments
 (0)