Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(autoindex): qualify the %PDF- magic signature (#403)

Vincenzo Lombardofe1486068f6e
diff --git a/engine/crates/xerj-autoindex/src/sniff.rs b/engine/crates/xerj-autoindex/src/sniff.rs
index da6309f4e..96aca24cb 100644
--- a/engine/crates/xerj-autoindex/src/sniff.rs
+++ b/engine/crates/xerj-autoindex/src/sniff.rs
@@ -189,7 +189,16 @@ fn sniff_bytes(
}
// 1. Magic bytes.
- if prefix.starts_with(b"%PDF-") {
+ //
+ // %PDF- stays here rather than in MAGIC_TABLE (#403's own fix sketch
+ // floated moving it): every MAGIC_TABLE row reports `Family::Binary` +
+ // a `binary_kind` string, but %PDF- has to report the distinct
+ // `Family::Pdf` the extractor routes on (lib.rs:1229) — widening the
+ // shared `MagicRow` type for the one row that needs a different Family
+ // is a bigger, riskier change than this bug needs. `pdf_header` gives it
+ // the same structural qualifier the table's own rows carry; see its doc
+ // comment for what it rejects.
+ if prefix.starts_with(b"%PDF-") && pdf_header(prefix) {
return Ok(mk(Family::Pdf));
}
if prefix.starts_with(b"SQLite format 3\0") {
@@ -940,6 +949,29 @@ fn id3v2_header(prefix: &[u8]) -> bool {
&& prefix[6..10].iter().all(|b| *b < 0x80)
}
+/// PDF header: `%PDF-M.N` — a single-digit major version, `.`, a single-digit
+/// minor version — immediately followed by an end-of-line marker, which the
+/// spec requires right after the version (ISO 32000-1 §7.5.2, "the header
+/// line shall be immediately followed by a comment... the header
+/// itself... terminated by an end-of-line marker").
+///
+/// Unqualified, this signature is five ASCII characters including `%` and
+/// `-`. Prose opening "%PDF- is the five byte header every PDF file begins
+/// with. " sniffed `Family::Pdf` and was handed to the PDF extractor, which
+/// cannot parse it (#403 — same class as #379/#380, filed separately: `%`
+/// and `-` shrink the collision surface a lot, and the wrong answer is a
+/// parse error rather than #379's silent junk reason). The qualifier is
+/// exactly what makes it safe: a sentence has a space (or nothing) after
+/// "1.7", never a CR/LF, and real generators always emit that break —
+/// Adobe's own spec calls it out as required, not conventional.
+fn pdf_header(prefix: &[u8]) -> bool {
+ prefix.len() >= 9
+ && prefix[5].is_ascii_digit()
+ && prefix[6] == b'.'
+ && prefix[7].is_ascii_digit()
+ && matches!(prefix[8], b'\r' | b'\n')
+}
+
/// Truevision TGA, which has no magic number at all — every byte of its
/// 18-byte header is data. Detection is therefore by CONSTRAINT: an image type
/// from the defined set, a pixel depth from the defined set, a colour-map
@@ -2899,15 +2931,10 @@ mod printable_magic_tests {
/// — they are early returns in `sniff_bytes` — so the walk above says
/// nothing about them. Two of the three are safe for the usual reason (a
/// byte text cannot contain: the NUL after `3`, and `\x03\x04`), and this
- /// pins that.
- ///
- /// `%PDF-` is NOT safe and is the same class as #379/#380: prose opening
- /// `%PDF-` sniffs `Family::Pdf` and is handed to the PDF extractor.
- /// Measured on this branch and filed as #403 rather than widened into this
- /// fix — five characters including `%` and `-` is a far smaller collision
- /// surface than `BM`, and it does not produce #379's silent "binary
- /// content" junk reason. The wrong answer is asserted here on purpose so
- /// #403 has to come to this file and flip it.
+ /// pins that. `%PDF-` needed its own structural qualifier (`pdf_header`,
+ /// #403 — same class as #379/#380) since all five of its bytes are
+ /// printable ASCII; this pins that prose opening with the bare signature
+ /// now reads `TxtProse`, not `Pdf`.
#[test]
fn signatures_matched_before_the_table() {
for (label, opener, want) in [
@@ -2921,11 +2948,10 @@ mod printable_magic_tests {
"PK is the local file header signature of the ZIP format. ",
Family::TxtProse,
),
- // #403: this one is wrong. It must read TxtProse when #403 lands.
(
"pdf",
"%PDF- is the five byte header every PDF file begins with. ",
- Family::Pdf,
+ Family::TxtProse,
),
] {
let text = opener.repeat(30);
@@ -2939,6 +2965,31 @@ mod printable_magic_tests {
);
}
}
+
+ /// The positive control `every_printable_signature_carries_a_qualifier`
+ /// cannot provide for `%PDF-`, since it walks `MAGIC_TABLE` only: a real
+ /// PDF header must still classify `Family::Pdf`, or `pdf_header` is not a
+ /// qualifier but a second way to break the format.
+ #[test]
+ fn a_real_pdf_header_still_classifies_as_pdf() {
+ let headers: [&[u8]; 3] = [
+ b"%PDF-1.7\n%\xE2\xE3\xCF\xD3\n1 0 obj\n",
+ b"%PDF-1.4\r\n1 0 obj\n",
+ b"%PDF-2.0\r%comment\r",
+ ];
+ for header in headers {
+ let p = Path::new("doc.pdf");
+ let sn = sniff_bytes(header, p, p, false).unwrap();
+ assert_eq!(
+ sn.family,
+ Family::Pdf,
+ "real PDF header {header:?} classified {:?}/{:?} — pdf_header \
+ rejected a spec-conformant file",
+ sn.family,
+ sn.binary_kind
+ );
+ }
+ }
}
#[cfg(test)]