-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hotwire-django/xss-vuln-fix
Remove content from f-strings
- Loading branch information
Showing
12 changed files
with
342 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,63 @@ | ||
# Standard Library | ||
from functools import lru_cache | ||
|
||
# Django | ||
from django.template import Context, Template | ||
from django.template.engine import Engine | ||
from django.utils.safestring import mark_safe | ||
|
||
# Local | ||
from .constants import Action | ||
|
||
|
||
def render_turbo_stream(action: Action, target: str, content: str = "") -> str: | ||
def render_turbo_stream( | ||
action: Action, target: str, content: str = "", is_safe: bool = False | ||
) -> str: | ||
"""Wraps content in correct <turbo-stream> tags. | ||
:param action: action type | ||
:param target: the DOM ID target of the stream | ||
:param content: content to be wrapped. Can be empty. | ||
:param is_safe: mark content safe for HTML escaping. | ||
:return: *<turbo-stream>* string | ||
""" | ||
return f'<turbo-stream action="{action.value}" target="{target}"><template>{content.strip()}</template></turbo-stream>' | ||
if is_safe: | ||
content = mark_safe(content) | ||
|
||
return get_turbo_stream_template().render( | ||
Context({"action": action.value, "target": target, "content": content}) | ||
) | ||
|
||
def render_turbo_frame(dom_id: str, content: str = "") -> str: | ||
|
||
def render_turbo_frame(dom_id: str, content: str = "", is_safe: bool = False) -> str: | ||
""" | ||
Wraps a response in correct *<turbo-frame>* tags. | ||
:param dom_id: a DOM ID present in the content | ||
:param content: content of the turbo-frame | ||
:param is_safe: mark content safe for HTML escaping. | ||
:return: *<turbo-frame>* string | ||
""" | ||
return f'<turbo-frame id="{dom_id}">{content.strip()}</turbo-frame>' | ||
if is_safe: | ||
content = mark_safe(content) | ||
|
||
return get_turbo_frame_template().render( | ||
Context({"dom_id": dom_id, "content": content}) | ||
) | ||
|
||
|
||
@lru_cache() | ||
def get_turbo_stream_template() -> Template: | ||
return Engine.get_default().from_string( | ||
'<turbo-stream action="{{ action }}" target="{{ target }}"><template>{{ content }}</template></turbo-stream>', | ||
) | ||
|
||
|
||
@lru_cache() | ||
def get_turbo_frame_template() -> Template: | ||
return Engine.get_default().from_string( | ||
'<turbo-frame id="{{ dom_id }}">{{ content }}</turbo-frame>' | ||
) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<div>my content</div> | ||
<div>{{ msg }}</div> |
This file contains 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
Oops, something went wrong.