Read lists of places as places, not recovery phrases, and keep code tails fast

Twelve BIP39 words in a row with three words for places or things on a map
("coffee, bar, hotel, museum, park …") are a list, not a phrase; a random
phrase has three of them about once in two hundred. A code's value and the
courtesy word after it no longer overlap, and trailing punctuation is
trimmed from a word's end only, so a long run of hyphens is read at once.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-18 19:13:04 -08:00
1 parent 3f5221b837
commit f75f42d3ca
3 files changed
+53 -12

No files matched your search

+39 -8
View File
@@ -15,6 +15,23 @@ 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);
/**
* Words on the BIP39 list that name places and things on a map, for telling
* a list of places from a recovery phrase.
*/
const MAP_WORDS = new Set(
(
'airport bar beach bike boat bridge bus cafe car castle city coast coffee ' +
'country desert east elevator ferry fire flight forest garden harbor ' +
'hospital hotel house island lake market moon mountain museum north ' +
'ocean office park planet pizza rain river road rocket satellite school ' +
'sea ship shop snow south space station storm street sun ' +
'swim taxi tower town traffic train truck tunnel valley village ' +
'weather west wind world zoo canyon church stadium bank restaurant ' +
'map area today tomorrow near'
).split(' '),
);
/**
* Text that may hold something private, which is never looked up, offered
* as a place or sent anywhere:
@@ -192,13 +209,25 @@ function privacyReason(text) {
// question). Too long to be sent for suggestions anyway, it is kept from
// the assistant too.
// Written with commas, numbered ("1. abandon 2. ability") or after a few
// words ("my seed is …"): twelve of the list's words in a row.
// words ("my seed is …"): twelve of the list's words in a row. The list
// has many words for places and things on a map, and a phrase is drawn at
// random from all of it: twelve with three of those ("coffee, bar,
// hotel, museum, park …") is a list of places.
let run = 0;
let mapWords = 0;
for (const token of line.toLowerCase().split(/[\s,;]+/)) {
const word = token.replace(/^\d+[.)]?/, '').replace(/[^a-z]+$/, '');
const word = token
.replace(/^\d+[.)]?/, '')
.replace(/(?<=[a-z])[^a-z]+$/, '');
if (!word && token) continue;
run = isBip39Word(word) ? run + 1 : 0;
if (run >= 12) return 'other';
if (!isBip39Word(word)) {
run = 0;
mapWords = 0;
continue;
}
run += 1;
if (MAP_WORDS.has(word)) mapWords += 1;
if (run >= 12 && mapWords < 3) return 'other';
}
// Coordinates and grid references are a spot, not a number to keep: only
// text the place search itself reads as coordinates, as typed ("١٢٣٤,
@@ -331,7 +360,9 @@ function privacyReason(text) {
.split(/\s+/)
// A key in quotes or brackets ("\"sk-…\"") and a postcode before a
// comma ("SW1A2AA,") are read without them.
.map((token) => token.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, ''))
.map((token) =>
token.replace(/^[^\p{L}\p{N}]+|(?<=[\p{L}\p{N}])[^\p{L}\p{N}]+$/gu, ''),
)
.some(
(token) =>
/^(?:sk[-_]|rk_|pk_live|ghp_|gho_|ghu_|ghs_|github_pat_|glpat-|hf_|xox[abposr]-)/i.test(
@@ -379,7 +410,7 @@ function codeAfterLabel(line) {
// After a label in lower case, any value is a code ("verification code
// Abc"); after one written as a name, only a value that is not one.
const coded =
/\b((?:otp|2fa|mfa|cvv|cvc|(?:verification|one-time|security|auth|login) code)(?:\s+is)?)\s+([a-z0-9][a-z0-9-]{2,})(?:[\s,;-]+(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
/\b((?:otp|2fa|mfa|cvv|cvc|(?:verification|one-time|security|auth|login) code)(?:\s+is)?)\s+([a-z0-9][a-z0-9-]{2,63})(?:(?:\s*[,;]\s*|\s+)(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
line,
);
if (coded) {
@@ -406,7 +437,7 @@ function codeAfterLabel(line) {
}
// A value in mixed case that is no name ("PIN aBcD", "gate code aBcD").
const mixed =
/\b(?:pin|(?:door|access|gate|alarm|entry) code)(?:\s+is)?\s+([a-z0-9-]{4,})(?:[\s,;-]+(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
/\b(?:pin|(?:door|access|gate|alarm|entry) code)(?:\s+is)?\s+([a-z0-9-]{4,64})(?:(?:\s*[,;]\s*|\s+)(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
line,
);
if (
@@ -417,7 +448,7 @@ function codeAfterLabel(line) {
)
return true;
const match =
/\b((?:pin|cvv|cvc|otp|2fa|mfa|(?:door|access|gate|alarm|entry|security|verification|one-time|auth|login) code)(?:\s+is)?)\s+([a-z0-9][a-z0-9-]{3,})(?:[\s,;-]+(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
/\b((?:pin|cvv|cvc|otp|2fa|mfa|(?:door|access|gate|alarm|entry|security|verification|one-time|auth|login) code)(?:\s+is)?)\s+([a-z0-9][a-z0-9-]{3,63})(?:(?:\s*[,;]\s*|\s+)(?:thanks|thank you|thx|ty|cheers|please|pls|ok|now)(?:\s+(?:a lot|so much|very much|again))?)?[\s.!?]*$/i.exec(
line,
);
if (!match) return false;
+11 -3
View File
@@ -539,6 +539,10 @@ test('places that only looked private are looked up', () => {
'meet at West Point tomorrow',
'OTP Bank',
'GATE CODE LANE',
// Lists of places, though every word is on the BIP39 list.
'coffee, bar, hotel, museum, park, beach, hospital, school, market, bridge, tower, airport',
'1. beach 2. park 3. museum 4. zoo 5. bridge 6. tower 7. market 8. castle 9. island 10. lake 11. river 12. canyon',
'show only bus, train, car, truck, ship, boat traffic over city bridge today',
// Questions for the assistant, however many short words they have.
'can you tell where the most flights are going over texas today',
'are there any storms near florida right now that might cause delays',
@@ -642,9 +646,13 @@ test('a line longer than a place name is never sent for suggestions', async () =
});
test('a long line is read quickly', () => {
for (const unit of ['a-', 'a.', '1-', 'a ', '//[a:']) {
for (const line of [
...['a-', 'a.', '1-', 'a ', '//[a:'].map((unit) => unit.repeat(10000)),
`otp a${'-'.repeat(20000)}!x`,
`gate code a${'-'.repeat(20000)}!x`,
]) {
const started = performance.now();
mayBePrivate(unit.repeat(10000));
assert.ok(performance.now() - started < 1000, unit);
mayBePrivate(line);
assert.ok(performance.now() - started < 1000, line.slice(0, 12));
}
});