Skip to content

Commit

Permalink
v1.2.5 🕊️
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato authored Mar 13, 2022
1 parent 0fed9b3 commit be6d340
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion blacksheep/server/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def view(

if model:
return view(self.templates, self.full_view_name(name), model, **kwargs)
return view(self.templates, self.full_view_name(name))
return view(self.templates, self.full_view_name(name), **kwargs)

async def view_async(
self, name: Optional[str] = None, model: Optional[Any] = None, **kwargs
Expand Down
17 changes: 10 additions & 7 deletions blacksheep/server/csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ def decorator(fn):
return decorator


def use_anti_forgery(app: Application) -> AntiForgeryHandler:
def use_anti_forgery(
app: Application, handler: Optional[AntiForgeryHandler] = None
) -> AntiForgeryHandler:
"""
Configures Anti-Forgery validation on the given application, to protect against
Cross-Site Request Forgery (XSRF/CSRF) attacks.
Expand All @@ -385,12 +387,13 @@ def use_anti_forgery(app: Application) -> AntiForgeryHandler:
When an anti-forgery token is rendered in a view, the HTTP Response object receives
also a cookie with a control value.
"""
anti_forgery_handler = AntiForgeryHandler()
if handler is None:
handler = AntiForgeryHandler()

env = getattr(app, "templates_environment")

if env is None: # pragma: no cover
anti_forgery_handler.logger.info(
handler.logger.info(
"Templating is not configured on the application, extensions to render "
"anti-forgery tokens with Jinja2 won't be configured."
)
Expand All @@ -401,14 +404,14 @@ def use_anti_forgery(app: Application) -> AntiForgeryHandler:
assert isinstance(env, Environment)

class BoundAntiForgeryInputExtension(AntiForgeryInputExtension):
af_handler = anti_forgery_handler
af_handler = handler

class BoundAntiForgeryValueExtension(AntiForgeryValueExtension):
af_handler = anti_forgery_handler
af_handler = handler

env.add_extension(BoundAntiForgeryInputExtension)
env.add_extension(BoundAntiForgeryValueExtension)

app.middlewares.append(anti_forgery_handler)
app.middlewares.append(handler)

return anti_forgery_handler
return handler

0 comments on commit be6d340

Please sign in to comment.