Address implementation-review blockers: import ordering, site-granular inventory, uppercase bech32

Blocker 1: import_identity persisted-before-cleanup ordering. New
commit_imported_identity helper runs durable persistence FIRST; a failed
different-key import now leaves both the old in-memory identity and its
valid canonical identity.ncryptsec intact. Stale-backup cleanup runs after
the durable commit and is deliberately best-effort (logged, not surfaced
as a half-applied-import error). Regression tests cover the failure path
and prove cleanup cannot precede persist.

Blocker 2: the /events inventory tripwire is now site-granular. Each
inventoried file pairs an expected non-comment /events occurrence count
with an expected egress-guard call count, so an unguarded ninth site in an
already-listed file (or a deleted guard call) fails the scan. The scan
core is pure over (path, content) pairs, with mutation-style tests
demonstrating all three drift classes.

Hardening: bech32 permits an all-uppercase encoding, so the egress guard
now rejects NCRYPTSEC1... too (mixed case stays unblocked - it cannot
decode), and both import classifiers (Rust recover_keys_from_input, TS
classifyKeyImportInput/isPlausibleNcryptsec) route uppercase-valid blobs
to the encrypted path consistently.

Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
This commit is contained in:
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
2026-07-25 23:45:09 -04:00
parent 1c01889929
commit dde37183e3
7 changed files with 379 additions and 72 deletions

View File

@@ -28,6 +28,22 @@ test("classify_by_hrp_with_whitespace_tolerance", () => {
assert.equal(classifyKeyImportInput("nsec1"), "nsec");
});
test("uppercase_bech32_encoding_classifies_and_gates_like_lowercase", () => {
// Bech32 permits an all-uppercase encoding; it must route to the
// encrypted path (matching Rust) and be submit-plausible.
const upper = NCRYPTSEC.toUpperCase();
assert.equal(classifyKeyImportInput(upper), "ncryptsec");
assert.equal(isPlausibleNcryptsec(upper), true);
assert.equal(keyImportSubmitEnabled(upper, ""), false);
assert.equal(keyImportSubmitEnabled(upper, "hunter2hunter2"), true);
// Mixed case: routed encrypted (Rust reports the accurate error) but
// never plausible/submittable — mixed-case bech32 cannot decode.
const mixed = `N${NCRYPTSEC.slice(1)}`;
assert.equal(classifyKeyImportInput(mixed), "ncryptsec");
assert.equal(isPlausibleNcryptsec(mixed), false);
assert.equal(keyImportSubmitEnabled(mixed, "hunter2hunter2"), false);
});
test("plausible_ncryptsec_requires_bech32_charset", () => {
assert.equal(isPlausibleNcryptsec(NCRYPTSEC), true);
// '1' and 'b' / 'i' / 'o' are not in the bech32 charset.

View File

@@ -12,12 +12,20 @@ import { nsecToNpub } from "@/shared/lib/nostrUtils";
export type KeyImportKind = "nsec" | "ncryptsec" | "unknown";
/** Bech32 charset after the `ncryptsec1` HRP; anything else can't decode. */
const NCRYPTSEC_SHAPE = /^ncryptsec1[02-9ac-hj-np-z]{10,}$/;
/**
* Bech32 charset after the `ncryptsec1` HRP; anything else can't decode.
* Bech32 also permits an ALL-UPPERCASE encoding of the same payload, so both
* casings are plausible (mixed case is invalid and stays implausible).
*/
const NCRYPTSEC_SHAPE =
/^(?:ncryptsec1[02-9ac-hj-np-z]{10,}|NCRYPTSEC1[02-9AC-HJ-NP-Z]{10,})$/;
export function classifyKeyImportInput(input: string): KeyImportKind {
const trimmed = input.trim();
if (trimmed.startsWith("ncryptsec1")) return "ncryptsec";
// Case-insensitive on the HRP to match the Rust classifier: an uppercase
// valid backup routes to the encrypted path (and decodes there); mixed
// case routes there too and fails in Rust with the accurate error.
if (trimmed.slice(0, 10).toLowerCase() === "ncryptsec1") return "ncryptsec";
if (trimmed.startsWith("nsec1")) return "nsec";
return "unknown";
}