-
Notifications
You must be signed in to change notification settings - Fork 59
Add failing tests for #652: purchase CORS violation & mock API in production #666
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -373,5 +373,87 @@ | |
|
|
||
| s.add(is_blacklisted) | ||
| s.add(z3.Not(rejected)) | ||
|
|
||
| assert s.check() == z3.unsat | ||
|
|
||
| assert s.check() == z3.unsat | ||
|
|
||
|
|
||
| # --- Issue #652: CORS must support production origins --- | ||
|
|
||
| def test_configure_cors_includes_production_origin(): | ||
| """Test that default CORS config includes the production origin (https://promptdriven.ai). | ||
|
|
||
| Issue #652: The purchase flow on promptdriven.ai fails with a CORS violation because | ||
| the CORS middleware only allows localhost origins by default, blocking requests from | ||
| the production domain. | ||
|
|
||
| This test fails on the buggy code because configure_cors() defaults to only | ||
| localhost:3000 and localhost:5173 origins. | ||
| """ | ||
| app = MagicMock(spec=FastAPI) | ||
| configure_cors(app) | ||
|
|
||
| call_args = app.add_middleware.call_args | ||
| kwargs = call_args[1] | ||
| origins = kwargs["allow_origins"] | ||
|
|
||
| assert "https://promptdriven.ai" in origins, ( | ||
Check failureCode scanning / CodeQL Incomplete URL substring sanitization High test
The string
https://promptdriven.ai Error loading related location Loading Copilot AutofixAI 2 months ago Copilot could not generate an autofix suggestion Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support. |
||
| f"Production origin 'https://promptdriven.ai' must be in default CORS origins. " | ||
| f"Got: {origins}" | ||
| ) | ||
|
|
||
|
|
||
| def test_configure_cors_allows_production_https(): | ||
| """Test that CORS configuration supports HTTPS production origins. | ||
|
|
||
| Issue #652: Browser preflight requests from https://promptdriven.ai are blocked | ||
| because Access-Control-Allow-Origin header doesn't include the production domain. | ||
| """ | ||
| app = MagicMock(spec=FastAPI) | ||
| configure_cors(app) | ||
|
|
||
| call_args = app.add_middleware.call_args | ||
| kwargs = call_args[1] | ||
| origins = kwargs["allow_origins"] | ||
|
|
||
| has_production = any( | ||
| origin.startswith("https://") and "promptdriven" in origin | ||
| for origin in origins | ||
| ) | ||
| assert has_production, ( | ||
| f"CORS config must include at least one production HTTPS origin for promptdriven.ai. " | ||
| f"Got only: {origins}" | ||
| ) | ||
|
|
||
|
|
||
| def test_configure_cors_localhost_still_present(): | ||
| """Test that localhost origins remain after adding production origins. | ||
|
|
||
| Ensures backward compatibility: adding production origins should not remove | ||
| the existing localhost development origins. | ||
| """ | ||
| app = MagicMock(spec=FastAPI) | ||
| configure_cors(app) | ||
|
|
||
| call_args = app.add_middleware.call_args | ||
| kwargs = call_args[1] | ||
| origins = kwargs["allow_origins"] | ||
|
|
||
| assert "http://localhost:3000" in origins | ||
| assert "http://localhost:5173" in origins or "http://127.0.0.1:5173" in origins | ||
|
|
||
|
|
||
| def test_configure_cors_rejects_unconfigured_origin(): | ||
| """Test that CORS does not use a wildcard (*) that would allow any origin. | ||
|
|
||
| Security: CORS should explicitly list allowed origins, not use '*'. | ||
| """ | ||
| app = MagicMock(spec=FastAPI) | ||
| configure_cors(app) | ||
|
|
||
| call_args = app.add_middleware.call_args | ||
| kwargs = call_args[1] | ||
| origins = kwargs["allow_origins"] | ||
|
|
||
| assert "*" not in origins, ( | ||
| "CORS should not use wildcard '*' — explicit origins are required for security." | ||
| ) | ||
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
Copilot Autofix
AI 2 months ago
General fix: instead of checking that the production hostname appears as a substring of the full URL, parse the URL and assert on its
hostname(and, if desired, scheme/path). This follows the guidance to operate on structured URL components rather than raw substrings.Best concrete fix here:
tests/core/test_cloud.py, importurlparse(fromurllib.parse) alongside existing imports.test_purchase_endpoint_url_resolves_to_production, after obtainingurl, parse it withurlparse(url)and assert thatparsed.hostnameequals the expected production hostname.httpsto preserve the intent that this is a production Cloud Functions URL, but the minimal change needed to address the CodeQL warning is to replace the substring check with a hostname equality check usingurlparse.Specific changes:
Add
from urllib.parse import urlparseafter the other imports at the top oftests/core/test_cloud.py.Replace:
with:
leaving the subsequent checks about
processPddcPurchasein the path and absence of “mock/local” substrings unchanged.No other files or logic need to be modified.