From b7a9fa1c21c75ce20d823c72e3e08ac3fbe0e063 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 13:04:34 -0800 Subject: [PATCH] Keep more private text from place suggestions, and name houses by their street MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Not sent any more: "john @Gmail.com" (a capital after "@"), "030 12345" and "Alice 12345-6789" (read as a house number and postcode), "access_token=abcdef", a key in quotes or brackets, and letter codes after a code label ("OTP: ABCDEF", "verification code ABCDEF"). - Sent now: "10 Downing Street SW1A2AA, London" (the comma hid the postcode). "Pin Oak" and "Gate Code Lane" stay places. - A house Photon returns without a name is named by its number and street, so 10 and 12 Downing Street are two suggestions, not one "London". - The same text asked twice at once is sent once. - The village pack's build finds twins near the poles and across 180°; the shipped tiles are unchanged. Co-authored-by: Manjunath Paparaju --- scripts/wikidataPlaceTiles.mjs | 25 ++++++++--- src/search/placeSuggestions.js | 49 ++++++++++++++++++--- src/search/placeSuggestions.test.mjs | 58 +++++++++++++++++++++++++ src/tooling/wikidataPlaceTiles.test.mjs | 15 +++++++ 4 files changed, 134 insertions(+), 13 deletions(-) diff --git a/scripts/wikidataPlaceTiles.mjs b/scripts/wikidataPlaceTiles.mjs index b874734472..15f0c89e3a 100644 --- a/scripts/wikidataPlaceTiles.mjs +++ b/scripts/wikidataPlaceTiles.mjs @@ -100,22 +100,35 @@ const kmBetween = (a, b) => * @param {Array} rows Largest first. */ export function withoutNearTwins(rows) { - // Cells of 0.05 degrees (about 5 km), searched with their neighbours. - const cell = (lat, lon) => `${Math.floor(lat * 20)}_${Math.floor(lon * 20)}`; + // Cells of 0.05 degrees (about 5 km of latitude), searched with their + // neighbours. A cell narrows toward the poles, so as many longitude cells + // are searched as TWIN_KM spans there, wrapping at the antimeridian. + const CELLS = 20; + const AROUND = 360 * CELLS; const kept = new Map(); return rows.filter((row) => { const name = foldName(row[0]); - const [la, lo] = cell(row[1], row[2]).split('_').map(Number); + const la = Math.floor(row[1] * CELLS); + const lo = Math.floor(row[2] * CELLS); + const cos = Math.cos( + (Math.min(89.9, Math.abs(row[1]) + 0.1) * Math.PI) / 180, + ); + const span = Math.min( + AROUND / 2, + Math.ceil((TWIN_KM / (111.32 * cos)) * CELLS) + 1, + ); for (let i = -1; i <= 1; i++) - for (let j = -1; j <= 1; j++) - for (const other of kept.get(`${la + i}_${lo + j}`) || []) + for (let j = -span; j <= span; j++) + for (const other of kept.get( + `${la + i}_${(((lo + j) % AROUND) + AROUND) % AROUND}`, + ) || []) if ( other.name === name && other.row[4] === row[4] && kmBetween(other.row, row) <= TWIN_KM ) return false; - const key = `${la}_${lo}`; + const key = `${la}_${((lo % AROUND) + AROUND) % AROUND}`; if (!kept.has(key)) kept.set(key, []); kept.get(key).push({ name, row }); return true; diff --git a/src/search/placeSuggestions.js b/src/search/placeSuggestions.js index e552fbd97f..99a68b62ce 100644 --- a/src/search/placeSuggestions.js +++ b/src/search/placeSuggestions.js @@ -108,7 +108,7 @@ function privacyReason(text) { // Chandambylu"). // Also while it is typed ("john.smith@") or spaced ("john @ gmail.com"). if ( - /[^\s@]@[^\s@]|(?:^|\s)@[a-z0-9_]{2,}(?![\p{L}\p{N}])|[^\s@]@(?:\s|$)|\S\s+@\s+[\p{L}\p{N}-]{2,}\.\p{L}{2,}/u.test( + /[^\s@]@[^\s@]|(?:^|\s)@[a-z0-9_]{2,}(?![\p{L}\p{N}])|[^\s@]@(?:\s|$)|\S\s+@\s+[\p{L}\p{N}-]{2,}\.\p{L}{2,}/iu.test( line, ) ) @@ -138,8 +138,9 @@ function privacyReason(text) { ? line : line .replace( - /(^|\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)/giu, - '$1 ', + // A street word before ("030 12345" is a phone number). + /(?<=\p{L}\.?\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)/giu, + ' ', ) // Other postcodes after a street's house number and before the city (a // four-digit one after a comma, so "is 555 0100 thanks" stays a number): @@ -150,7 +151,9 @@ function privacyReason(text) { /(?<=\p{L}\.?\s)\d{1,4}[a-z]?(?:,\s*\d{4}(?:-\d{3})?|,?\s+(?:\d{2}-\d{3}|\d{3} \d{2}|\d{6}))(?=\s+\p{L})/giu, ' x ', ) - .replace(/(?<=\p{L}\.?,?\s)\d{5}-\d{4}\b/gu, ' x ') + // ZIP+4 after a state's two letters ("DC 20500-0003"), not after + // any word ("Alice 12345-6789"). + .replace(/(?<=(?:^|\s)\p{L}{2},?\s)\d{5}-\d{4}\b/gu, ' x ') .replace(/\b\d{5}-\d{3}\b/g, ' x ') .replace( /〒\s?\d{3}-\d{4}|\b\d{3}-\d{4}(?=\s*[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}])/gu, @@ -173,7 +176,7 @@ function privacyReason(text) { /(? token.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '')) .some( (token) => /^(?:sk[-_]|rk_|pk_live|ghp_|xox[abposr]-)/i.test(token) || @@ -201,6 +212,21 @@ function privacyReason(text) { return longNumber ? 'number' : null; } +/** + * A code of letters after a label that can only be a code, without a colon + * ("verification code ABCDEF", "otp 7GH2K"): written in capitals or holding + * a digit, so "Gate Code Lane" stays a street. + */ +function codeAfterLabel(line) { + const match = + /\b(?:otp|2fa|mfa|(?:door|access|gate|alarm|entry|security|verification|one-time|auth|login) code)(?:\s+is)?\s*[:=#]?\s*([a-z0-9]{4,})\b/i.exec( + line, + ); + if (!match) return false; + const value = match[1]; + return /\d/.test(value) || value === value.toUpperCase(); +} + /** * A word that may be a password or key, with letters and digits mixed * within one run: eight characters or more, four letters or more with a digit ("hunter22", @@ -282,6 +308,8 @@ export function createPlaceSuggester({ } } signal?.throwIfAborted(); + // The same text asked again while this one waited is answered now. + if (cache.has(key)) return cache.get(key); if (Date.now() < shared.pausedUntil) return []; const url = new URL(photonSearchUrl(text, { endpoint, limit })); if (['en', 'de', 'fr', 'it'].includes(language)) @@ -323,8 +351,15 @@ export function createPlaceSuggester({ countrycode: countryCode, osm_value: osmType, type, + street, + housenumber: houseNumber, } = feature.properties ?? {}; - const name = normalized.name || city || state || country; + // A house has no name of its own: its street and number are its + // name, so two houses on one street stay two places. + const address = street + ? [houseNumber, street].filter(Boolean).join(' ') + : ''; + const name = normalized.name || address || city || state || country; if (!name) continue; const detail = [...new Set([city, state, country])] .filter((part) => part && part !== name) diff --git a/src/search/placeSuggestions.test.mjs b/src/search/placeSuggestions.test.mjs index f5e2ac6f51..ca08fe6744 100644 --- a/src/search/placeSuggestions.test.mjs +++ b/src/search/placeSuggestions.test.mjs @@ -327,3 +327,61 @@ test('a UK postcode without its space is a place', () => { for (const line of ['EC1A1BB', 'W1A0AX', 'BT487NN']) assert.equal(mayBePrivate(line), false, line); }); + +test('emails, keys and codes in other shapes stay private', () => { + for (const line of [ + 'john @Gmail.com', + 'John @EXAMPLE.COM', + '030 12345', + 'Alice 12345-6789', + 'access_token=abcdef', + 'secret_key=abcdef', + '"sk-abcdefghijklmnop"', + '(ghp_abcdef)', + 'OTP: ABCDEF', + 'verification code ABCDEF', + 'PIN: A7B2', + ]) + assert.equal(mayBePrivate(line), true, line); + for (const line of [ + '10 Downing Street SW1A2AA, London', + 'Washington DC 20500-0003', + 'Pin Oak', + 'Gate Code Lane', + ]) + assert.equal(mayBePrivate(line), false, line); +}); + +test('two houses on one street are two suggestions, asked once', async () => { + let sent = 0; + const house = (number, lon) => ({ + geometry: { type: 'Point', coordinates: [lon, 51.5034] }, + properties: { + street: 'Downing Street', + housenumber: number, + city: 'London', + country: 'United Kingdom', + countrycode: 'gb', + osm_value: 'house', + }, + }); + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async () => { + sent++; + return Response.json({ + features: [house('10', -0.1276), house('12', -0.1277)], + }); + }, + }); + const [first, second] = await Promise.all([ + service.suggest('Downing Street'), + service.suggest('Downing Street'), + ]); + assert.deepEqual( + first.map((place) => place.name), + ['10 Downing Street', '12 Downing Street'], + ); + assert.deepEqual(second, first); + assert.equal(sent, 1); +}); diff --git a/src/tooling/wikidataPlaceTiles.test.mjs b/src/tooling/wikidataPlaceTiles.test.mjs index 007c15ed15..9e9cf54179 100644 --- a/src/tooling/wikidataPlaceTiles.test.mjs +++ b/src/tooling/wikidataPlaceTiles.test.mjs @@ -88,3 +88,18 @@ test('one place listed twice nearby keeps its larger listing', () => { ['Q1', 'Q3', 'Q4', 'Q5'], ); }); + +test('twins are found where longitude cells narrow, and across 180°', () => { + const rows = [ + // 1.98 km apart at 70° N, two longitude cells apart. + ['Nord', 70, 10.049, 9000, 'NO', 'Q1'], + ['Nord', 70, 10.101, 2000, 'NO', 'Q2'], + // Either side of the antimeridian, under 2 km apart. + ['Kai', -16.5, 179.995, 5000, 'FJ', 'Q3'], + ['Kai', -16.5, -179.995, 1000, 'FJ', 'Q4'], + ]; + assert.deepEqual( + withoutNearTwins(rows).map((row) => row[5]), + ['Q1', 'Q3'], + ); +});