Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(documents): parse MIME charset parameters independently

Afloate504a7b8f227
diff --git a/apps/node-services/src/document-normalizer/README.md b/apps/node-services/src/document-normalizer/README.md
new file mode 100644
index 00000000..3fde80c1
--- /dev/null
+++ b/apps/node-services/src/document-normalizer/README.md
@@ -0,0 +1,13 @@
+# Document source normalization
+
+`normalizeDocumentContent` converts supported local source bytes to canonical
+Markdown. Source selection uses a recognized MIME essence first, then the
+filename and representation format. Generic or unspecified media types retain
+those fallbacks.
+
+Text sources accept MIME metadata such as CSV's `header=present` independently
+of the character encoding. Parameters use MIME quoted-string parsing, so
+`charset="UTF-8"` and semicolons inside quoted values are supported. An explicit
+charset must be `utf-8` or `utf8`, case-insensitively; other declared encodings
+are unsupported. Regardless of metadata, text bytes must pass strict UTF-8
+decoding before format-specific conversion.
diff --git a/apps/node-services/src/document-normalizer/document-source-kind.ts b/apps/node-services/src/document-normalizer/document-source-kind.ts
index eedfd4d6..50ea2f2f 100644
--- a/apps/node-services/src/document-normalizer/document-source-kind.ts
+++ b/apps/node-services/src/document-normalizer/document-source-kind.ts
@@ -1,3 +1,5 @@
+import { MIMEType } from "node:util";
+
import {
DocumentNormalizationError,
type DocumentNormalizationInput,
@@ -243,21 +245,21 @@ export function resolveDocumentSourceKind(
const officeKind = zipDocumentKind(input);
if (officeKind) return officeKind;
}
- assertCompatibleCharset(fromMedia, parsed.parameters);
+ assertCompatibleCharset(fromMedia, parsed.charset);
return fromMedia;
}
const fromName = kindFromFilename(input.filename);
if (fromName) {
- assertCompatibleCharset(fromName, parsed.parameters);
+ assertCompatibleCharset(fromName, parsed.charset);
return fromName;
}
const fromFormat = kindFromRepresentation(input.representationFormat);
if (fromFormat) {
- assertCompatibleCharset(fromFormat, parsed.parameters);
+ assertCompatibleCharset(fromFormat, parsed.charset);
return fromFormat;
}
if (parsed.essence.startsWith("text/")) {
- assertCompatibleCharset("plain", parsed.parameters);
+ assertCompatibleCharset("plain", parsed.charset);
return "plain";
}
return "unsupported";
@@ -321,25 +323,29 @@ function kindFromRepresentation(
function parseMediaType(value: string): {
essence: string;
- parameters: string[];
+ charset: string | null;
} {
- const parts = value
- .split(";")
- .map((part) => part.trim().toLocaleLowerCase("en-US"))
- .filter((part) => part.length > 0);
- return { essence: parts.shift() ?? "", parameters: parts };
+ const separator = value.indexOf(";");
+ const essence = (separator < 0 ? value : value.slice(0, separator))
+ .trim()
+ .toLocaleLowerCase("en-US");
+ // Parse parameters independently so an absent or generic essence can still
+ // fall back to the filename or representation format.
+ const parameters = separator < 0 ? "" : value.slice(separator);
+ const charset = new MIMEType(
+ `application/octet-stream${parameters}`,
+ ).params.get("charset");
+ return { essence, charset };
}
function assertCompatibleCharset(
kind: DocumentSourceKind,
- parameters: readonly string[],
+ charset: string | null,
): void {
if (!IN_PROCESS_DOCUMENT_SOURCE_KINDS.has(kind)) return;
if (
- parameters.some(
- (parameter) =>
- parameter !== "charset=utf-8" && parameter !== "charset=utf8",
- )
+ charset !== null &&
+ !["utf-8", "utf8"].includes(charset.toLocaleLowerCase("en-US"))
) {
throw new DocumentNormalizationError("DOCUMENT_SOURCE_UNSUPPORTED");
}
diff --git a/apps/node-services/tests/document-normalizer/document-normalizer.test.ts b/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
index 7724d4b0..b1e09d23 100644
--- a/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
+++ b/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
@@ -123,6 +123,45 @@ describe("document normalization", () => {
);
});
+ it.each([
+ 'text/csv; header=present; charset="UTF-8"',
+ 'application/octet-stream; charset="utf8"',
+ 'application/vnd.example.table; profile="https://example.test/schema; charset=windows-1252"; charset=utf-8',
+ ])("normalizes UTF-8 CSV with MIME parameters: %s", async (mediaType) => {
+ const input = {
+ filename: "people.csv",
+ mediaType,
+ source: encode("name,count\nCaf\u00e9,42\n"),
+ };
+ expect(isInProcessDocumentSource(input)).toBe(true);
+ await expect(normalizeDocumentContent(input).then(decode)).resolves.toBe(
+ "| name | count |\n| --- | --- |\n| Caf\u00e9 | 42 |\n",
+ );
+ });
+
+ it.each([
+ 'text/csv; header=present; charset="windows-1252"',
+ 'application/octet-stream; charset="UTF-16"',
+ ])("still rejects unsupported declared encodings: %s", async (mediaType) => {
+ await expect(
+ normalizeDocumentContent({
+ filename: "people.csv",
+ mediaType,
+ source: encode("name,count\nAda,42\n"),
+ }),
+ ).rejects.toMatchObject({ code: "DOCUMENT_SOURCE_UNSUPPORTED" });
+ });
+
+ it("still validates text bytes when an accepted charset is quoted", async () => {
+ await expect(
+ normalizeDocumentContent({
+ filename: "notes.txt",
+ mediaType: 'text/plain; charset="utf-8"',
+ source: new Uint8Array([0x63, 0x61, 0x66, 0xe9]),
+ }),
+ ).rejects.toMatchObject({ code: "DOCUMENT_SOURCE_INVALID" });
+ });
+
it("extracts archive and SVG text and reads no raster image", async () => {
await expect(
normalizeDocumentContent({