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

xmlEncode #11

Open
itsumura-h opened this issue May 9, 2022 · 3 comments
Open

xmlEncode #11

itsumura-h opened this issue May 9, 2022 · 3 comments

Comments

@itsumura-h
Copy link

itsumura-h commented May 9, 2022

Variable should be xml encoded to prevent XSS
https://nim-lang.org/docs/cgi.html#xmlEncode%2Cstring

import templates

let x = "<script>alert("hello")</script>"

let dom = tmpli html"""
<p>$x</p>
"""Dainger

so I propose a new syntax if possible

  • $if, $else $case $for => as it is
  • ${ let x = "aaa" } => as it is
  • $(x) => bind variable in html without xml encode
  • ${{x}} => bind variable in html with xml encode

then it shoud be like this

import templates

let x = "<script>alert("hello")</script>"

let dom = tmpli html"""
<p>${{x}}</p>
"""
assert dom == "<p>&lt;script&gt;alert(&quot;hello&quot;)&lt;/script&gt;</p>"
@onionhammer
Copy link
Owner

onionhammer commented May 9, 2022

Interesting idea - My only concern would be it interfering with some other nim syntax. What about adding another prefix to indicate escape; like '$!' instead of just '$' ?

i..e.

import templates

let x = "<script>alert("hello")</script>"

let dom = tmpli html"""
<p>$!x</p>
"""
assert dom == "<p>&lt;script&gt;alert(&quot;hello&quot;)&lt;/script&gt;</p>"

@itsumura-h
Copy link
Author

@onionhammer
"!" means destructive change or doing something that should not be used but is unavoidably dangerous in other programming languages.
so "$!x" prefers be like dangerouslySetInnerHTML in react, and if "$x" is escaped by default, it is safe.

@onionhammer
Copy link
Owner

Yeah good point. I wouldn't want to issue a breaking change for this, but syntactic sugar for escaping the HTML would be nice.

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

2 participants