Skip to content
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

fix(ASGI mounts): Prevent accidental scope overrides by mounted ASGI apps #3945

Merged
merged 2 commits into from
Jan 11, 2025

Conversation

provinzkraut
Copy link
Member

@provinzkraut provinzkraut commented Jan 11, 2025

When mounting ASGI apps, there's no guarantee they won't overwrite some key in the scope that we rely on, e.g. scope["app"], which is what caused #3934.

To prevent this, I've implemented two things:

  1. Do not store the Litestar instance under the generic app key, but the more specific litestar_app key. I've also added a Litestar.from_scope method, which can be used to safely access the current app from the sope
  2. Added a new parameter copy_scope to the ASGI route handler, which, when set to True will copy the scope before calling into the mounted ASGI app. This should make things behave more as expected, since it truly give the called app its own environment without causing any side-effects. Since this change might break some things, I've left it with a default of None, which does not copy the scope, but will issue a warning if the mounted app modified it, enabling users to decide how to deal with that situation

Fixes #3934

@provinzkraut provinzkraut requested review from a team as code owners January 11, 2025 16:42
@github-actions github-actions bot added area/connection area/docs This PR involves changes to the documentation area/handlers This PR involves changes to the handlers area/middleware This PR involves changes to the middleware area/testing area/types This PR involves changes to the custom types size: medium type/bug labels Jan 11, 2025
Copy link

codecov bot commented Jan 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.34%. Comparing base (a814224) to head (98ac51d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3945   +/-   ##
=======================================
  Coverage   98.34%   98.34%           
=======================================
  Files         347      347           
  Lines       15727    15743   +16     
  Branches     1738     1740    +2     
=======================================
+ Hits        15467    15483   +16     
  Misses        124      124           
  Partials      136      136           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@euri10
Copy link
Contributor

euri10 commented Jan 11, 2025

dumb question maybe but what would happen if you mount a litestar app in a litestar app, won't you end up with the same issue you're fixing for other asgi apps ?

@provinzkraut
Copy link
Member Author

dumb question maybe but what would happen if you mount a litestar app in a litestar app, won't you end up with the same issue you're fixing for other asgi apps ?

True, didn't think of that case as it's not something we really advertise and I can't immediately think of a use case.

copy_scope=True would fix this though. Not sure if we want to special-case app in the scope too much, since mounted apps might modify other things as well, which is why I added the copy option, so issues with this can be prevented outright.

If we want to handle this one case in particular, we could just "restore" the original app key after calling the mounted ASGI app?

@euri10
Copy link
Contributor

euri10 commented Jan 11, 2025

The use case to is pretty much the same as this one: you mount something you have no control on ?

Realistically we'll have more Litestar users mounting FastAPI for the time being but you never know ;)

This was just a small remark, every asgi app is a squatter of the same namespace, but I'm all for squatts generally speaking so probably leaving this as is is a good option if ths copy_scope let the user solve the issue, it gives Litestar another edge other probably don't have.

Copy link
Contributor

@euri10 euri10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent :)

litestar/testing/client/base.py Show resolved Hide resolved
docs/usage/applications.rst Outdated Show resolved Hide resolved
@provinzkraut provinzkraut enabled auto-merge (squash) January 11, 2025 18:56
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/3945

@provinzkraut provinzkraut merged commit 2db1f4d into main Jan 11, 2025
28 checks passed
@provinzkraut provinzkraut deleted the fix-3934 branch January 11, 2025 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/connection area/docs This PR involves changes to the documentation area/handlers This PR involves changes to the handlers area/middleware This PR involves changes to the middleware area/testing area/types This PR involves changes to the custom types pr/internal size: medium type/bug
Projects
None yet
3 participants