Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfs-interaction-policy): classify %PDF- inside text as text and accept GFS names

Alfredo Lopezb596102947bc
diff --git a/packages/gfs-interaction-policy/fileClassifier.cjs b/packages/gfs-interaction-policy/fileClassifier.cjs
index 3fafc445e..e37249a99 100644
--- a/packages/gfs-interaction-policy/fileClassifier.cjs
+++ b/packages/gfs-interaction-policy/fileClassifier.cjs
@@ -359,7 +359,10 @@ function textClassFromContent(text) {
/**
* Inside the text family the content decides svg and html; otherwise the
- * declared class breaks the tie. SVG is never granted by the name alone.
+ * declared class breaks the tie, except svg, which this function returns only
+ * for an svg root. classifyBytes can still grant a declared svg without
+ * content evidence: when the prefix of a longer file is empty, or is short text
+ * without a signature class, the declared class is returned as `declared`.
*/
function textClassFor(text, expectedClass) {
const fromContent = textClassFromContent(text)
@@ -415,11 +418,14 @@ function classifyFromEvidence(bytes, partial, expectedClass) {
if (officeClass) return { fileClass: officeClass, detection: 'magic' }
return { fileClass: 'binary_unsupported', detection: 'magic', zip: true }
}
- if (hasPdfSignature(bytes)) return { fileClass: 'pdf', detection: 'magic' }
+ if (startsWith(bytes, PDF_SIGNATURE)) return { fileClass: 'pdf', detection: 'magic' }
const text = decodeStrictText(bytes, partial)
if (text !== null) {
return { fileClass: textClassFor(text, expectedClass), detection: 'text_utf8', text: true }
}
+ // A signature after offset 0 counts only for bytes that are not text, so a
+ // text file that mentions %PDF- stays text.
+ if (hasPdfSignature(bytes)) return { fileClass: 'pdf', detection: 'magic' }
return { fileClass: 'binary_unsupported', detection: 'magic' }
}
diff --git a/packages/gfs-interaction-policy/fileClassifier.test.cjs b/packages/gfs-interaction-policy/fileClassifier.test.cjs
index 6018e7ab4..00f4b2f60 100644
--- a/packages/gfs-interaction-policy/fileClassifier.test.cjs
+++ b/packages/gfs-interaction-policy/fileClassifier.test.cjs
@@ -6,7 +6,7 @@ const path = require('node:path')
const { describe, it } = require('node:test')
const { classifyBytes, FILE_CLASSES, MEDIA_TYPE_BY_CLASS } = require('./fileClassifier.cjs')
-const EXPECTED_VECTOR_COUNT = 37
+const EXPECTED_VECTOR_COUNT = 38
const { vectors } = JSON.parse(
fs.readFileSync(path.join(__dirname, 'fixtures', 'classifier-vectors.v1.json'), 'utf8')
)
diff --git a/packages/gfs-interaction-policy/fileReference.cjs b/packages/gfs-interaction-policy/fileReference.cjs
index 6df32c806..006fee11f 100644
--- a/packages/gfs-interaction-policy/fileReference.cjs
+++ b/packages/gfs-interaction-policy/fileReference.cjs
@@ -60,9 +60,11 @@ function nameProblem(name) {
return `name must be at most ${FILE_REFERENCE_NAME_MAX_CODE_POINTS} code points`
}
if (name === '.' || name === '..') return 'name must not be a relative path segment'
+ // Same character rule as the GFS controller's resource names: `/` and
+ // control characters are rejected, a backslash is an ordinary character.
// eslint-disable-next-line no-control-regex
- if (/[/\\\u0000-\u001f\u007f]/.test(name)) {
- return 'name must not contain path separators or control characters'
+ if (/[/\u0000-\u001f\u007f]/.test(name)) {
+ return 'name must not contain "/" or control characters'
}
return null
}
diff --git a/packages/gfs-interaction-policy/fileReference.d.ts b/packages/gfs-interaction-policy/fileReference.d.ts
index ba9ece5e0..38f8a67a1 100644
--- a/packages/gfs-interaction-policy/fileReference.d.ts
+++ b/packages/gfs-interaction-policy/fileReference.d.ts
@@ -18,7 +18,7 @@ export interface FileReferenceV1 {
*/
id: string
source: FileReferenceSource
- /** Base name, NFC, 1-255 code points, no path separators or control characters. */
+ /** Base name, NFC, 1-255 code points, no `/` or control characters. Builders normalize to NFC. */
name: string
declaredMediaType: string | null
detectedMediaType: string
diff --git a/packages/gfs-interaction-policy/fileReference.test.cjs b/packages/gfs-interaction-policy/fileReference.test.cjs
index 0060d3d64..c1bc0be82 100644
--- a/packages/gfs-interaction-policy/fileReference.test.cjs
+++ b/packages/gfs-interaction-policy/fileReference.test.cjs
@@ -144,4 +144,55 @@ describe('builders', () => {
assert.equal(built.value.detection, 'declared')
assert.equal(built.value.digest, undefined)
})
+
+ it('normalizes an NFD gfs name to NFC, which the parser then accepts', () => {
+ const nfd = 'Informe de producción.md'
+ assert.notEqual(nfd, nfd.normalize('NFC'))
+ const built = buildGfsFileReference({
+ drive: 'personal',
+ resourceId: 'res-43',
+ gfsUri: 'gfs://personal/Informe.md',
+ version: 1,
+ name: nfd,
+ declaredMediaType: 'text/markdown',
+ byteLength: markdown.length,
+ classification,
+ })
+ assert.equal(built.ok, true, built.ok ? '' : built.message)
+ assert.equal(built.value.name, 'Informe de producción.md')
+ assert.deepEqual(parseFileReferenceV1(built.value), built)
+ const parsedNfd = parseFileReferenceV1({ ...built.value, name: nfd })
+ assert.equal(parsedNfd.ok, false)
+ assert.equal(parsedNfd.message, 'name must be NFC-normalized')
+ })
+
+ it('accepts a backslash in a name, as GFS resource names do', () => {
+ const built = buildAttachmentFileReference({
+ attachmentId: 'att-1',
+ messageId: 'msg-1',
+ name: 'a\\b.txt',
+ byteLength: markdown.length,
+ digestHex: HEX,
+ classification,
+ })
+ assert.equal(built.ok, true, built.ok ? '' : built.message)
+ assert.equal(built.value.name, 'a\\b.txt')
+ assert.equal(parseFileReferenceV1(built.value).ok, true)
+ })
+
+ it('still rejects a slash and control characters in a name', () => {
+ for (const name of ['a/b.txt', 'a\u0000b.txt', 'a\u001fb.txt', 'a\u007fb.txt']) {
+ const built = buildAttachmentFileReference({
+ attachmentId: 'att-1',
+ messageId: 'msg-1',
+ name,
+ byteLength: markdown.length,
+ digestHex: HEX,
+ classification,
+ })
+ assert.equal(built.ok, false, JSON.stringify(name))
+ assert.equal(built.code, 'FILE_REFERENCE_INVALID')
+ assert.equal(built.message, 'name must not contain "/" or control characters')
+ }
+ })
})
diff --git a/packages/gfs-interaction-policy/fixtures/classifier-vectors.v1.json b/packages/gfs-interaction-policy/fixtures/classifier-vectors.v1.json
index e75bc1013..9930744be 100644
--- a/packages/gfs-interaction-policy/fixtures/classifier-vectors.v1.json
+++ b/packages/gfs-interaction-policy/fixtures/classifier-vectors.v1.json
@@ -76,7 +76,7 @@
{
"name": "pdf signature after leading bytes",
"input": {
- "bytesBase64": "ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICVQREYtMS43CjEgMCBvYmogPDwgL1R5cGUgL0NhdGFsb2cgPj4gZW5kb2JqCiUlRU9GCg==",
+ "bytesBase64": "//79/CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICVQREYtMS43CjEgMCBvYmogPDwgL1R5cGUgL0NhdGFsb2cgPj4gZW5kb2JqCiUlRU9GCg==",
"totalByteLength": 151,
"declaredMediaType": null,
"filename": "doc.pdf"
@@ -91,6 +91,24 @@
"mismatch": false
}
},
+ {
+ "name": "text mentioning %PDF- after offset 0",
+ "input": {
+ "bytesBase64": "QSBQREYgZmlsZSBzdGFydHMgd2l0aCAlUERGLTEuNyBhbmQgZW5kcyB3aXRoICUlRU9GLgo=",
+ "totalByteLength": 53,
+ "declaredMediaType": "text/plain",
+ "filename": "notes.txt"
+ },
+ "expected": {
+ "class": "text",
+ "detectedMediaType": "text/plain",
+ "detection": "text_utf8",
+ "textReadable": true,
+ "reader": "text",
+ "modelImageInput": "unsupported",
+ "mismatch": false
+ }
+ },
{
"name": "pdf bytes named .txt",
"input": {
diff --git a/packages/gfs-interaction-policy/fixtures/file-reference-vectors.v1.json b/packages/gfs-interaction-policy/fixtures/file-reference-vectors.v1.json
index 911951cd1..d4c922cc8 100644
--- a/packages/gfs-interaction-policy/fixtures/file-reference-vectors.v1.json
+++ b/packages/gfs-interaction-policy/fixtures/file-reference-vectors.v1.json
@@ -472,8 +472,7 @@
"modelImageInput": "unsupported"
},
"expected": {
- "ok": false,
- "code": "FILE_REFERENCE_INVALID"
+ "ok": true
}
},
{
diff --git a/packages/gfs-interaction-policy/scripts/generate-vectors.cjs b/packages/gfs-interaction-policy/scripts/generate-vectors.cjs
index b2d7727dc..ee2062a17 100644
--- a/packages/gfs-interaction-policy/scripts/generate-vectors.cjs
+++ b/packages/gfs-interaction-policy/scripts/generate-vectors.cjs
@@ -117,11 +117,18 @@ const classifier = [
v('pdf by signature', PDF, 'report.pdf', 'application/pdf', T('pdf', 'magic', false)),
v(
'pdf signature after leading bytes',
- b(' '.repeat(100), PDF),
+ b([0xff, 0xfe, 0xfd, 0xfc], ' '.repeat(96), PDF),
'doc.pdf',
null,
T('pdf', 'magic', false)
),
+ v(
+ 'text mentioning %PDF- after offset 0',
+ b('A PDF file starts with %PDF-1.7 and ends with %%EOF.\n'),
+ 'notes.txt',
+ 'text/plain',
+ T('text', 'text_utf8', false)
+ ),
v('pdf bytes named .txt', PDF, 'notes.txt', 'text/plain', T('pdf', 'magic', true)),
v('docx by zip entries', DOCX, 'letter.docx', DOCX_MIME, T('docx', 'magic', false)),
v('xlsx by zip entries', XLSX, 'sheet.xlsx', XLSX_MIME, T('xlsx', 'magic', false)),
@@ -418,7 +425,7 @@ const references = [
invalid
),
r('name with a slash', { ...attachmentBase, name: 'dir/notes.md' }, invalid),
- r('name with a backslash', { ...attachmentBase, name: 'dir\\notes.md' }, invalid),
+ r('name with a backslash', { ...attachmentBase, name: 'dir\\notes.md' }, ok),
r('name ..', { ...attachmentBase, name: '..' }, invalid),
r('name with a control character', { ...attachmentBase, name: 'notes\u0007.md' }, invalid),
r('name not in NFC', { ...attachmentBase, name: 'café.md' }, invalid),