Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

API: a passkey name can no longer carry a right-to-left override or other invisible format characters (#95)

Duarte Santos53ec5224461c
diff --git a/api/passkeys-store.js b/api/passkeys-store.js
index 479859877..cbfe85857 100644
--- a/api/passkeys-store.js
+++ b/api/passkeys-store.js
@@ -17,8 +17,14 @@ export const NAME_MAX = 40;
// A label someone typed: control characters out, runs of whitespace folded, capped. Empty means
// no name, and the app shows a numbered "Passkey n" instead.
+//
+// Invisible format characters go too. The device redeeming a code names its own passkey, and the
+// owner and the admin read that name: a right-to-left override (U+202E) turns the rest of it
+// around, so a passkey could be made to read like another one. The two joiners stay, since emoji
+// sequences and several scripts (Devanagari and Arabic among them) need them to render.
export const passkeyName = v => (typeof v === 'string' ? v : '')
- .replace(/[\u0000-\u001f\u007f]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, NAME_MAX);
+ .replace(/[\u0000-\u001f\u007f]/g, ' ').replace(/(?![\u200c\u200d])\p{Cf}/gu, '')
+ .replace(/\s+/g, ' ').trim().slice(0, NAME_MAX);
// `transports` comes from the browser that made the passkey, which is to say from the request. It
// is handed back to browsers as a hint, so only a short list of short words is kept.
diff --git a/api/test/passkeys-store.test.js b/api/test/passkeys-store.test.js
index 47d2bb7e2..7c51154b9 100644
--- a/api/test/passkeys-store.test.js
+++ b/api/test/passkeys-store.test.js
@@ -78,6 +78,16 @@ describe('passkeyName', () => {
assert.equal(passkeyName(42), '');
assert.equal(passkeyName({ toString: () => 'x' }), '');
});
+
+ it('drops invisible format characters that reorder or hide text, and keeps the joiners', () => {
+ // Right-to-left override and isolates, zero-width space, word joiner, byte-order mark, soft hyphen.
+ assert.equal(passkeyName('Phone\u202Egnp.exe'), 'Phonegnp.exe');
+ assert.equal(passkeyName('\u2066Lap\u200Btop\u2069 \u2060\uFEFFkey\u00AD'), 'Laptop key');
+ assert.equal(passkeyName('\u200E\u200F\u061C'), '');
+ // An emoji sequence and a Devanagari conjunct keep their joiners.
+ assert.equal(passkeyName('👨\u200D👩\u200D👧 iPad'), '👨\u200D👩\u200D👧 iPad');
+ assert.equal(passkeyName('क्\u200Dष फ़ोन'), 'क्\u200Dष फ़ोन');
+ });
});
describe('removePasskeyRecord', () => {