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

Feature request: JavaScript sanitisation #23

Closed
a-h opened this issue Oct 4, 2021 · 0 comments
Closed

Feature request: JavaScript sanitisation #23

a-h opened this issue Oct 4, 2021 · 0 comments

Comments

@a-h
Copy link
Owner

a-h commented Oct 4, 2021

Script blocks support

This code:

{% script functionName(a int, b int) %}
  alert(a+b);
{% endscript %}

Will render:

<script>function functionName(a, b){alert(a+b};}</script>

Script blocks should not support script tags at all and should warn to use the {% script %} template instead

{% templ ScriptBlocksNotSupported(script string) %}
  <script type="text/javascript">
    {%= script %}
  </script>
{% endtempl %}

onClick / on* handlers should support constant values as well as {% script %} templates

{% templ BasicTemplate() %}
  <div>
	  <a onClick="alert('hello');">{%= "Say hello" %}</a>
  </div>
{% endtempl %}

Dynamic onClick / on* handlers with templates use sanitised values by forcing the use of {% script %} templates

The Google SafeHTML code describes how this can work: https://github.com/google/safehtml/blob/2057dd9c30f9e264f4d01c29d886d51f1b519302/script.go#L67

The named variables are inserted into the JavaScript template JSON escaped.

To render raw scripts, a templ.RawScript component can be used

This is described in the documentation, but not provided to encourage not doing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant