From bd7b389cef7a85fb489f43205b7bc41bdb3844fb Mon Sep 17 00:00:00 2001 From: Manjunath Paparaju Date: Wed, 18 Nov 2026 11:58:23 -0800 Subject: [PATCH 1/4] Suggest places from Photon while a place is typed The suggester asks Photon, the place search's keyless geocoder, for candidates as a place is typed: at most one request every 500 ms, a six second limit, and none for a minute after a refusal. Text that may be private (an email address, seven or more digits however separated, an IBAN shape, a password, PIN, token or key) is never sent. It also lists a region's bundled cities and tells whether a place lies in an extent, for the search box's place strip. The browser's Photon setting is read in one place. Co-authored-by: Sameh Khamis --- src/main.js | 4 +- src/search/photonSetting.js | 6 + src/search/placeSuggestions.js | 362 +++++++++++++++++++++++++++ src/search/placeSuggestions.test.mjs | 280 +++++++++++++++++++++ 4 files changed, 649 insertions(+), 3 deletions(-) create mode 100644 src/search/photonSetting.js create mode 100644 src/search/placeSuggestions.js create mode 100644 src/search/placeSuggestions.test.mjs diff --git a/src/main.js b/src/main.js index 545cc800fa..99de9cca06 100644 --- a/src/main.js +++ b/src/main.js @@ -1,9 +1,7 @@ import { createStandaloneApplication } from './standalone/application.js'; import { describeError } from './standalone/errors.js'; import { t } from './i18n/index.js'; - -// The Vite boundary exposes only undefined, null, or a validated URL. -const photonSetting = import.meta.env.PHOTON_URL; +import { PHOTON_SETTING as photonSetting } from './search/photonSetting.js'; const application = createStandaloneApplication({ googleApiKey: import.meta.env.GOOGLE_MAPS_API_KEY, diff --git a/src/search/photonSetting.js b/src/search/photonSetting.js new file mode 100644 index 0000000000..371e6fa9b7 --- /dev/null +++ b/src/search/photonSetting.js @@ -0,0 +1,6 @@ +/** + * The browser's Photon endpoint setting, read in one place. The Vite boundary + * (build/vite.js) exposes only undefined (use the public service), null (off, + * from an empty PHOTON_URL) or a validated https URL. + */ +export const PHOTON_SETTING = import.meta.env?.PHOTON_URL; diff --git a/src/search/placeSuggestions.js b/src/search/placeSuggestions.js new file mode 100644 index 0000000000..f00f847dab --- /dev/null +++ b/src/search/placeSuggestions.js @@ -0,0 +1,362 @@ +import { normalizePhotonFeature, photonSearchUrl } from '../keylessGeocoder.js'; +import { parseCoordinateQuery } from './coordinateParser.js'; +import { + requestWithDeadline, + waitForSignal, +} from '../sources/requestDeadline.js'; + +const MAX_QUERY_CHARS = 200; +const CJK = + /[\p{Script=Han}\p{Script=Hangul}\p{Script=Hiragana}\p{Script=Katakana}]/u; + +/** Han, Hangul or Kana text, whose place names are often two characters. */ +export const isCjk = (text) => CJK.test(String(text || '')); +/** The fewest characters a place name is looked up from. */ +export const minPlaceChars = (text) => (isCjk(text) ? 2 : 3); + +/** + * Text that may hold something private, which is never looked up, offered + * as a place or sent anywhere: + * - an "@" with something right on both sides (an email or a handle); + * - seven digits or more (in any script, full-width too) with nothing but + * spaces, punctuation, symbols or format characters between them + * (phone, card, SSN and account numbers, and their starts); + * - a phone number written with dots ("415.555.0100"); + * - two letters, two digits and ten letters or digits more (an IBAN's + * shape), spaces allowed; + * - "password", "passcode", "passwd", "ssn", "pw", "pwd" or "bearer" with + * anything after it, "token", "secret", "api key" or "key" with ":" or + * "=" after it, "api key", "access token" or "secret key" with a word of + * six characters or more after it, the word for "password" in the + * interface's languages with anything after it, or "pin" (also "pin + * code", "pin number", "pin is") with a digit after it; + * - a word starting "sk_", "sk-", "rk_", "pk_live", "ghp_" or a Slack token's "xoxb-" and the like (any + * case), or "AIza", "eyJ" or "AKIA"; + * - a word that may be a password or key (see mayBeSecret). + * The text is read after NFKC, so full-width letters and "@" count too. + * Text that parses as coordinates or a grid reference is a spot, not a + * secret. + */ +export function mayBePrivate(text) { + return privacyReason(text) !== null; +} + +/** + * Whether the only thing private-looking in a line is a long run of digits, + * as an address's house number and postcode can be ("Москва, ул. Тверская, + * д. 13, 125009"): such a line may be searched as a place when the user + * picks that on purpose. Never for an email, handle, password, key, code, + * IBAN or anything else. + * @param {string} text + */ +export function onlyNumbersLookPrivate(text) { + // Read as privacyReason reads it: "⁴¹¹¹", "①②", "card" and words split by + // zero-width spaces count as the digits and words they fold to. + const line = foldLine(text); + const groups = line.match(/\d+/g) || []; + return ( + privacyReason(text) === 'number' && + /\p{L}{2,}/u.test(line) && + // Address-sized: no group over six digits, nine digits at most in all + // (a house number and a postcode), and no word that says what a number + // is ("card 4111 …", "phone +1 415 …", "token 12345678"). + groups.every((group) => group.length <= 6) && + groups.join('').length <= 9 && + !NUMBER_CONTEXT.test(line) + ); +} + +/** + * Words that say a number is an account, a card, a phone or a secret, in the + * interface's languages where they differ ("Karte", "Telefon", "tarjeta"). + */ +const NUMBER_CONTEXT = + /(? + /^(?:sk[-_]|rk_|pk_live|ghp_|xox[abposr]-)/i.test(token) || + /^(?:AIza|eyJ|AKIA)/.test(token) || + mayBeSecret(token), + ) + ) + return 'other'; + return longNumber ? 'number' : null; +} + +/** + * 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", + * "letmein1"), or letters, digits and a symbol together ("Tr0ub4dor&3"). + * Callsigns (UAL2110, BAW15K, N123AB) and postcodes are shorter or have + * fewer letters. + */ +function mayBeSecret(token) { + // Letters and digits must mix within one run ("hunter22", "w0rd"): a + // number beside a word ("Yanino-1", "2-chōme", "656/G.B.", "Route-66") + // is a name. + // Latin letters only: CJK addresses are written without spaces + // ("東京都港区六本木6丁目", "北京市朝阳区建国路88号"). + const mixed = token + .split(/[^\p{L}\p{N}]+/u) + .some((run) => /\p{Script=Latin}/u.test(run) && /\d/.test(run)); + if (!mixed) return false; + const letters = (token.match(/\p{L}/gu) || []).length; + const digits = (token.match(/\d/g) || []).length; + if (!letters || !digits) return false; + if (token.length >= 8) return true; + if (token.length >= 6 && /[^\p{L}\p{N}'.,\-/]/u.test(token)) return true; + return token.length >= 6 && letters >= 4; +} +const MIN_GAP_MS = 500; +const TIMEOUT_MS = 6000; +const PAUSE_AFTER_REFUSAL_MS = 60_000; +const CACHE_LIMIT = 200; + +/** List Photon candidates while typing. The submitted search owns navigation. */ +export function createPlaceSuggester({ + endpoint, + fetchImpl = (...args) => globalThis.fetch(...args), + limit = 4, +} = {}) { + const enabled = endpoint === undefined || Boolean(endpoint); + const cache = new Map(); + let nextRequestAt = 0; + let pausedUntil = 0; + + return { + enabled, + async suggest(query, { signal } = {}) { + signal?.throwIfAborted(); + const text = String(query ?? '') + .trim() + .slice(0, MAX_QUERY_CHARS); + if (!enabled || text.length < minPlaceChars(text) || mayBePrivate(text)) + return []; + const language = (globalThis.document?.documentElement?.lang || 'en') + .slice(0, 2) + .toLowerCase(); + const key = `${language}|${text.toLowerCase()}`; + if (cache.has(key)) return cache.get(key); + + // Concurrent callers recheck the gap after waking before sending. + while (nextRequestAt > Date.now()) { + let timer; + const pause = new Promise((resolve) => { + timer = setTimeout(resolve, nextRequestAt - Date.now()); + }); + try { + await waitForSignal(pause, signal); + } finally { + clearTimeout(timer); + } + } + signal?.throwIfAborted(); + if (Date.now() < pausedUntil) return []; + const url = new URL(photonSearchUrl(text, { endpoint, limit })); + if (['en', 'de', 'fr', 'it'].includes(language)) + url.searchParams.set('lang', language); + + nextRequestAt = Date.now() + MIN_GAP_MS; + const features = await requestWithDeadline( + async (requestSignal) => { + const response = await fetchImpl(url, { signal: requestSignal }); + requestSignal.throwIfAborted(); + if (!response.ok) { + pausedUntil = Date.now() + PAUSE_AFTER_REFUSAL_MS; + return null; + } + const data = await response.json(); + return data?.features; + }, + { signal, timeoutMs: TIMEOUT_MS }, + ); + signal?.throwIfAborted(); + if (!Array.isArray(features)) return []; + + const seen = new Set(); + const places = []; + for (const feature of features) { + const normalized = normalizePhotonFeature(feature); + if (!normalized) continue; + const { + city, + state, + country, + countrycode: countryCode, + osm_value: osmType, + type, + } = feature.properties ?? {}; + const name = normalized.name || city || state || country; + if (!name) continue; + const detail = [...new Set([city, state, country])] + .filter((part) => part && part !== name) + .join(', '); + const id = `${name}|${detail}`; + if (seen.has(id)) continue; + seen.add(id); + const { southwest, northeast } = normalized.viewport ?? {}; + places.push({ + name, + detail, + latitude: normalized.lat, + longitude: normalized.lng, + type: (osmType === 'administrative' ? type : osmType || type) || '', + state: state || null, + country: country || null, + countryCode: /^[a-z]{2}$/i.test(countryCode ?? '') + ? countryCode.toUpperCase() + : null, + extent: southwest + ? [southwest.lng, northeast.lat, northeast.lng, southwest.lat] + : null, + }); + if (places.length >= limit) break; + } + + cache.set(key, places); + if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value); + return places; + }, + }; +} + +/** + * The cities that lie in a region: inside its extent (`[west, north, east, + * south]`, as Photon gives it) and, when the region names a country by its + * ISO code, in that country. Without either there is nothing to tell a + * region's cities from its neighbours', so none are listed. + * @param {Array<{latitude: number, longitude: number, country?: string}>} cities + * Bundled cities, `country` an ISO 3166-1 code. + * @param {{extent?: ?Array, countryCode?: ?string}} region + */ +export function citiesInRegion(cities, { extent, countryCode } = {}) { + const box = Array.isArray(extent) && extent.length === 4 ? extent : null; + if (!box && !countryCode) return []; + return cities.filter( + (city) => + (!countryCode || city.country === countryCode) && + (!box || inExtent(box, city)), + ); +} + +/** + * Whether a place lies in an extent `[west, north, east, south]`. An extent + * across the antimeridian (west east of east) runs from west round to east. + * @param {Array} extent + * @param {{latitude: number, longitude: number}} place + */ +export function inExtent([west, north, east, south], place) { + const lon = place.longitude; + const inLongitude = + west <= east ? lon >= west && lon <= east : lon >= west || lon <= east; + return place.latitude <= north && place.latitude >= south && inLongitude; +} diff --git a/src/search/placeSuggestions.test.mjs b/src/search/placeSuggestions.test.mjs new file mode 100644 index 0000000000..a549202f2c --- /dev/null +++ b/src/search/placeSuggestions.test.mjs @@ -0,0 +1,280 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { + citiesInRegion, + createPlaceSuggester, + inExtent, + mayBePrivate, +} from './placeSuggestions.js'; + +test('Photon suggestions keep distinct valid candidates and their region bounds', async () => { + const texas = { + geometry: { type: 'Point', coordinates: [-99, 31] }, + properties: { + name: 'Texas', + country: 'United States', + countrycode: 'us', + type: 'state', + osm_value: 'administrative', + extent: [-107, 37, -93, 25], + }, + }; + const requested = []; + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async (url) => { + requested.push(url); + return Response.json({ + features: [ + texas, + texas, + { + ...texas, + geometry: { type: 'Point', coordinates: [-99, 131] }, + }, + ], + }); + }, + }); + const expected = [ + { + name: 'Texas', + detail: 'United States', + type: 'state', + latitude: 31, + longitude: -99, + state: null, + country: 'United States', + countryCode: 'US', + extent: [-107, 37, -93, 25], + }, + ]; + assert.deepEqual(await service.suggest(' Texas '), expected); + assert.deepEqual(await service.suggest('texas'), expected); + assert.deepEqual(await service.suggest('Tx'), []); + assert.equal(requested.length, 1); + assert.equal(requested[0].origin, 'https://fixture.test'); + assert.equal(requested[0].searchParams.get('q'), 'Texas'); +}); + +test('a disabled or cancelled suggestion sends no request', async () => { + const sent = []; + const fetchImpl = async (url) => { + sent.push(url.searchParams.get('q')); + return Response.json({ features: [] }); + }; + const off = createPlaceSuggester({ endpoint: null, fetchImpl }); + assert.equal(off.enabled, false); + assert.deepEqual(await off.suggest('Texas'), []); + + const service = createPlaceSuggester({ fetchImpl }); + await service.suggest('Texas'); + const abort = new AbortController(); + const waiting = service.suggest('Austin', { signal: abort.signal }); + abort.abort(); + await assert.rejects(waiting, { name: 'AbortError' }); + assert.deepEqual(sent, ['Texas']); +}); + +test('suggestions while typing keep their spacing to the place service', async () => { + const sentAt = []; + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async () => { + sentAt.push(performance.now()); + return Response.json({ features: [] }); + }, + }); + // Three different texts at once, as fast typing past the debounce sends. + await Promise.all(['Aus', 'Aust', 'Austin'].map((q) => service.suggest(q))); + assert.equal(sentAt.length, 3); + for (let i = 1; i < sentAt.length; i++) + assert.ok( + sentAt[i] - sentAt[i - 1] >= 490, + `request ${i} came ${Math.round(sentAt[i] - sentAt[i - 1])} ms after the one before`, + ); +}); + +test('a state lists only its own cities: Texas has no Mexican one', () => { + const texas = { extent: [-106.65, 36.5, -93.51, 25.84], countryCode: 'US' }; + const cities = [ + { name: 'Houston', latitude: 29.76, longitude: -95.37, country: 'US' }, + { name: 'El Paso', latitude: 31.76, longitude: -106.49, country: 'US' }, + // Inside the Texas box, across the border. + { + name: 'Ciudad Juárez', + latitude: 31.74, + longitude: -106.49, + country: 'MX', + }, + { name: 'Reynosa', latitude: 26.09, longitude: -98.28, country: 'MX' }, + // In the US, outside Texas. + { name: 'Denver', latitude: 39.74, longitude: -104.99, country: 'US' }, + ]; + assert.deepEqual( + citiesInRegion(cities, texas).map((city) => city.name), + ['Houston', 'El Paso'], + ); + // A country without bounds still keeps only its own. + assert.deepEqual( + citiesInRegion(cities, { countryCode: 'MX' }).map((city) => city.name), + ['Ciudad Juárez', 'Reynosa'], + ); + // Fiji's extent runs across the antimeridian. + assert.deepEqual( + citiesInRegion( + [ + { name: 'Labasa', latitude: -16.43, longitude: 179.38, country: 'FJ' }, + { name: 'Lakeba', latitude: -18.2, longitude: -178.8, country: 'FJ' }, + ], + { extent: [177, -12, -178, -21], countryCode: 'FJ' }, + ).length, + 2, + ); + assert.deepEqual(citiesInRegion(cities, {}), []); +}); + +test('two-character Han, Hangul and Kana names are looked up', async () => { + const sent = []; + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async (url) => { + sent.push(url.searchParams.get('q')); + return Response.json({ features: [] }); + }, + }); + await service.suggest('東'); + await service.suggest('NY'); + await service.suggest('東京'); + assert.deepEqual(sent, ['東京']); +}); + +test('nothing that may be private is sent to the place service', async () => { + const sent = []; + const service = createPlaceSuggester({ + endpoint: 'https://fixture.test/api/', + fetchImpl: async (url) => { + sent.push(url.searchParams.get('q')); + return Response.json({ features: [] }); + }, + }); + for (const text of [ + 'me@example.com', + 'sk-abc123', + 'AKIAIOSFODNN7EXAMPLE', + 'ghp_0123abcd', + 'xoxb-1234', + 'token a1b2c3d4e5f6g7', + ]) + assert.deepEqual(await service.suggest(text), [], text); + assert.equal(mayBePrivate('Saint Petersburg 2'), false); + assert.equal(mayBePrivate('Bahnhofstrasse'), false); + assert.deepEqual(sent, []); +}); + +test('an extent across the antimeridian holds both of its sides', () => { + const fiji = [177, -12, -178, -21]; + assert.equal(inExtent(fiji, { latitude: -16.4, longitude: 179.4 }), true); + assert.equal(inExtent(fiji, { latitude: -18.2, longitude: -178.8 }), true); + assert.equal(inExtent(fiji, { latitude: -18.2, longitude: 170 }), false); + assert.equal( + inExtent([-107, 37, -93, 25], { latitude: 31, longitude: -99 }), + true, + ); +}); + +test('cards, phones, IBANs, keys and passwords are private', () => { + for (const text of [ + '4111 1111 1111 1111', + '+1 (415) 555-0100', + 'SSN 123-45-6789', + 'GB29 NWBK 6016 1331 9268 19', + 'sk_live_51Habc', + 'rk_live_x', + 'pk_live_abc', + 'AIzaSyA0', + 'eyJhbGciOi', + 'password hunter2', + // Digits of other scripts that would read as a spot once folded, and + // password words split by format characters or curly apostrophes. + '١٢٣٤, ٥٦٧٨', + '१२३४ ५६७८', + 'parola d’ordine viola', + 'pass\u200bword: violet', + 'mot\u200b de passe violet', + 'me\u200b@example.com', + '@elonmusk', + 'john.smith@', + 'my number is 555 0100 thanks', + 'john @ gmail.com', + 'door code 4421', + 'otp 482913', + 'verification code 482913', + '2fa 482913', + 'card ending 4242', + 'my secret is swordfish', + 'passphrase correct horse battery', + 'mnemonic: abandon ability able', + 'john at example dot com', + 'the access code is 8842', + 'ping @jdoe', + 'pass: correcthorse', + 'pin 1234', + // Full-width forms and digits of any script, and digits joined by + // dots, underscores or commas. + '4111 1111 1111 1111', + 'john@example.com', + 'sk_live_abc', + 'my password is hunter', + '123-45-6789', + '٤١١١ ١١١١ ١١١١ ١١١١', + '४१११ ११११ ११११ ११११', + '4111.1111.1111.1111', + '4111_1111_1111_1111', + '4111,1111,1111,1111', + '123.45.6789', + 'what is 4111.1111.1111.1111', + ]) + assert.equal(mayBePrivate(text), true, text); + for (const text of [ + 'Pin Oak, Texas', + // An address with a spaced "@", and names the old rules caught. + 'flat 2 @ 10 Downing St', + 'Nanjampeta @ Chandambylu', + 'Sachsenhausen', + 'Eisenhammer', + 'Xoxocotla', + 'Santa Cruz Xoxocotlán', + 'Yanino-1', + 'Meguro Honmachi 2-chome', + 'Chak No. 656/G.B.', + 'Route-66', + // Street addresses: a house number beside a postcode, CJK addresses + // written without spaces, and postcodes as they are written. + '北京市朝阳区建国路88号', + '東京都港区六本木6丁目', + '台北市信義區市府路45號', + '渋谷区道玄坂2-1', + '〒100-0001 東京都千代田区千代田1-1', + 'Washington DC 20500-0003', + '1600 Pennsylvania Ave NW, Washington, DC 20500-0003', + 'São Paulo 01310-200', + 'Calle 100 # 19-61 Bogota', + 'ul. Nowy Świat 10, 00-950 Warszawa', + 'Drottninggatan 53, 111 21 Stockholm', + 'Mariahilfer Straße 120, 1070 Wien', + 'Rua Augusta 274, 1100-053 Lisboa', + 'Tverskaya 13 125009 Moscow', + 'Hauptstrasse 12 10115 Berlin', + 'Calle Mayor 10, 28013 Madrid', + 'Av Paulista 1578 01310-200', + 'Eyjafjallajökull', + 'Route 66', + '30.27, -97.74', + '30.2672, -97.7431', + '14R PU 2090 4906', + 'New York 10001', + 'Passau', + ]) + assert.equal(mayBePrivate(text), false, text); +}); From c11200976fc2f0e834df2faec4c3f83b1b17de35 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 12:00:07 -0800 Subject: [PATCH 2/4] Say what the place suggester sends to Photon Co-authored-by: Manjunath Paparaju --- DATA_SOURCES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DATA_SOURCES.md b/DATA_SOURCES.md index 8049a412bc..441552dd89 100644 --- a/DATA_SOURCES.md +++ b/DATA_SOURCES.md @@ -287,6 +287,7 @@ The optional Local ADS-B layer and the broadcast-FM receiver use radio you recei - **Acartia.** Keyless. `/api/acartia/sightings` reads the current 7-day marine-mammal feed for ALL, MAMMALS and WHALES views intersecting 45.5–50.5° N, 126–121° W in the Salish Sea. WHALES accepts recognized cetaceans; other identified marine mammals appear under ALL/MAMMALS. The server rounds positions to 0.01° and drops comments before caching projected records (10 min fresh, stale ≤24 h); raw responses are not retained. Selected text cards credit the reporting provider under CC BY 4.0 and show Be Whale Wise guidance. `/sightings/current` answers without a key today, but Acartia's README asks API users to create an account and generate an API key on the [Acartia dashboard](https://acartia.io/) and to follow its [community guidelines](https://acartia.io/register). Attribution also appears in the layer row and Data attribution popover. Opportunistic sightings, not a census. - **Mapped installations.** OpenFreeMap military polygons use a bundled Overture/OSM name index; wide views show named points. Optional operator-configured Overpass and explicit Google Places searches keep their own source paths. Mapped data does not establish current activity or complete coverage. - **Place search.** Named-place fly-to uses Google Geocoding when a Maps key is configured, then Photon (`PHOTON_URL`; unset uses the public instance, empty disables it), then `/api/geocode` (Nominatim search) when neither answers. `/api/geocode` and the voice outline route `/api/geocode/outline` share one Nominatim queue (`NOMINATIM_URL`; unset, empty and invalid values disable it; an operator must explicitly select an authorized endpoint): one request at a time at least 1.1 s apart, an identifying `User-Agent` and `Referer`, a short queue that refuses a burst, a pause after 429/403/5xx answers (honouring `Retry-After` in full), at most one retry after a network failure, and a per-install daily cap on public-instance requests (`NOMINATIM_DAILY_CAP`, default 50). Processes using the same installation state file hold one lock through the actual fetch invocation and recording the next public start, with the daily slot persisted before invocation; restarts share that count, spacing and any pause. An unfinished dispatch marker fails closed until operator recovery; it is never automatically reclaimed. Public access fails closed if the state file cannot be read, locked, written or semantically validated. This coordination does not span independent installations or state directories and does not establish an application-wide public-service allowance or authorize public use. An operator-selected non-public endpoint is outside the public cap, spacing and durable-state requirement; an upstream cooldown is kept with the endpoint that returned it rather than leaking across operator URLs. Answers are cached on disk (outlines 90 days, places 30 days, no-results 24 hours); failures are not cached. Outlines are looked up only for an explicit ask, after bundled data and any configured Overpass, and only results whose type suits the ask are used. Administrative outline responses expose country/admin-1/admin-2 identity only when the selected Nominatim row has a recognized, unambiguous address type consistent with its own name; numeric rank, ambiguous types, missing fields and contradictory names do not manufacture identity. It is not used for systematic or bulk queries. +- **Place suggestions.** The search box's suggester (`src/search/placeSuggestions.js`) asks Photon for a few candidates while a place is typed: at most one request every 500 ms per tab, a six-second limit, none for a minute after a refusal, and text capped at 200 characters. Text that may be private (an email address, seven or more digits however separated, an IBAN shape, a password, PIN, token or key) is never sent. An empty `PHOTON_URL` turns suggestions off along with the place search. Answers are cached in the tab. - **Street and building outlines.** Voice street highlights, pointed-at buildings and grounds use the OpenFreeMap vector tiles the traffic layer already loads. Traffic and outlines share one bounded pending fetch/decode owner per immutable XYZ tile; materialized outline geometry is charged to the retained-byte ceiling and capped by coordinate count. Consumer result caches and request lifetimes remain independent, while eviction or explicit teardown cancels queued idle projections and releases their decoded owner. No Overpass request is made unless `OVERPASS_UPSTREAMS` is configured. Named grounds require a returned name matching the selected place, except that an ask whose head noun names a kind of grounds (a school, university or college, hospital, cemetery, stadium or zoo) may use the part of the smallest unnamed tile area of that class that holds the resolved place; a differently named area never answers. Other anonymous landcover polygons cannot establish identity, so unsupported grounds stay unavailable rather than borrowing a nearby POI name. - **Voice areas and place search.** Voice areas (`resolve_area`) use the same outline sources as voice outlines. Voice place search (`osm_query`, `/api/osm/features`) runs only on an operator-configured Overpass: a fixed list of kinds, a bounded box and result count, one query at a time, answers cached for a day. It never uses Nominatim. - **Cockpit regional briefing.** `/api/regional-brief` rounds aircraft coordinates into 0.1° cache cells, caches results for five minutes, and resolves physical regions from bundled public-domain Natural Earth polygons without reverse-geocoding requests. Google News RSS is queried with the resolved locality/region first; GDELT is used only when that RSS query fails or is empty. Google's published Google News terms restrict that source to personal, noncommercial use, so commercial deployments must disable/replace it or obtain separate permission; GDELT permits commercial dataset use with citation. The Data attribution popover identifies the active headline sources; article links retain publisher attribution. Headlines are location-query matches, not verified incidents, risk rankings, or evidence that a location is safe. Empty, partial, stale, and unavailable source states remain distinct. Open-Meteo supplies current conditions independently of the news source. `WX OFF` disables cockpit weather rendering only; the Local Info briefing still fetches its source-backed weather values and displays the required linked Open-Meteo credit. From 9ec95102ca6d5528fdbdb18b233b4566406e9bec Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 12:00:24 -0800 Subject: [PATCH 3/4] Add the changelog entry for the place suggester Co-authored-by: Manjunath Paparaju --- changelog.d/1814-place-suggestions.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog.d/1814-place-suggestions.md diff --git a/changelog.d/1814-place-suggestions.md b/changelog.d/1814-place-suggestions.md new file mode 100644 index 0000000000..48f67fc721 --- /dev/null +++ b/changelog.d/1814-place-suggestions.md @@ -0,0 +1,6 @@ +- The place suggester asks Photon for candidates while a place is typed: at + most one request every 500 ms per tab, none for a minute after a refusal, + and never text that may be private (an email address, long numbers, an + IBAN shape, a password, PIN, token or key). An empty `PHOTON_URL` turns it + off along with the place search. The search box that uses it follows + (manjunath22466, samehkhamis, #1814). From c06aa37ce2c829aea4e3efe9f688a4fb74cdae46 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 12:05:16 -0800 Subject: [PATCH 4/4] Keep the place suggester's fair-use and privacy promises - One spacing, pause and cache for the whole tab, so a search box put up again does not start afresh. - A failed request or a timeout pauses suggestions for a minute, as a refusal does. - A number named as a card, a phone or an account keeps its digits together, so no address is read out of them; a UK postcode written without its space is a place, not a password. - The docs say exactly what is sent. Co-authored-by: Manjunath Paparaju --- DATA_SOURCES.md | 2 +- changelog.d/1814-place-suggestions.md | 10 +-- scripts/format-scope.json | 3 +- src/search/placeSuggestions.js | 109 ++++++++++++++++---------- src/search/placeSuggestions.test.mjs | 49 ++++++++++++ 5 files changed, 126 insertions(+), 47 deletions(-) diff --git a/DATA_SOURCES.md b/DATA_SOURCES.md index 441552dd89..ce24d709cd 100644 --- a/DATA_SOURCES.md +++ b/DATA_SOURCES.md @@ -287,7 +287,7 @@ The optional Local ADS-B layer and the broadcast-FM receiver use radio you recei - **Acartia.** Keyless. `/api/acartia/sightings` reads the current 7-day marine-mammal feed for ALL, MAMMALS and WHALES views intersecting 45.5–50.5° N, 126–121° W in the Salish Sea. WHALES accepts recognized cetaceans; other identified marine mammals appear under ALL/MAMMALS. The server rounds positions to 0.01° and drops comments before caching projected records (10 min fresh, stale ≤24 h); raw responses are not retained. Selected text cards credit the reporting provider under CC BY 4.0 and show Be Whale Wise guidance. `/sightings/current` answers without a key today, but Acartia's README asks API users to create an account and generate an API key on the [Acartia dashboard](https://acartia.io/) and to follow its [community guidelines](https://acartia.io/register). Attribution also appears in the layer row and Data attribution popover. Opportunistic sightings, not a census. - **Mapped installations.** OpenFreeMap military polygons use a bundled Overture/OSM name index; wide views show named points. Optional operator-configured Overpass and explicit Google Places searches keep their own source paths. Mapped data does not establish current activity or complete coverage. - **Place search.** Named-place fly-to uses Google Geocoding when a Maps key is configured, then Photon (`PHOTON_URL`; unset uses the public instance, empty disables it), then `/api/geocode` (Nominatim search) when neither answers. `/api/geocode` and the voice outline route `/api/geocode/outline` share one Nominatim queue (`NOMINATIM_URL`; unset, empty and invalid values disable it; an operator must explicitly select an authorized endpoint): one request at a time at least 1.1 s apart, an identifying `User-Agent` and `Referer`, a short queue that refuses a burst, a pause after 429/403/5xx answers (honouring `Retry-After` in full), at most one retry after a network failure, and a per-install daily cap on public-instance requests (`NOMINATIM_DAILY_CAP`, default 50). Processes using the same installation state file hold one lock through the actual fetch invocation and recording the next public start, with the daily slot persisted before invocation; restarts share that count, spacing and any pause. An unfinished dispatch marker fails closed until operator recovery; it is never automatically reclaimed. Public access fails closed if the state file cannot be read, locked, written or semantically validated. This coordination does not span independent installations or state directories and does not establish an application-wide public-service allowance or authorize public use. An operator-selected non-public endpoint is outside the public cap, spacing and durable-state requirement; an upstream cooldown is kept with the endpoint that returned it rather than leaking across operator URLs. Answers are cached on disk (outlines 90 days, places 30 days, no-results 24 hours); failures are not cached. Outlines are looked up only for an explicit ask, after bundled data and any configured Overpass, and only results whose type suits the ask are used. Administrative outline responses expose country/admin-1/admin-2 identity only when the selected Nominatim row has a recognized, unambiguous address type consistent with its own name; numeric rank, ambiguous types, missing fields and contradictory names do not manufacture identity. It is not used for systematic or bulk queries. -- **Place suggestions.** The search box's suggester (`src/search/placeSuggestions.js`) asks Photon for a few candidates while a place is typed: at most one request every 500 ms per tab, a six-second limit, none for a minute after a refusal, and text capped at 200 characters. Text that may be private (an email address, seven or more digits however separated, an IBAN shape, a password, PIN, token or key) is never sent. An empty `PHOTON_URL` turns suggestions off along with the place search. Answers are cached in the tab. +- **Place suggestions.** The search box's suggester (`src/search/placeSuggestions.js`) asks Photon for a few candidates while a place is typed: at most one request every 500 ms per tab (shared by every search box), a six-second limit for the whole answer, none for a minute after a refusal, a failed request or a timeout, and text capped at 200 characters. Text that may be private is never sent: an email address, seven or more digits however separated (a street address's house number and postcode excepted, unless a word such as "card" or "phone" names the number), an IBAN shape, a password, PIN, token or key. An empty `PHOTON_URL` turns suggestions off along with the place search. Answers are cached in the tab. - **Street and building outlines.** Voice street highlights, pointed-at buildings and grounds use the OpenFreeMap vector tiles the traffic layer already loads. Traffic and outlines share one bounded pending fetch/decode owner per immutable XYZ tile; materialized outline geometry is charged to the retained-byte ceiling and capped by coordinate count. Consumer result caches and request lifetimes remain independent, while eviction or explicit teardown cancels queued idle projections and releases their decoded owner. No Overpass request is made unless `OVERPASS_UPSTREAMS` is configured. Named grounds require a returned name matching the selected place, except that an ask whose head noun names a kind of grounds (a school, university or college, hospital, cemetery, stadium or zoo) may use the part of the smallest unnamed tile area of that class that holds the resolved place; a differently named area never answers. Other anonymous landcover polygons cannot establish identity, so unsupported grounds stay unavailable rather than borrowing a nearby POI name. - **Voice areas and place search.** Voice areas (`resolve_area`) use the same outline sources as voice outlines. Voice place search (`osm_query`, `/api/osm/features`) runs only on an operator-configured Overpass: a fixed list of kinds, a bounded box and result count, one query at a time, answers cached for a day. It never uses Nominatim. - **Cockpit regional briefing.** `/api/regional-brief` rounds aircraft coordinates into 0.1° cache cells, caches results for five minutes, and resolves physical regions from bundled public-domain Natural Earth polygons without reverse-geocoding requests. Google News RSS is queried with the resolved locality/region first; GDELT is used only when that RSS query fails or is empty. Google's published Google News terms restrict that source to personal, noncommercial use, so commercial deployments must disable/replace it or obtain separate permission; GDELT permits commercial dataset use with citation. The Data attribution popover identifies the active headline sources; article links retain publisher attribution. Headlines are location-query matches, not verified incidents, risk rankings, or evidence that a location is safe. Empty, partial, stale, and unavailable source states remain distinct. Open-Meteo supplies current conditions independently of the news source. `WX OFF` disables cockpit weather rendering only; the Local Info briefing still fetches its source-backed weather values and displays the required linked Open-Meteo credit. diff --git a/changelog.d/1814-place-suggestions.md b/changelog.d/1814-place-suggestions.md index 48f67fc721..1c02bcb55c 100644 --- a/changelog.d/1814-place-suggestions.md +++ b/changelog.d/1814-place-suggestions.md @@ -1,6 +1,6 @@ - The place suggester asks Photon for candidates while a place is typed: at - most one request every 500 ms per tab, none for a minute after a refusal, - and never text that may be private (an email address, long numbers, an - IBAN shape, a password, PIN, token or key). An empty `PHOTON_URL` turns it - off along with the place search. The search box that uses it follows - (manjunath22466, samehkhamis, #1814). + most one request every 500 ms per tab, none for a minute after a refusal + or a failed request, and never text that may be private (an email address, + long numbers, an IBAN shape, a password, PIN, token or key). An empty + `PHOTON_URL` turns it off along with the place search. The search box that + uses it follows (manjunath22466, samehkhamis, #1814). diff --git a/scripts/format-scope.json b/scripts/format-scope.json index aa51f28c05..2472b69114 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -1583,5 +1583,6 @@ "src/ui/layerFinder.test.mjs", "src/data/wikidataCities.js", "src/data/wikidataCities.test.mjs", - "scripts/build-wikidata-cities.mjs" + "scripts/build-wikidata-cities.mjs", + "src/search/placeSuggestions.test.mjs" ] diff --git a/src/search/placeSuggestions.js b/src/search/placeSuggestions.js index f00f847dab..e552fbd97f 100644 --- a/src/search/placeSuggestions.js +++ b/src/search/placeSuggestions.js @@ -130,25 +130,34 @@ function privacyReason(text) { // ("DC 20500-0003"), a Brazilian CEP ("01310-200") and a Japanese one // after "〒" or before the address ("〒100-0001", "100-0001 東京都"). // "#" (a street number's sign, "Calle 100 # 19-61") splits digits too. - const digits = line - .replace(/(^|\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)/giu, '$1 ') - // 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): - // "Nowy Świat 10, 00-950 Warszawa", "Drottninggatan 53, 111 21 - // Stockholm", "Mariahilfer Straße 120, 1070 Wien", "Rua Augusta 274, - // 1100-053 Lisboa", "Tverskaya 13 125009 Moscow". - .replace( - /(?<=\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 ') - .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, - ' x ', - ) - .replace(/#/g, ' x ') - .replace(/[^\p{L}\p{N}]/gu, ''); + // A word that says what the number is ("card 4111 111111", "call 0207 + // 946095") keeps the digits together: no address is read out of them. + const named = NUMBER_CONTEXT.test(line); + const digits = ( + named + ? line + : line + .replace( + /(^|\s)\d{1,4}[a-z]?,?\s+\d{5}(?:-\d{3})?(?=\s+\p{L}|$)/giu, + '$1 ', + ) + // 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): + // "Nowy Świat 10, 00-950 Warszawa", "Drottninggatan 53, 111 21 + // Stockholm", "Mariahilfer Straße 120, 1070 Wien", "Rua Augusta 274, + // 1100-053 Lisboa", "Tverskaya 13 125009 Moscow". + .replace( + /(?<=\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 ') + .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, + ' x ', + ) + .replace(/#/g, ' x ') + ).replace(/[^\p{L}\p{N}]/gu, ''); const longNumber = /\d{7,}/.test(digits) || // A phone number written with dots ("415.555.0100"). @@ -205,6 +214,8 @@ function mayBeSecret(token) { // is a name. // Latin letters only: CJK addresses are written without spaces // ("東京都港区六本木6丁目", "北京市朝阳区建国路88号"). + // A UK postcode written without its space ("EC1A1BB", "BT487NN"). + if (/^[a-z]{1,2}\d[a-z\d]?\d[a-z]{2}$/i.test(token)) return false; const mixed = token .split(/[^\p{L}\p{N}]+/u) .some((run) => /\p{Script=Latin}/u.test(run) && /\d/.test(run)); @@ -221,6 +232,19 @@ const TIMEOUT_MS = 6000; const PAUSE_AFTER_REFUSAL_MS = 60_000; const CACHE_LIMIT = 200; +/** + * The spacing, the pause and the cache, shared by every suggester in the tab: + * a search box that is put up again starts from where the last one left off. + */ +const shared = { nextRequestAt: 0, pausedUntil: 0, cache: new Map() }; + +/** Forget the shared spacing, pause and cache (tests). */ +export function resetPlaceSuggestions() { + shared.nextRequestAt = 0; + shared.pausedUntil = 0; + shared.cache.clear(); +} + /** List Photon candidates while typing. The submitted search owns navigation. */ export function createPlaceSuggester({ endpoint, @@ -228,9 +252,7 @@ export function createPlaceSuggester({ limit = 4, } = {}) { const enabled = endpoint === undefined || Boolean(endpoint); - const cache = new Map(); - let nextRequestAt = 0; - let pausedUntil = 0; + const { cache } = shared; return { enabled, @@ -244,14 +266,14 @@ export function createPlaceSuggester({ const language = (globalThis.document?.documentElement?.lang || 'en') .slice(0, 2) .toLowerCase(); - const key = `${language}|${text.toLowerCase()}`; + const key = `${endpoint ?? ''}|${limit}|${language}|${text.toLowerCase()}`; if (cache.has(key)) return cache.get(key); // Concurrent callers recheck the gap after waking before sending. - while (nextRequestAt > Date.now()) { + while (shared.nextRequestAt > Date.now()) { let timer; const pause = new Promise((resolve) => { - timer = setTimeout(resolve, nextRequestAt - Date.now()); + timer = setTimeout(resolve, shared.nextRequestAt - Date.now()); }); try { await waitForSignal(pause, signal); @@ -260,25 +282,32 @@ export function createPlaceSuggester({ } } signal?.throwIfAborted(); - if (Date.now() < pausedUntil) return []; + if (Date.now() < shared.pausedUntil) return []; const url = new URL(photonSearchUrl(text, { endpoint, limit })); if (['en', 'de', 'fr', 'it'].includes(language)) url.searchParams.set('lang', language); - nextRequestAt = Date.now() + MIN_GAP_MS; - const features = await requestWithDeadline( - async (requestSignal) => { - const response = await fetchImpl(url, { signal: requestSignal }); - requestSignal.throwIfAborted(); - if (!response.ok) { - pausedUntil = Date.now() + PAUSE_AFTER_REFUSAL_MS; - return null; - } - const data = await response.json(); - return data?.features; - }, - { signal, timeoutMs: TIMEOUT_MS }, - ); + shared.nextRequestAt = Date.now() + MIN_GAP_MS; + let features; + try { + features = await requestWithDeadline( + async (requestSignal) => { + const response = await fetchImpl(url, { signal: requestSignal }); + requestSignal.throwIfAborted(); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + const data = await response.json(); + return data?.features; + }, + { signal, timeoutMs: TIMEOUT_MS }, + ); + } catch (error) { + signal?.throwIfAborted(); + // A refusal, a timeout or a failed request (a refusal sent without + // CORS headers arrives as one) all hold suggestions off a while. + void error; + shared.pausedUntil = Date.now() + PAUSE_AFTER_REFUSAL_MS; + return []; + } signal?.throwIfAborted(); if (!Array.isArray(features)) return []; diff --git a/src/search/placeSuggestions.test.mjs b/src/search/placeSuggestions.test.mjs index a549202f2c..f5e2ac6f51 100644 --- a/src/search/placeSuggestions.test.mjs +++ b/src/search/placeSuggestions.test.mjs @@ -5,8 +5,12 @@ import { createPlaceSuggester, inExtent, mayBePrivate, + resetPlaceSuggestions, } from './placeSuggestions.js'; +// The spacing, pause and cache are the tab's: each test starts clean. +test.beforeEach(() => resetPlaceSuggestions()); + test('Photon suggestions keep distinct valid candidates and their region bounds', async () => { const texas = { geometry: { type: 'Point', coordinates: [-99, 31] }, @@ -278,3 +282,48 @@ test('cards, phones, IBANs, keys and passwords are private', () => { ]) assert.equal(mayBePrivate(text), false, text); }); + +const answering = (calls, answer) => async (url) => { + calls.push(String(url)); + if (answer instanceof Error) throw answer; + return { ok: true, json: async () => ({ features: [] }) }; +}; + +test('a failed or timed-out request pauses suggestions as a refusal does', async () => { + const calls = []; + const suggester = createPlaceSuggester({ + endpoint: 'https://photon.test/api/', + fetchImpl: answering(calls, new TypeError('Failed to fetch')), + }); + assert.deepEqual(await suggester.suggest('Paris'), []); + assert.deepEqual(await suggester.suggest('Berlin'), []); + assert.equal(calls.length, 1); +}); + +test('every suggester in the tab keeps one spacing and one pause', async () => { + const calls = []; + const options = { + endpoint: 'https://photon.test/api/', + fetchImpl: answering(calls), + }; + const first = createPlaceSuggester(options); + const second = createPlaceSuggester(options); + const started = Date.now(); + await Promise.all([first.suggest('Paris'), second.suggest('Berlin')]); + assert.equal(calls.length, 2); + assert.ok(Date.now() - started >= 490); +}); + +test('a number named as a card or a phone stays private', () => { + for (const line of [ + 'card 4111 111111 thanks', + 'call 0207 946095 now', + 'phone 12 345678 home', + ]) + assert.equal(mayBePrivate(line), true, line); +}); + +test('a UK postcode without its space is a place', () => { + for (const line of ['EC1A1BB', 'W1A0AX', 'BT487NN']) + assert.equal(mayBePrivate(line), false, line); +});