Skip to content

Commit ac94ad4

Browse files
authored
fix(fetchers): avoid utf-8 panic in hn html stripping (#115)
Iterate `strip_html_tags` input with `char_indices()` and compute the `<p>`-lookahead slice from `idx + c.len_utf8()` instead of `html.len() - (html.len() - result.len())`, which dropped UTF-8 byte alignment when non-ASCII characters had been skipped from `result`. Prevents a panic on untrusted HN item/comment text.
1 parent e95d349 commit ac94ad4

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

crates/fetchkit/src/fetchers/hackernews.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,12 @@ fn strip_html_tags(html: &str) -> String {
320320
let mut result = String::with_capacity(html.len());
321321
let mut in_tag = false;
322322

323-
for c in html.chars() {
323+
for (idx, c) in html.char_indices() {
324324
match c {
325325
'<' => {
326326
in_tag = true;
327327
// Check for <p> tags -> newlines
328-
let rest: String = html[html.len() - (html.len() - result.len())..]
329-
.chars()
330-
.take(3)
331-
.collect();
328+
let rest: String = html[idx + c.len_utf8()..].chars().take(3).collect();
332329
if rest.starts_with("p>") || rest.starts_with("br") {
333330
result.push('\n');
334331
}
@@ -393,6 +390,7 @@ mod tests {
393390
fn test_strip_html_tags() {
394391
assert_eq!(strip_html_tags("Hello <b>world</b>"), "Hello world");
395392
assert_eq!(strip_html_tags("a &amp; b"), "a & b");
393+
assert_eq!(strip_html_tags("ab<é>xy<"), "abxy");
396394
}
397395

398396
#[test]

0 commit comments

Comments
 (0)