Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): reject Office documents as raw downloads

llke9d253ceabe3
diff --git a/moli-web-mime/src/classification.rs b/moli-web-mime/src/classification.rs
index a50fca152..2d50fdbbe 100644
--- a/moli-web-mime/src/classification.rs
+++ b/moli-web-mime/src/classification.rs
@@ -170,6 +170,42 @@ pub fn is_webassembly_mime(input: &str) -> bool {
mime_essence(input).is_some_and(|mime| mime == "application/wasm")
}
+pub(crate) fn is_office_document_mime_essence(input: &str) -> bool {
+ matches!(
+ input,
+ "application/msexcel"
+ | "application/mspowerpoint"
+ | "application/msword"
+ | "application/msword-template"
+ | "application/vnd.ms-excel"
+ | "application/vnd.ms-excel.addin.macroenabled.12"
+ | "application/vnd.ms-excel.sheet.binary.macroenabled.12"
+ | "application/vnd.ms-excel.sheet.macroenabled.12"
+ | "application/vnd.ms-excel.template.macroenabled.12"
+ | "application/vnd.ms-powerpoint"
+ | "application/vnd.ms-powerpoint.addin.macroenabled.12"
+ | "application/vnd.ms-powerpoint.presentation.macroenabled.12"
+ | "application/vnd.ms-powerpoint.slideshow.macroenabled.12"
+ | "application/vnd.ms-powerpoint.template.macroenabled.12"
+ | "application/vnd.ms-word"
+ | "application/vnd.ms-word.document.12"
+ | "application/vnd.ms-word.document.macroenabled.12"
+ | "application/vnd.ms-word.template.macroenabled.12"
+ | "application/vnd.msword"
+ | "application/vnd.openxmlformats-officedocument.presentationml.presentation"
+ | "application/vnd.openxmlformats-officedocument.presentationml.slideshow"
+ | "application/vnd.openxmlformats-officedocument.presentationml.template"
+ | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
+ | "application/vnd.openxmlformats-officedocument.spreadsheetml.template"
+ | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ | "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
+ | "application/vnd.presentation-openxml"
+ | "application/vnd.presentation-openxmlm"
+ | "application/vnd.spreadsheet-openxml"
+ | "application/vnd.wordprocessing-openxml"
+ )
+}
+
/// Returns whether a navigation response can be represented by Moli's
/// lightweight Document surface. Raw main-resource handling is deliberately
/// broader: binary responses can be returned to a caller without becoming a
@@ -202,6 +238,7 @@ pub fn is_binary_document_mime_type(input: &str) -> bool {
|| is_font_mime_essence(&essence)
|| is_image_mime_essence(&essence)
|| is_video_mime_essence(&essence)
+ || is_office_document_mime_essence(&essence)
|| matches!(
essence.as_str(),
"application/gzip"
diff --git a/moli-web-mime/src/response_policy.rs b/moli-web-mime/src/response_policy.rs
index 15f47bc0d..3290e4465 100644
--- a/moli-web-mime/src/response_policy.rs
+++ b/moli-web-mime/src/response_policy.rs
@@ -1,6 +1,7 @@
use crate::classification::{
is_audio_mime_essence, is_css_mime, is_font_mime_essence, is_image_mime_essence,
- is_javascript_mime, is_json_mime_essence, is_video_mime_essence,
+ is_javascript_mime, is_json_mime_essence, is_office_document_mime_essence,
+ is_video_mime_essence,
};
use crate::destination::FetchDestination;
use crate::headers::{extract_response_mime_essence, extract_response_mime_type};
@@ -69,34 +70,13 @@ pub fn should_opaque_response_be_blocked_by_orb(headers: &[(String, Vec<u8>)]) -
|| essence == "text/csv"
|| essence == "text/vtt"
|| is_font_mime_essence(&essence)
+ || is_office_document_mime_essence(&essence)
|| matches!(
essence.as_str(),
- "application/msexcel"
- | "application/mspowerpoint"
- | "application/msword"
- | "application/msword-template"
- | "application/vnd.apple.mpegurl"
+ "application/vnd.apple.mpegurl"
| "application/vnd.ces-quickpoint"
| "application/vnd.ces-quicksheet"
| "application/vnd.ces-quickword"
- | "application/vnd.ms-excel"
- | "application/vnd.ms-excel.sheet.macroenabled.12"
- | "application/vnd.ms-powerpoint"
- | "application/vnd.ms-powerpoint.presentation.macroenabled.12"
- | "application/vnd.ms-word"
- | "application/vnd.ms-word.document.12"
- | "application/vnd.ms-word.document.macroenabled.12"
- | "application/vnd.msword"
- | "application/vnd.openxmlformats-officedocument.presentationml.presentation"
- | "application/vnd.openxmlformats-officedocument.presentationml.template"
- | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
- | "application/vnd.openxmlformats-officedocument.spreadsheetml.template"
- | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
- | "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
- | "application/vnd.presentation-openxml"
- | "application/vnd.presentation-openxmlm"
- | "application/vnd.spreadsheet-openxml"
- | "application/vnd.wordprocessing-openxml"
)
}
diff --git a/moli-web-mime/src/tests.rs b/moli-web-mime/src/tests.rs
index fc46ea958..9e1b8fbc9 100644
--- a/moli-web-mime/src/tests.rs
+++ b/moli-web-mime/src/tests.rs
@@ -188,6 +188,10 @@ fn classifies_binary_main_document_headers() {
"Content-Type".to_owned(),
b"application/pdf; charset=binary".to_vec(),
)]));
+ assert!(response_headers_indicate_raw_document(&[(
+ "Content-Type".to_owned(),
+ b"application/vnd.openxmlformats-officedocument.wordprocessingml.document".to_vec(),
+ )]));
assert!(response_headers_indicate_raw_document(&[(
"Content-Type".to_owned(),
b"image/png".to_vec(),
@@ -237,6 +241,12 @@ fn classifies_supported_lightweight_document_mime_types() {
"application/pdf",
"application/zip",
"application/wasm",
+ "application/msword",
+ "application/vnd.ms-excel",
+ "application/vnd.ms-powerpoint",
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"font/woff2",
] {
assert!(
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index d1e058993..c8b69b0e0 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -340,6 +340,7 @@ struct CacheableFixtureServer {
const PDF_FIXTURE_BODY: &[u8] =
b"%PDF-1.7\n% moli raw document fixture\n1 0 obj\n<<>>\nendobj\n%%EOF\n";
+const DOCX_FIXTURE_BODY: &[u8] = b"PK\x03\x04moli-docx-fixture";
const VIDEO_FIXTURE_BODY: &[u8] = b"\0\0\0\x18ftypmp42\xff\0moli-video-fixture";
const PNG_FIXTURE_BODY: &[u8] = &[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
@@ -586,6 +587,30 @@ impl BinaryDocumentFixtureServer {
)
}
+ async fn inline_docx() -> impl IntoResponse {
+ (
+ [(
+ "content-type",
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ )],
+ DOCX_FIXTURE_BODY.to_vec(),
+ )
+ }
+
+ async fn html_document() -> impl IntoResponse {
+ (
+ [("content-type", "text/html; charset=utf-8")],
+ "<!doctype html><main><h1>HTML body remains renderable</h1></main>",
+ )
+ }
+
+ async fn text_document() -> impl IntoResponse {
+ (
+ [("content-type", "text/plain; charset=utf-8")],
+ "Plain text remains renderable",
+ )
+ }
+
async fn attachment_pdf() -> impl IntoResponse {
(
[
@@ -611,6 +636,9 @@ impl BinaryDocumentFixtureServer {
let addr = listener.local_addr()?;
let app = Router::new()
.route("/inline.pdf", get(inline_pdf))
+ .route("/inline.docx", get(inline_docx))
+ .route("/page.html", get(html_document))
+ .route("/notes.txt", get(text_document))
.route("/clip.mp4", get(inline_video))
.route("/pixel.png", get(inline_png))
.route("/attachment.html", get(attachment_pdf));
@@ -1181,6 +1209,39 @@ fn cli_fetch_without_dump_writes_pdf_verbatim() -> Result<()> {
Ok(())
}
+#[test]
+fn cli_dump_markdown_rejects_docx_but_keeps_text_documents_renderable() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(BinaryDocumentFixtureServer::spawn())?;
+
+ let docx_url = server.url("/inline.docx");
+ let docx = run_fetch_cli_with_dump_and_args(&docx_url, "markdown", &[])?;
+ assert!(!docx.status.success(), "stdout={:?}", docx.stdout);
+ assert!(docx.stdout.is_empty(), "stdout={:?}", docx.stdout);
+ assert_single_fetch_failure_reason(
+ &clean_output(&docx.stderr),
+ &docx_url,
+ "raw download output only supports automatic output or --dump json",
+ );
+
+ for (path, expected) in [
+ ("/page.html", "HTML body remains renderable"),
+ ("/notes.txt", "Plain text remains renderable"),
+ ] {
+ let url = server.url(path);
+ let output = run_fetch_cli_with_dump_and_args(&url, "markdown", &[])?;
+ assert!(
+ output.status.success(),
+ "path={path} stderr={}",
+ clean_output(&output.stderr)
+ );
+ assert!(clean_output(&output.stdout).contains(expected), "path={path}");
+ }
+
+ runtime.block_on(server.shutdown());
+ Ok(())
+}
+
#[test]
fn cli_fetch_without_dump_writes_video_verbatim() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;