Skip to content

fix(cors): apply a CORS policy set only in .env - #305

Merged
andrevanzuydam merged 4 commits into
v3from
fix/cors-origins-from-dotenv
Nov 13, 2026
Merged

andrevanzuydam merged 4 commits into
v3from
fix/cors-origins-from-dotenv

Conversation

@M1gael

@M1gael M1gael commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

TINA4_CORS_ORIGINS set only in .env (as chapter 10 says to) was ignored under run(), asgi() and TestClient. The CORS singleton is built at import, before .env is loaded, and was never rebuilt. Cross-origin requests from an allowed origin got no Access-Control-Allow-Origin, plus a log line asking for the variable that was already set.

Fix: _refresh_cors_from_env() re-reads the TINA4_CORS_* settings into the singleton when they differ from what it was built from, each time it runs. It updates the object in place, so a reference held elsewhere stays current. _load_project_env (covering run() and asgi()) and TestClient both call it. It only touches a plain CorsMiddleware that is exactly as built: a subclass, a test double, or an instance changed by hand is left as it is (a value set by hand that equals the built one cannot be told apart).

Limits: .env is still loaded first-wins, so editing it after the first load changes nothing until a restart. A TestClient built inside a serving process (e.g. from a route) applies .env keys added since start to the live server, and a request served at that moment can get a response that mixes the old and new policy.

Tests: tests/test_cors_origins_from_dotenv.py, 19 cases: real child servers under run() and asgi(), TestClient, repeated changes, a held reference, and each left-alone case. On v3, 17 fail (12 of them only because the function does not exist there); the 2 refusal controls pass.

Please squash-merge: the earlier commit messages describe designs this one replaced (rebuilding the object, keeping only some subclasses).

The CORS singleton read TINA4_CORS_* at import, before run()/asgi() load
.env, and was never rebuilt. A policy written only in .env was ignored:
no Access-Control-Allow-Origin, and a log line telling the operator to
set the variable already set. Rebuild it in _load_project_env, next to
the existing health-path fix-up.

Test: real child servers under run() and asgi(), policy only in .env.
Signed-off-by: Michael <michael@codeinfinity.co.za>
@M1gael

M1gael commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

I have read the Tina4 Contributor Licence Agreement and I agree to it.

@M1gael M1gael closed this Nov 10, 2026
@M1gael M1gael reopened this Nov 10, 2026
@M1gael
M1gael marked this pull request as draft November 10, 2026 13:35
@M1gael

M1gael commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Withdrawn for rework; a corrected PR will follow.

@M1gael M1gael closed this Nov 10, 2026
The CORS singleton read TINA4_CORS_* at import, before run(), asgi() or
TestClient loaded .env, and was never rebuilt. A policy written only in
.env was ignored: no Access-Control-Allow-Origin, and a log line asking
for the variable that was already set.

_refresh_cors_from_env() rebuilds it when the TINA4_CORS_* settings
differ from what the live instance was built from (recorded on the
instance). A test double is left alone and a CorsMiddleware subclass is
rebuilt as itself. _load_project_env and TestClient both call it.

Tests: run() and asgi() child servers and TestClient with the policy only
in .env; a swapped-in instance built from a different env; test double
and subclass kept.

Signed-off-by: Michael <michael@codeinfinity.co.za>
@M1gael M1gael reopened this Nov 10, 2026
@M1gael

M1gael commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Reworked after review and reopened.

Rebuilding it with no arguments raised TypeError inside the boot and
TestClient. The refresh now keeps such an instance.

Signed-off-by: Michael <michael@codeinfinity.co.za>
@M1gael
M1gael marked this pull request as ready for review November 10, 2026 18:25
@M1gael

M1gael commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

Withdrawn for rework.

@M1gael M1gael closed this Nov 11, 2026
The refresh replaced whatever _cors held once the settings changed: a
spec'd mock passed the isinstance check and was swapped for a bare one,
a CorsMiddleware configured by hand was rebuilt and lost its settings
even when nothing had changed, and references held elsewhere went
stale.

Each instance now records the TINA4_CORS_* values it read and its
attributes as built. The refresh acts only on an exact CorsMiddleware
that is still as built and whose recorded settings differ from the
environment, and updates it in place.

Signed-off-by: Michael <michael@codeinfinity.co.za>
@M1gael M1gael reopened this Nov 11, 2026
@M1gael

M1gael commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

Reworked after review and reopened.

@andrevanzuydam
andrevanzuydam merged commit 15cf8a8 into v3 Nov 13, 2026
14 checks passed
@andrevanzuydam
andrevanzuydam deleted the fix/cors-origins-from-dotenv branch November 13, 2026 09:08
@andrevanzuydam andrevanzuydam mentioned this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants