* Update version-incompatible dependencies in examples.
* Update version-incompatible dev-dependencies.
* Replaced deprecated `criterion::black_box`.
* Add `features = "simd"` to `winnow`. This adds a transitive
dependency to `memchr`, on which we already depend on directly.
We adopted `askama_escape` with the other askama crates. It was not
updated for quite some time, but still gets 25k+ downloads / day Mon to
Fri.
This PR re-adds the crate using our current HTML escaping function.
This PR changes the filter `|tojson` so that it does not collect the
serialized data into a string, but writes the data into the target
writer directly.
This makes the benchmark run 81% (only serializing) or 39% (serializing
and escaping for HTML) faster. The benchmarked data is not a fair
representation for the data you would most likely serialize, though.
Escaped HTML characters vary in length. So, in order to select the
correct replacement two variables need to be loaded: The pointer to the
new substring and its length. Because of this the generated code is less
dense than it could be.
With this PR instead of selecting the appropriate `&str`, an `&&str` is
selected. The former consumes two words while the latter consumes only
one. Intuitively one might assume that the double dereference makes the
code slower, but the optimized lookup seems to be so much faster, so
that the change is worth its weight.
Comparing the result of `cargo bench` (best out of three runs for both):
```text
Old: [4.3592 µs 4.3675 µs 4.3764 µs]
New: [3.8691 µs 3.8766 µs 3.8860 µs]
Diff: [-11.24 % -11.24 % -12.21 % ]
```
Using only safe code is actually same as fast as the previous "unsafe"
code according to the crate's benchmark.
The code was extracted from [markup]'s escape function in [escape.rs],
written by Utkarsh Kukreti <utkarshkukreti@gmail.com>, licensed as
`MIT OR Apache-2.0`.
[markup]: https://crates.io/crates/markup
[escape.rs]: 8ec4042848/markup/src/escape.rs (L1-L21)
Previously the built-in json filter had an issue that made it unsafe to
use in HTML data. When used in HTML attributes an attacker who is able
to supply an arbitrary string that should be JSON encoded could close
the containing HTML element e.g. with `"</div>"`, and write arbitrary
HTML code afterwards as long as they use apostrophes instead of
quotation marks. The programmer could make this use case safe by
explicitly escaping the JSON result: `{{data|json|escape}}`.
In a `<script>` context the json filter was not usable at all, because
in scripts HTML escaped entities are not parsed outside of XHTML
documents. Without using the safe filter an attacker could close the
current script using `"</script>"`.
This PR fixes the problem by always escaping less-than, greater-than,
ampersand, and apostrophe characters using their JSON unicode escape
sequence `\u00xx`. Unless the programmer explicitly uses the safe
filter, quotation marks are HTML encoded as `"`. In scripts the
programmer should use the safe filter, otherwise not.