Fix/api safety score contract#22
Open
KevinRusu wants to merge 2 commits into
Open
Conversation
Added testing instructions and updated environment variable descriptions in README.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR standardizes the Beacon “score” contract end-to-end to prevent inverted interpretations (10=safe, 0=scam), moves Tier-2 AI analysis networking into the extension background worker for durability/caching, and hardens/extends the server-side API (validation, origin gating, health check, logging, Docker proxy headers) with new contract tests.
Changes:
- Standardized on a single safety_score scale (0–10, 10=safe) across extension + API; removed risk_score/inversion logic.
- Moved the AI
/v1/analyzefetch from the popup UI into the background service worker and cached results per tab. - Added/updated API validation, origin allowlist auth,
/health, improved logging, Docker proxy-header support, and a new test suite.
Reviewed changes
Copilot reviewed 15 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| extension/vite.config.ts | Stops compiling an API key into the shipped extension bundle. |
| extension/src/types/api.ts | Updates response contract to safety_score (consistent safety scale). |
| extension/src/popup/App.tsx | Uses cached AI results and delegates Tier-2 analysis to background worker (no direct fetch). |
| extension/src/env.d.ts | Removes the now-eliminated __BEACON_API_KEY__ global. |
| extension/src/background/background.ts | Centralizes API payload building + fetching in the service worker; caches aiResult per tab. |
| api/tests/test_analyze.py | Adds contract/validation/origin tests to pin the safety-score invariant and request limits. |
| api/tests/conftest.py | Ensures env/config and import paths are set correctly for API tests. |
| api/schemas.py | Renames response field to safety_score and adds request size constraints. |
| api/requirements-dev.txt | Adds dev deps for running API tests. |
| api/README.md | Documents score convention, env vars, and test running. |
| api/providers/mock_provider.py | Updates mock provider to return consistent safety-score/label/action mapping. |
| api/providers/gemini_provider.py | Updates prompt to safety-score scale and adds prompt-injection hardening boundaries. |
| api/main.py | Switches auth to origin allowlist, updates rate limiting defaults, adds /health, improves logging. |
| api/Dockerfile | Runs uvicorn with --proxy-headers for correct client-IP rate limiting behind proxies. |
| api/auth.py | Replaces shared-key auth with origin allowlist verification. |
| api/.env.example | Updates example env vars for new origin allowlist + rate limit format. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+27
to
+37
| if not allowed_origins(): | ||
| if use_mock: | ||
| logger.warning( | ||
| "ORIGIN CHECK DISABLED — ALLOWED_EXTENSION_ORIGINS not set (mock/dev mode only)" | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| "ALLOWED_EXTENSION_ORIGINS is not set with USE_MOCK=false: " | ||
| "the API will accept requests from any origin. Set it to the " | ||
| "extension origin (chrome-extension://<id>) before deploying." | ||
| ) |
Comment on lines
+8
to
+24
| import pytest | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from main import app | ||
|
|
||
| client = TestClient(app) | ||
|
|
||
| BASE_BODY = { | ||
| "url": "https://example.com/login", | ||
| "text": "Enter your password to continue.", | ||
| "context": "page_body", | ||
| } | ||
|
|
||
|
|
||
| def analyze(score: int, headers: dict | None = None, **overrides): | ||
| body = {**BASE_BODY, "heuristic_score": score, **overrides} | ||
| return client.post("/v1/analyze", json=body, headers=headers or {}) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
/healthendpoint, and 17 automated tests for the API.Changes by area
API (
api/)schemas.py— renamed the response fieldrisk_score→safety_score, added size limits on request fieldsmock_provider.py,gemini_provider.py— safety-scale scoring logic, hardened prompt with clear boundaries around untrusted page textauth.py— replaced the shared API key check with an origin allowlistmain.py— fixed rate limiting to work behind a proxy, added/health, better error loggingDockerfile— runs uvicorn with--proxy-headersso rate limiting sees real client IPstests/— new test suite (17 tests) covering the score mapping, origin checks, and request validationREADME.md,.env.example— updated to matchExtension (
extension/)types/api.ts,App.tsx— updated to the newsafety_scorefield, no more score conversionbackground/background.ts— newcheckWithAIhandler; this is now the only place that makes network callsvite.config.ts,env.d.ts— stopped baking the API key into the buildTesting done
pytest api/tests/)