Repository navigation
security: harden CORS, rate limits, /health, and SQLite compat (1.0.0a5) #27
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
Merged
Merged
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
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,82 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from sqlalchemy import create_engine | ||
|
|
||
| from app.core.config import Settings, settings | ||
| from app.db.session import ( | ||
| SQLITE_COMPAT_COLUMNS, | ||
| _validate_sqlite_identifier, | ||
| ensure_sqlite_schema_compatibility, | ||
| ) | ||
| from app.main import CORS_ALLOWED_HEADERS, CORS_ALLOWED_METHODS, create_app | ||
|
|
||
|
|
||
| def test_cors_uses_explicit_methods_and_headers(client) -> None: | ||
| assert "*" not in CORS_ALLOWED_METHODS | ||
| assert "*" not in CORS_ALLOWED_HEADERS | ||
| response = client.options( | ||
| "/api/readiness", | ||
| headers={ | ||
| "Origin": "http://localhost:3000", | ||
| "Access-Control-Request-Method": "POST", | ||
| "Access-Control-Request-Headers": "X-Operator-Scan-Token, Content-Type", | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.headers["access-control-allow-origin"] == "http://localhost:3000" | ||
| assert "POST" in response.headers["access-control-allow-methods"] | ||
|
|
||
|
|
||
| def test_health_redacts_internal_runtime_outside_local(client, monkeypatch) -> None: | ||
| monkeypatch.setattr(settings, "environment", "production") | ||
| monkeypatch.setattr(settings, "operator_scan_token", "prod-health-secret") | ||
|
|
||
| unauthenticated = client.get("/health") | ||
| assert unauthenticated.status_code == 200 | ||
| assert unauthenticated.json() == {"status": "ok"} | ||
|
|
||
| authenticated = client.get("/health", headers={"X-Operator-Scan-Token": "prod-health-secret"}) | ||
| assert authenticated.status_code == 200 | ||
| body = authenticated.json() | ||
| assert body["status"] == "ok" | ||
| assert "llm_provider" in body | ||
| assert "embedding_model" in body | ||
|
|
||
|
|
||
| def test_high_load_endpoints_enforce_rate_limit(monkeypatch) -> None: | ||
| monkeypatch.setattr(settings, "rate_limit_requests_per_minute", 2) | ||
| app = create_app() | ||
| with TestClient(app) as limited_client: | ||
| first = limited_client.post("/api/search/semantic", json={"query": "rate limit test"}) | ||
| second = limited_client.post("/api/search/semantic", json={"query": "rate limit test"}) | ||
| third = limited_client.post("/api/search/semantic", json={"query": "rate limit test"}) | ||
|
|
||
| assert first.status_code == 200 | ||
| assert second.status_code == 200 | ||
| assert third.status_code == 429 | ||
| assert int(third.headers["Retry-After"]) >= 1 | ||
|
|
||
|
|
||
| def test_settings_default_rate_limit_enables_outside_local() -> None: | ||
| local_cfg = Settings(environment="local", author_hash_salt="change-me") | ||
| prod_cfg = Settings(environment="production", author_hash_salt="unique-prod-salt-123") | ||
|
|
||
| assert local_cfg.effective_rate_limit_per_minute == 0 | ||
| assert prod_cfg.effective_rate_limit_per_minute == 60 | ||
|
|
||
|
|
||
| def test_sqlite_compat_rejects_unsafe_identifiers(tmp_path: Path, monkeypatch) -> None: | ||
| with pytest.raises(ValueError, match="Unsafe SQLite"): | ||
| _validate_sqlite_identifier("scan_jobs; DROP TABLE scan_jobs", "table") | ||
|
|
||
| db_file = tmp_path / "compat.db" | ||
| test_engine = create_engine(f"sqlite:///{db_file}") | ||
| with test_engine.begin() as conn: | ||
| conn.exec_driver_sql("CREATE TABLE safe_tbl (id INTEGER PRIMARY KEY)") | ||
| monkeypatch.setitem(SQLITE_COMPAT_COLUMNS, "safe_tbl", [("bad;col", "TEXT")]) | ||
| with pytest.raises(ValueError, match="Unsafe SQLite column identifier"): | ||
| ensure_sqlite_schema_compatibility(test_engine) | ||
| test_engine.dispose() |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Canonical semantic search endpoint bypasses rate limiting
High Severity
RATE_LIMITED_POST_PATHSonly includes"/search/semantic", which is the hidden compatibility alias forsemantic_search_routeinapps/api/app/api/routes.py. Requests to the canonical, OpenAPI-documented endpoint (POST /api/v1/searchandPOST /api/search, also used byapps/web/src/lib/api.ts) returnNonefrom_normalize_rate_limit_pathand bypass rate limiting completely.Reviewed by Cursor Bugbot for commit 2de5368. Configure here.