News for hackers. Stuff that matters.
Re:My Own Python Web Framework by ptx on Wednesday February 22, 2023 @03:35PM (#34902245)
This looks like an API designed to encourage XSS vulnerabilities:

  def render(data):
      return f"<h1>Last regenerated at: {data['time']}</h1>", {}
What if data['time'] is '<script>alert("oops!")</script>'?

While JavaScript's template literals [1] would allow this kind of API to work (because the prefix of the template string can be a function that escapes the parameters), Python's f-strings doesn't have equivalent functionality. The tuple returned from the render function should probably include an additional item containing the parameters for the format string (which shouldn't be an f-string).

See also [2].

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[2] https://en.wikipedia.org/wiki/Uncontrolled_format_string

Re:My Own Python Web Framework by seszett on Wednesday February 22, 2023 @03:53PM (#34902552)
Data isn't user-supplied data, so just don't put Javascript code there yourself and you'll be fine.
Re:My Own Python Web Framework by ptx on Wednesday February 22, 2023 @04:40PM (#34903290)
The second example from the documentation [1] disagrees. It displays HTTP header values, which are user supplied, without properly encoding them.

Also, it's not just about JavaScript. If values aren't properly encoded as HTML, the app is just plain broken when it tries to display text that contains certain character sequences. For example, the app is not capable of displaying the data value "To create a heading in HTML, use <h1>My heading</h1>".

The values need to be separated from the format string and passed through an encoding function such as html.escape [2].

[1] https://github.com/healeycodes/jar#fresh-pages

[2] https://docs.python.org/3/library/html.html#html.escape

Re:My Own Python Web Framework by Jiocus on Wednesday February 22, 2023 @05:49PM (#34904165)
Sure, you're right about that and I agree with the specifics, but your message doesn't feel relevant to the OP or the discussion.

Is it a reasonable expectation that every dev and blog posts must rehash all of OWASP and audit their experimental frameworks (now several years in the making) before publishing?

Re:My Own Python Web Framework by ptx on Wednesday February 22, 2023 @06:02PM (#34904300)
Templating is a core concern of a web framework, so a fundamental design flaw in the templating mechanism seems relevant.