-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [HIGH] Fix Information Leakage in API Errors #159
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
Open
AGI-Corporation
wants to merge
1
commit into
main
Choose a base branch
from
sentinel/fix-error-leakage-11848863788720786274
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
β35
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
| import pytest | ||
| from httpx import ASGITransport, AsyncClient | ||
| from unittest.mock import patch | ||
| from backend.main import app | ||
| from backend.db.database import Base, engine, init_db | ||
|
|
||
| @pytest.fixture(scope="module", autouse=True) | ||
| async def setup_db(): | ||
| async with engine.begin() as conn: | ||
| await conn.run_sync(Base.metadata.drop_all) | ||
| await init_db() | ||
| yield | ||
|
|
||
| @pytest.mark.anyio | ||
| async def test_error_leakage_mistral_gap_analysis(): | ||
| """ | ||
| Verify that the Mistral gap-analysis endpoint does not leak exception details. | ||
| """ | ||
| # Request data | ||
| payload = { | ||
| "control_id": "AC.1.001", | ||
| "control_title": "Limit system access to authorized users", | ||
| "control_description": "Limit information system access to authorized users...", | ||
| "zt_pillar": "User", | ||
| "current_status": "not_implemented", | ||
| "existing_evidence": [] | ||
| } | ||
|
|
||
| # Mock the agent's analyze_gap method to raise a sensitive exception | ||
| with patch("agents.mistral_agent.agent.agent.analyze_gap") as mock_analyze: | ||
| mock_analyze.side_effect = Exception("Sensitive database connection string: postgresql://admin:secret_password@db.internal:5432/cmmc") | ||
|
|
||
| async with AsyncClient( | ||
| transport=ASGITransport(app=app, raise_app_exceptions=False), | ||
| base_url="http://test" | ||
| ) as ac: | ||
| response = await ac.post("/api/agents/mistral/gap-analysis", json=payload) | ||
|
|
||
| assert response.status_code == 500 | ||
| # Currently it leaks, so we expect this to FAIL once we start fixing it if we assert it DOES NOT contain it | ||
| # But for now, let's just assert it exists to confirm we have a working test that detects the leak. | ||
| # Actually, Sentinel's job is to fix it, so the test should assert the SAFE state. | ||
|
|
||
| detail = response.json().get("detail", "") | ||
| print(f"\nResponse detail: {detail}") | ||
| assert "secret_password" not in detail | ||
| assert "Sensitive database connection string" not in detail | ||
| assert detail == "Internal server error" | ||
|
|
||
| @pytest.mark.anyio | ||
| async def test_http_exception_not_swallowed(): | ||
| """ | ||
| Verify that standard HTTPExceptions (like 404) are still returned correctly. | ||
| """ | ||
| async with AsyncClient( | ||
| transport=ASGITransport(app=app, raise_app_exceptions=False), | ||
| base_url="http://test" | ||
| ) as ac: | ||
| # Non-existent evidence ID | ||
| response = await ac.get("/api/evidence/non-existent-id-123") | ||
|
|
||
| assert response.status_code == 404 | ||
| assert response.json()["detail"] == "Evidence non-existent-id-123 not found" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new global handler rewraps
StarletteHTTPExceptioninto a freshJSONResponsebut dropsexc.headers, which changes protocol behavior for framework-generated/client-critical errors. Inbackend/main.py, this means responses like405 Method Not Allowedlose theirAllowheader (and any future401auth challenges would loseWWW-Authenticate), so clients can no longer rely on standard HTTP metadata even though the status code/detail are preserved.Useful? React with πΒ / π.