Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/plugin-web/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ impl FetchClient {
Err(e) => return soft_or_hard(&e),
};
let base_url = final_url.clone();
let rendering =
on_blocking_thread(move || extract_html(&decoded, Some(&base_url), raw));
let rendering = on_blocking_thread(move || {
extract_html(&decoded, Some(&base_url), raw, max_chars)
});
let Some((markdown, extraction)) = rendering.await else {
return Err(did_not_finish());
};
Expand Down
66 changes: 66 additions & 0 deletions crates/plugin-web/src/fetch/tests-body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,72 @@ async fn gzip_bomb_refused_on_decompressed_count() {
assert!(result.contains("exceeds"), "got: {result}");
}

/// The whole page is over the default `max_chars`, so falling back to it would
/// return only the menu at its top.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_short_article_below_a_long_menu_is_returned_and_raw_returns_the_menu() {
let (port, _hits) = spawn_server().await;
let tool = loopback_tool(port);
let url = format!("http://localhost:{port}/archive");

let out = tool
.call(serde_json::json!({ "url": url }))
.await
.expect("an archive fetch must succeed")
.text()
.to_owned();
let (header, body) = split_header(&out);
assert!(
header.contains("extraction: readability") && header.contains("truncated: false"),
"got: {header}"
);
assert!(
body.contains("The article a reader came for"),
"got: {body}"
);
assert!(!body.contains("Archive entry"), "got: {body}");

let out = tool
.call(serde_json::json!({ "url": url, "raw": true }))
.await
.expect("a raw archive fetch must succeed")
.text()
.to_owned();
let (header, body) = split_header(&out);
assert!(
header.contains("extraction: raw-html") && header.contains("truncated: true"),
"got: {header}"
);
assert!(body.contains("Archive entry number 0"), "got: {body}");
assert!(
!body.contains("The article a reader came for"),
"got: {body}"
);
}

/// `readabilityrs` finds no article, so the whole page is returned and cut
/// at `max_chars`, though it is over it.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_page_with_no_article_over_max_chars_returns_the_cut_whole_page() {
let (port, _hits) = spawn_server().await;
let tool = loopback_tool(port);
let url = format!("http://localhost:{port}/topics");

let out = tool
.call(serde_json::json!({ "url": url }))
.await
.expect("a topics fetch must succeed")
.text()
.to_owned();
let (header, body) = split_header(&out);

assert!(
header.contains("extraction: raw-html") && header.contains("truncated: true"),
"got: {header}"
);
assert!(body.contains("Forum topic number 0"), "got: {body}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn text_over_max_chars_is_truncated_on_char_boundary() {
let (port, _hits) = spawn_server().await;
Expand Down
39 changes: 39 additions & 0 deletions crates/plugin-web/src/fetch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! the body tests (size caps, truncation, content types, charsets) sit in
//! the child modules and share these fixtures.

use std::fmt::Write as _;
use std::io::Write;
use std::net::{IpAddr, SocketAddr};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -158,6 +159,42 @@ async fn large() -> Html<String> {
Html(format!("<html><body><p>{filler}</p></body></html>"))
}

/// A short article below an archive menu longer than the default
/// `max_chars`.
async fn archive() -> Html<String> {
let mut menu = String::new();
for i in 0..1_500 {
write!(
menu,
r#"<li><a href="/archive/{i}">Archive entry number {i}</a></li>"#
)
.expect("writing to a String cannot fail");
}
let prose =
"<p>The article a reader came for, which says one thing in plain words.</p>".repeat(5);
Html(format!(
"<html><head><title>T</title></head><body><nav><ul>{menu}</ul></nav>\
<article><h1>The article</h1>{prose}</article></body></html>"
))
}

/// A forum topic list, longer than the default `max_chars`, served only
/// inside `<noscript>`, so `readabilityrs` finds no article in it.
async fn topics() -> Html<String> {
let mut rows = String::new();
for i in 0..1_500 {
write!(
rows,
"<li><a href=\"/t/{i}\">Forum topic number {i}</a></li>"
)
.expect("writing to a String cannot fail");
}
Html(format!(
"<html><head><title>T</title></head><body><div id=\"app\"></div>\
<noscript><ul>{rows}</ul></noscript></body></html>"
))
}

async fn gzip_bomb() -> impl IntoResponse {
let raw = "A".repeat(200_000);
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
Expand Down Expand Up @@ -310,6 +347,8 @@ async fn spawn_server() -> (u16, Arc<AtomicUsize>) {
.route("/target", get(target))
.route("/unicode", get(unicode))
.route("/large", get(large))
.route("/archive", get(archive))
.route("/topics", get(topics))
.route("/gzip", get(gzip_bomb))
.route("/liar", get(liar_content_length))
.route("/table", get(table))
Expand Down
9 changes: 5 additions & 4 deletions crates/plugin-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
//! extraction. The tool sends one GET request and chooses how to render
//! the response from its `Content-Type`. For an HTML page, it extracts
//! the main article with `readabilityrs` and renders it to markdown.
//! When a page yields too little article text, or the call asks for
//! `raw`, `htmd` converts the whole page to markdown. Any other text
//! body, such as JSON, XML, or plain text, is decoded and returned
//! verbatim. The tool refuses every other type, and a response that
//! When the call asks for `raw`, `htmd` converts the whole page to
//! markdown. It also does so in place of the article when the article is
//! empty, and when it is too small a part of the page and the whole page
//! fits in `max_chars`. Any other text body, such as JSON, XML, or plain
//! text, is decoded and returned verbatim. The tool refuses every other type, and a response that
//! declares none.
//!
//! The answer is untrusted output: three header lines, then a blank
Expand Down
Loading
Loading