Skip to content

Commit a451ea9

Browse files
committed
remove ctx factory due to risks
1 parent 54f0851 commit a451ea9

File tree

2 files changed

+3
-37
lines changed

2 files changed

+3
-37
lines changed

openlibrary/core/fulltext.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,4 @@ async def fulltext_search_async(q, page=1, limit=100, js=False, facets=False):
8080
return ia_results
8181

8282

83-
def ctx_func_factory():
84-
"""Factory function that returns setup_ctx_site for use with async_bridge.wrap
85-
TODO: next time we use this in a different context, we should consider generalizing it.
86-
"""
87-
88-
forwarded_for = web.ctx.env.get('HTTP_X_FORWARDED_FOR', 'ol-internal')
89-
user_agent = web.ctx.env.get('HTTP_USER_AGENT', '')
90-
91-
async def setup_env_vars():
92-
from infogami.utils.delegate import create_site
93-
94-
web.ctx.site = create_site()
95-
web.ctx.env = {
96-
'HTTP_X_FORWARDED_FOR': forwarded_for,
97-
'HTTP_USER_AGENT': user_agent,
98-
}
99-
100-
return setup_env_vars
101-
102-
103-
fulltext_search = async_bridge.wrap(
104-
fulltext_search_async, init_ctx_factory=ctx_func_factory
105-
)
83+
fulltext_search = async_bridge.wrap(fulltext_search_async)

openlibrary/utils/async_utils.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,10 @@ def __init__(self):
2626
def run[T](self, coro: Coroutine[Any, Any, T]) -> T:
2727
return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
2828

29-
def wrap(
30-
self,
31-
func: Callable[P, Coroutine[Any, Any, T]],
32-
init_ctx_factory: (
33-
Callable[[], Callable[[], Coroutine[Any, Any, None]]] | None
34-
) = None,
35-
) -> Callable[P, T]:
36-
"""Wrap an async function so it can be called from sync code, preserving type hints.
37-
38-
init_ctx_factory is a factory function that returns a function that can be called to initialize the context.
39-
We must do this because we need to capture the request context when the request comes in, not when the wrapper is defined.
40-
"""
29+
def wrap(self, func: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, T]:
30+
"""Wrap an async function so it can be called from sync code, preserving type hints."""
4131

4232
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
43-
if init_ctx_factory:
44-
self.run(init_ctx_factory()())
4533
return self.run(func(*args, **kwargs))
4634

4735
return wrapper

0 commit comments

Comments
 (0)