Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(search): a lone-surrogate entity in a hit title corrupted every result (#1689)

tashdroid3d56c5d388f9
diff --git a/server/src/__tests__/services/search.test.ts b/server/src/__tests__/services/search.test.ts
index ca2d2491..4a2fc0db 100644
--- a/server/src/__tests__/services/search.test.ts
+++ b/server/src/__tests__/services/search.test.ts
@@ -484,4 +484,16 @@ describe('decodeEntities', () => {
expect(decodeEntities('&bogus; stays')).toBe('&bogus; stays');
expect(decodeEntities('plain')).toBe('plain');
});
+ it('leaves lone-surrogate entities verbatim instead of corrupting the string', () => {
+ // Obfuscated-email bait on scraped pages renders as a lone surrogate
+ // entity. String.fromCodePoint happily "decodes" it, producing a string
+ // that is not valid UTF-16: JSON.stringify turns it into U+FFFD escapes
+ // and strict encoders/serialisers choke on it. The entity must survive
+ // untouched, exactly like the out-of-range case (�).
+ expect(decodeEntities('contact � us')).toBe('contact � us');
+ expect(decodeEntities('tail � end')).toBe('tail � end');
+ expect(decodeEntities('�?')).toBe('\uFFFD?'); // real replacement char still decodes
+ expect(decodeEntities('� too big')).toBe('� too big');
+ expect(decodeEntities('😀 emoji')).toBe('\u{1F600} emoji');
+ });
});
diff --git a/server/src/services/search.ts b/server/src/services/search.ts
index 21de6e62..237180e9 100644
--- a/server/src/services/search.ts
+++ b/server/src/services/search.ts
@@ -232,7 +232,19 @@ export function decodeEntities(v: string): string {
return v.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e: string) => {
if (e[0] === '#') {
const code = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
- return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : m;
+ // Lone surrogates (0xD800–0xDFFF) are in the 0..0x10FFFF range but are
+ // not valid Unicode scalar values: fromCodePoint hands back a broken
+ // string whose half gets replaced with U+FFFD by every JSON encoder
+ // and rejected outright by strict ones. Web pages carry them as bait
+ // (obfuscated-email addresses render as &#xD800;-style entities), and
+ // this decoder runs on every hit title and snippet from every engine.
+ // Same guard as safeCodePoint in routes/update.ts, which caught this
+ // exact class in the update-check feed. Leave the entity verbatim —
+ // it is display text, not a hard parse dependency.
+ const isLoneSurrogate = code >= 0xd800 && code <= 0xdfff;
+ return Number.isFinite(code) && code > 0 && code <= 0x10ffff && !isLoneSurrogate
+ ? String.fromCodePoint(code)
+ : m;
}
return NAMED_ENTITIES[e.toLowerCase()] ?? m;
});