You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tighter integration between markupsafe.Markup and other parts of turbohtml#802
Bandit/Ruff will flag code like Markup(some_dynamic_expression) as unsafe, since it cannot prove that some_dynamic_expression doesn't contain some non-sanitized user input, which doesn't play well with functions like sanitize or linkify, so you end up needing to put ignore pragmas in your code at the boundaries between those two APIs and risk introducing a security regression later on, since the pragma is still there.
Similarly there's questions about how function like sanitize and linkify should behave when they receive a Markup object or some other object implementing __html__, since the assumption should be that the given markup is pre-vetted and thus doesn't need to be sanitized.
Overall it is just a little awkward and error prone to combine Markup with the other utilites in turbohtml right now, in the same way it was awkward before with bleach and I think there's some potential for improving ergonomics and lowering the risk of introducing vulnerabilities at this boundary.
It doesn't seem realistic to simply return Markup from sanitize, since we don't know how the return value is used, but a separate function that works that way might make sense. Or a configuration knob, although that would make writing accurate type hints a little bit more annoying.
On a somewhat related note: While mixing turbohtml's version of Markup with markupsafe's version usually works at runtime (apart from isinstance checks against either version, which can certainly cause real problems), it can get awkward when type hints are involved, since type checkers will not accept one in place of the other. It might be worth considering offering an optionally installable .pth hack to globally replace markupsafe with a module that re-exports turbohtml's version at import time, to allow sidestepping those interoperability issues. There might also be something that can be done with inserting a __subclass_check__/__subclass_hook__ into both types, when markupsafe is detected and pretending turbohtmls Markup is the same as the one in markupsafe when it's available, although optional imports are generally not well supported by type checkers.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The idea
Bandit/Ruff will flag code like
Markup(some_dynamic_expression)as unsafe, since it cannot prove thatsome_dynamic_expressiondoesn't contain some non-sanitized user input, which doesn't play well with functions likesanitizeorlinkify, so you end up needing to put ignore pragmas in your code at the boundaries between those two APIs and risk introducing a security regression later on, since the pragma is still there.Similarly there's questions about how function like
sanitizeandlinkifyshould behave when they receive aMarkupobject or some other object implementing__html__, since the assumption should be that the given markup is pre-vetted and thus doesn't need to be sanitized.Overall it is just a little awkward and error prone to combine
Markupwith the other utilites inturbohtmlright now, in the same way it was awkward before withbleachand I think there's some potential for improving ergonomics and lowering the risk of introducing vulnerabilities at this boundary.It doesn't seem realistic to simply return
Markupfromsanitize, since we don't know how the return value is used, but a separate function that works that way might make sense. Or a configuration knob, although that would make writing accurate type hints a little bit more annoying.On a somewhat related note: While mixing
turbohtml's version ofMarkupwithmarkupsafe's version usually works at runtime (apart fromisinstancechecks against either version, which can certainly cause real problems), it can get awkward when type hints are involved, since type checkers will not accept one in place of the other. It might be worth considering offering an optionally installable.pthhack to globally replacemarkupsafewith a module that re-exportsturbohtml's version at import time, to allow sidestepping those interoperability issues. There might also be something that can be done with inserting a__subclass_check__/__subclass_hook__into both types, whenmarkupsafeis detected and pretendingturbohtmlsMarkupis the same as the one inmarkupsafewhen it's available, although optional imports are generally not well supported by type checkers.All reactions