Repository navigation
fix(cors): apply a CORS policy set only in .env - #305
Merged
Merged
Conversation
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>
Collaborator
Author
|
I have read the Tina4 Contributor Licence Agreement and I agree to it. |
Collaborator
Author
|
Withdrawn for rework; a corrected PR will follow. |
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>
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
marked this pull request as ready for review
November 10, 2026 18:25
Collaborator
Author
|
Withdrawn for rework. |
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>
Collaborator
Author
|
Reworked after review and reopened. |
Merged
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.
TINA4_CORS_ORIGINSset only in.env(as chapter 10 says to) was ignored underrun(),asgi()andTestClient. The CORS singleton is built at import, before.envis loaded, and was never rebuilt. Cross-origin requests from an allowed origin got noAccess-Control-Allow-Origin, plus a log line asking for the variable that was already set.Fix:
_refresh_cors_from_env()re-reads theTINA4_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(coveringrun()andasgi()) andTestClientboth call it. It only touches a plainCorsMiddlewarethat 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:
.envis still loaded first-wins, so editing it after the first load changes nothing until a restart. ATestClientbuilt inside a serving process (e.g. from a route) applies.envkeys 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 underrun()andasgi(),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).