keithasaurus / simple_html Goto Github PK
View Code? Open in Web Editor NEWfast, templateless html generation
License: MIT License
fast, templateless html generation
License: MIT License
Thanks for this package. I built a very similar project in TypeScript a few years ago, having completions and types makes a big difference.
I've been trying the package and noticed that there's no escape
ing of attributes keys or values. On some scenarios you might need to take user input and present it as attribute keys or values.
For example:
>>> # this is ok
>>> user_input = "red"
>>> div({"class": user_input})
<div class="red"></div>
>>> # this is xss
>>> user_input = '" onmouseenter="alert(1)" noop="'
>>> div({"class": user_input})
<div class="" onmouseenter="alert(1)" noop=""></div>
>>> # this is xss
>>> user_input = 'onmouseenter="alert(1)" noop'
>>> div({user_input: "1"})
<div onmouseenter="alert(1)" noop="1"></div>
I suggest escape
ing both attribute keys and values to prevent this. On my testing I've tried the following which has successfully prevented the issue.
>>> # this is ok
>>> user_input = '" onmouseenter="alert(1)" noop="'
>>> div({"class": escape(user_input)})
<div class="" onmouseenter="alert(1)" noop=""></div>
On the key side of attributes simple escape
ing works at least on my basic tests but I would throw a few additional replacements that I've seen on other libraries to be on the safe side.
>>> # this is ok
>>> user_input = 'onmousenter="alert(1)" noop'
>>> div({escape(user_input): "1"})
<div onmousenter="alert(1)" noop="1"></div>
>>> # this is more ok?
>>> def escape_attr_key(key: str) -> str:
return (
escape(key)
.replace("=", "=")
.replace("\\", "\")
.replace("`", "`")
)
>>> user_input = 'onmousenter="alert(1)" noop'
>>> div({escape_attr_key(user_input): "1"})
<div onmousenter="alert(1)" noop="1"></div>
What do you think?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.