commit 20d452496467da9fa7e3f2854eef664a9b60be2c
parent 6948b039c2c5c8e3a49bf1c85faf3a2e21c8015b
Author: triesap <tyson@radroots.org>
Date: Tue, 13 Oct 2026 23:01:08 +0000
web: Preserve original incoming message identities
- Accept bounded recipient hints and inert incoming tags without routing effects
- Hash original rumor fields while keeping strict outgoing templates intact
- Verify received persistence and actual encrypted browser admission boundaries
- Bind the incoming owner to exact output guards and hostile provenance controls
Diffstat:
12 files changed, 737 insertions(+), 207 deletions(-)
diff --git a/web/src/lib/messaging/admit-conversation.ts b/web/src/lib/messaging/admit-conversation.ts
@@ -1,12 +1,5 @@
-import { buildCanonicalPairRumor } from '../nostr/rumor-template.ts';
import { canonicalPublicKey } from '../contracts/public-key.ts';
-import { exactLocalFields } from '../contracts/local-records.ts';
-import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts';
-import {
- messageFromWireParts,
- messageToWireParts
-} from '../contracts/message-v1/index.ts';
-import { safeUnsignedInteger } from '../nostr/envelope-bounds.ts';
+import { readIncomingPairRumor } from '../nostr/incoming-wire.ts';
import {
receivedNestedEnvelopeSnapshot,
receivedNestedEnvelopeOwnership,
@@ -37,44 +30,18 @@ export function inspectConversationData(
if (
!owner ||
(expectedRole !== 'inbound' && expectedRole !== 'self_archive') ||
- typeof raw !== 'string' ||
- !boundedUtf8(raw, 8192)
+ typeof raw !== 'string'
)
return undefined;
try {
- const rumor: unknown = JSON.parse(raw);
- if (
- !exactLocalFields(rumor, [
- 'id',
- 'pubkey',
- 'created_at',
- 'kind',
- 'tags',
- 'content'
- ]) ||
- rumor.kind !== 14 ||
- typeof rumor.id !== 'string' ||
- !/^[0-9a-f]{64}$/u.test(rumor.id) ||
- safeUnsignedInteger(rumor.created_at) === undefined
- )
- return undefined;
- const sender = canonicalPublicKey(rumor.pubkey);
- if (!sender) return undefined;
- const parts = { kind: 14, tags: rumor.tags, content: rumor.content };
- const decoded = messageFromWireParts(JSON.stringify(parts));
- if (!decoded || decoded.recipients.length !== 1) return undefined;
+ const incoming = readIncomingPairRumor(raw);
+ if (!incoming) return undefined;
+ const rumor = incoming.original,
+ decoded = incoming.message,
+ sender = rumor.pubkey;
const recipient = decoded.recipients[0].public_key;
if (sender === recipient || (owner !== sender && owner !== recipient))
return undefined;
- const canonical = messageToWireParts(JSON.stringify(decoded));
- if (!canonical || JSON.stringify(canonical) !== JSON.stringify(parts))
- return undefined;
- const template = buildCanonicalPairRumor(
- JSON.stringify(decoded),
- sender,
- rumor.created_at
- );
- if (!template || template.id !== rumor.id) return undefined;
const role = sender === owner ? 'self_archive' : 'inbound';
if (role !== expectedRole) return undefined;
return {
@@ -84,8 +51,8 @@ export function inspectConversationData(
recipient,
role,
rumorId: rumor.id,
- createdAt: template.created_at,
- content: canonical.content,
+ createdAt: rumor.created_at,
+ content: decoded.content,
subject: decoded.subject,
// Private parent hash only; no hint, URL or public lookup is authorized.
replyTo: decoded.reply_to?.id ?? null
diff --git a/web/src/lib/nostr/incoming-wire.ts b/web/src/lib/nostr/incoming-wire.ts
@@ -0,0 +1,202 @@
+import { getEventHash, type UnsignedEvent } from 'applesauce-core/helpers';
+import { PRIVATE_TRANSPORT_BUDGETS } from '../config/budgets.ts';
+import { canonicalPublicKey } from '../contracts/public-key.ts';
+import { exactLocalFields } from '../contracts/local-records.ts';
+import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts';
+import {
+ messageFromWireParts,
+ type MessageV1
+} from '../contracts/message-v1/index.ts';
+import { safeUnsignedInteger } from './envelope-bounds.ts';
+import { boundedEnvelopeTags } from './verified-envelope.ts';
+
+// Syntax-only duplicate detection precedes ordinary JSON semantics. Escaped
+// field aliases count as the same key; harmless whitespace stays supported.
+function uniqueJSONFields(raw: string): boolean {
+ type Frame = {
+ array: boolean;
+ key: boolean;
+ parent: Frame | undefined;
+ accept: (name: string) => boolean;
+ };
+ function frame(parent: Frame | undefined, array: boolean): Frame {
+ const names = new Set<string>();
+ return {
+ parent,
+ array,
+ key: !array,
+ accept: (name: string) => {
+ if (names.has(name)) return false;
+ names.add(name);
+ return true;
+ }
+ };
+ }
+ let current: Frame | undefined;
+ try {
+ for (let index = 0; index < raw.length; index++) {
+ const character = raw[index];
+ if (character === '"') {
+ let end = index + 1;
+ while (end < raw.length) {
+ if (raw[end] === '\\') {
+ end += 2;
+ continue;
+ }
+ if (raw[end] === '"') break;
+ end++;
+ }
+ if (end >= raw.length) return false;
+ if (current && !current.array && current.key) {
+ const name: unknown = JSON.parse(raw.slice(index, end + 1));
+ if (typeof name !== 'string' || !current.accept(name)) return false;
+ current = {
+ parent: current.parent,
+ array: current.array,
+ accept: current.accept,
+ key: false
+ };
+ }
+ index = end;
+ } else if (character === '{' || character === '[')
+ current = frame(current, character === '[');
+ else if (character === '}' || character === ']') {
+ if (!current) return false;
+ current = current.parent;
+ } else if (character === ',' && current && !current.array)
+ current = {
+ parent: current.parent,
+ array: current.array,
+ accept: current.accept,
+ key: true
+ };
+ }
+ return current === undefined;
+ } catch {
+ return false;
+ }
+}
+export function parseIncomingLayer(raw: unknown, bytes: number): unknown {
+ if (
+ typeof raw !== 'string' ||
+ !boundedUtf8(raw, bytes) ||
+ !uniqueJSONFields(raw)
+ )
+ return undefined;
+ try {
+ let invalid = false;
+ const parsed: unknown = JSON.parse(
+ raw,
+ function (key: string, value: unknown, context?: { source?: string }) {
+ if (
+ !key.isWellFormed() ||
+ (typeof value === 'string' && !value.isWellFormed())
+ )
+ invalid = true;
+ if (
+ typeof value === 'number' &&
+ (!Number.isFinite(value) ||
+ ((key === 'kind' || key === 'created_at') &&
+ (context?.source === undefined ||
+ !/^[0-9]+$/u.test(context.source))))
+ )
+ invalid = true;
+ return value;
+ }
+ );
+ return invalid ? undefined : parsed;
+ } catch {
+ return undefined;
+ }
+}
+// Receive-only syntax/projection. These detached values grant no signature,
+// nested custody, routing, persistence, publication or conversation authority.
+export function incomingGiftWrapTags(
+ tags: unknown,
+ expectedOwner: unknown
+): boolean {
+ const owner = canonicalPublicKey(expectedOwner);
+ if (
+ !owner ||
+ !boundedEnvelopeTags(tags) ||
+ tags.some((row) => row[0] === 'expiration')
+ )
+ return false;
+ const recipients = tags.filter((row) => row[0] === 'p');
+ return (
+ recipients.length === 1 &&
+ (recipients[0].length === 2 || recipients[0].length === 3) &&
+ recipients[0][1] === owner
+ );
+}
+
+export type IncomingPairRumor = Readonly<{
+ original: Readonly<UnsignedEvent & { id: string }>;
+ message: MessageV1;
+}>;
+
+// Hash the original ordered fields. Semantic projection is deliberately
+// independent of the strict outgoing writer; hints and unknown tags grant no effects.
+export function readIncomingPairRumor(
+ raw: unknown,
+ expectedSender?: unknown
+): IncomingPairRumor | undefined {
+ const parsed = parseIncomingLayer(raw, PRIVATE_TRANSPORT_BUDGETS.rumorBytes);
+ if (
+ !exactLocalFields(parsed, [
+ 'id',
+ 'pubkey',
+ 'created_at',
+ 'kind',
+ 'tags',
+ 'content'
+ ]) ||
+ parsed.kind !== 14 ||
+ typeof parsed.id !== 'string' ||
+ !/^[0-9a-f]{64}$/u.test(parsed.id) ||
+ safeUnsignedInteger(parsed.created_at) === undefined ||
+ !boundedEnvelopeTags(parsed.tags)
+ )
+ return undefined;
+ const sender = canonicalPublicKey(parsed.pubkey);
+ if (
+ !sender ||
+ (expectedSender !== undefined &&
+ canonicalPublicKey(expectedSender) !== sender)
+ )
+ return undefined;
+ const tags = parsed.tags;
+ // Unsupported retention/attachment semantics never become inert acceptance.
+ if (tags.some((row) => ['expiration', 'file', 'imeta'].includes(row[0])))
+ return undefined;
+ const recipients = tags.filter((row) => row[0] === 'p'),
+ parents = tags.filter((row) => row[0] === 'e'),
+ subjects = tags.filter((row) => row[0] === 'subject');
+ if (
+ recipients.length !== 1 ||
+ ![2, 3].includes(recipients[0].length) ||
+ parents.length > 1 ||
+ parents.some((row) => ![2, 3].includes(row.length)) ||
+ subjects.length > 1 ||
+ subjects.some((row) => row.length !== 2)
+ )
+ return undefined;
+ const decoded = messageFromWireParts(
+ JSON.stringify({ kind: 14, tags, content: parsed.content })
+ );
+ if (
+ !decoded ||
+ decoded.recipients.length !== 1 ||
+ decoded.recipients[0].public_key === sender
+ )
+ return undefined;
+ const template: UnsignedEvent = {
+ pubkey: sender,
+ kind: 14,
+ created_at: parsed.created_at as number,
+ tags: tags.map((row) => [...row]),
+ content: decoded.content
+ };
+ if (getEventHash(template) !== parsed.id) return undefined;
+ return { original: { id: parsed.id, ...template }, message: decoded };
+}
diff --git a/web/src/lib/nostr/unwrap-admission.ts b/web/src/lib/nostr/unwrap-admission.ts
@@ -1,13 +1,13 @@
import { ExtensionSigner } from 'applesauce-signers';
-import { getEventHash, type UnsignedEvent } from 'applesauce-core/helpers';
+import type { UnsignedEvent } from 'applesauce-core/helpers';
import { PRIVATE_TRANSPORT_BUDGETS } from '../config/budgets.ts';
import { canonicalPublicKey } from '../contracts/public-key.ts';
import { exactLocalFields } from '../contracts/local-records.ts';
-import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts';
import {
- messageFromWireParts,
- messageToWireParts
-} from '../contracts/message-v1/index.ts';
+ incomingGiftWrapTags,
+ parseIncomingLayer,
+ readIncomingPairRumor
+} from './incoming-wire.ts';
import {
identityMessagingOwnership,
subscribeIdentityInvalidation,
@@ -23,7 +23,6 @@ import {
privateRecordSnapshot,
privateRecordWire
} from '../persistence/private-records.ts';
-import { safeUnsignedInteger } from './envelope-bounds.ts';
import {
verifyEnvelope,
verifiedEnvelopeSnapshot
@@ -47,111 +46,15 @@ const unsignedFields = [
'content'
];
const signedFields = [...unsignedFields, 'sig'];
-// Syntax-only duplicate detection precedes ordinary JSON semantics. Escaped
-// field aliases count as the same key; harmless whitespace stays supported.
-function uniqueJSONFields(raw: string): boolean {
- type Frame = {
- array: boolean;
- key: boolean;
- parent: Frame | undefined;
- accept: (name: string) => boolean;
- };
- function frame(parent: Frame | undefined, array: boolean): Frame {
- const names = new Set<string>();
- return {
- parent,
- array,
- key: !array,
- accept: (name: string) => {
- if (names.has(name)) return false;
- names.add(name);
- return true;
- }
- };
- }
- let current: Frame | undefined;
- try {
- for (let index = 0; index < raw.length; index++) {
- const character = raw[index];
- if (character === '"') {
- let end = index + 1;
- while (end < raw.length) {
- if (raw[end] === '\\') {
- end += 2;
- continue;
- }
- if (raw[end] === '"') break;
- end++;
- }
- if (end >= raw.length) return false;
- if (current && !current.array && current.key) {
- const name: unknown = JSON.parse(raw.slice(index, end + 1));
- if (typeof name !== 'string' || !current.accept(name)) return false;
- current = {
- parent: current.parent,
- array: current.array,
- accept: current.accept,
- key: false
- };
- }
- index = end;
- } else if (character === '{' || character === '[')
- current = frame(current, character === '[');
- else if (character === '}' || character === ']') {
- if (!current) return false;
- current = current.parent;
- } else if (character === ',' && current && !current.array)
- current = {
- parent: current.parent,
- array: current.array,
- accept: current.accept,
- key: true
- };
- }
- return current === undefined;
- } catch {
- return false;
- }
-}
-function layer(raw: unknown, bytes: number): unknown {
- if (
- typeof raw !== 'string' ||
- !boundedUtf8(raw, bytes) ||
- !uniqueJSONFields(raw)
- )
- return undefined;
- try {
- let invalid = false;
- const parsed: unknown = JSON.parse(
- raw,
- function (key: string, value: unknown, context?: { source?: string }) {
- if (
- !key.isWellFormed() ||
- (typeof value === 'string' && !value.isWellFormed())
- )
- invalid = true;
- if (
- typeof value === 'number' &&
- (!Number.isFinite(value) ||
- ((key === 'kind' || key === 'created_at') &&
- (context?.source === undefined ||
- !/^[0-9]+$/u.test(context.source))))
- )
- invalid = true;
- return value;
- }
- );
- return invalid ? undefined : parsed;
- } catch {
- return undefined;
- }
-}
function outerLayer(raw: unknown, owner: string) {
- const parsed = layer(raw, PRIVATE_TRANSPORT_BUDGETS.envelopeBytes);
+ const parsed = parseIncomingLayer(
+ raw,
+ PRIVATE_TRANSPORT_BUDGETS.envelopeBytes
+ );
if (
!exactLocalFields(parsed, signedFields) ||
parsed.kind !== 1059 ||
- JSON.stringify(parsed.tags) !== JSON.stringify([['p', owner]]) ||
+ !incomingGiftWrapTags(parsed.tags, owner) ||
!sealCiphertextV2(parsed.content)
)
return undefined;
@@ -159,7 +62,7 @@ function outerLayer(raw: unknown, owner: string) {
return verified.ok ? verifiedEnvelopeSnapshot(verified.value) : undefined;
}
function sealLayer(raw: unknown) {
- const parsed = layer(raw, PRIVATE_TRANSPORT_BUDGETS.sealBytes);
+ const parsed = parseIncomingLayer(raw, PRIVATE_TRANSPORT_BUDGETS.sealBytes);
if (
!exactLocalFields(parsed, signedFields) ||
parsed.kind !== 13 ||
@@ -175,38 +78,7 @@ function rumorLayer(
raw: unknown,
sender: string
): Readonly<UnsignedEvent & { id: string }> | undefined {
- const parsed = layer(raw, PRIVATE_TRANSPORT_BUDGETS.rumorBytes);
- if (
- !exactLocalFields(parsed, unsignedFields) ||
- parsed.kind !== 14 ||
- parsed.pubkey !== sender ||
- canonicalPublicKey(parsed.pubkey) !== sender ||
- safeUnsignedInteger(parsed.created_at) === undefined ||
- typeof parsed.id !== 'string' ||
- !/^[0-9a-f]{64}$/u.test(parsed.id)
- )
- return undefined;
- const parts = { kind: 14, tags: parsed.tags, content: parsed.content };
- const decoded = messageFromWireParts(JSON.stringify(parts));
- if (
- !decoded ||
- decoded.recipients.length !== 1 ||
- decoded.recipients[0].public_key === sender
- )
- return undefined;
- const canonical = messageToWireParts(JSON.stringify(decoded));
- if (!canonical || JSON.stringify(canonical) !== JSON.stringify(parts))
- return undefined;
- const template: UnsignedEvent = {
- pubkey: sender,
- kind: 14,
- created_at: parsed.created_at as number,
- tags: canonical.tags.map((row) => [...row]),
- content: canonical.content
- };
- return getEventHash(template) === parsed.id
- ? { id: parsed.id, ...template }
- : undefined;
+ return readIncomingPairRumor(raw, sender)?.original;
}
// Detached inspection only: supplied plaintext cannot prove ciphertext
// correspondence. Only the scoped actual SDK issuer below mints nested custody.
diff --git a/web/src/lib/persistence/private-records.ts b/web/src/lib/persistence/private-records.ts
@@ -8,6 +8,7 @@ import {
verifiedEnvelopeSnapshot
} from '../nostr/verified-envelope.ts';
import { canonicalRelayOrigin } from '../config/relays.ts';
+import { incomingGiftWrapTags } from '../nostr/incoming-wire.ts';
import type { InboxRouteSnapshot } from '../messaging/inbox-routing.ts';
import {
LOCAL_PERSISTENCE_BUDGETS,
@@ -326,7 +327,11 @@ export function inspectPrivateSendReservation(
return undefined;
}
}
-function outer(raw: unknown, destination: string): string | undefined {
+function outer(
+ raw: unknown,
+ destination: string,
+ received = false
+): string | undefined {
if (
typeof raw !== 'string' ||
!boundedUtf8(raw, PRIVATE_TRANSPORT_BUDGETS.envelopeBytes)
@@ -348,7 +353,9 @@ function outer(raw: unknown, destination: string): string | undefined {
'sig'
]) ||
event.kind !== 1059 ||
- JSON.stringify(event.tags) !== JSON.stringify([['p', destination]])
+ !(received
+ ? incomingGiftWrapTags(event.tags, destination)
+ : JSON.stringify(event.tags) === JSON.stringify([['p', destination]]))
)
return undefined;
// Structural standard-v2 preflight only. No SDK decrypt or nested admission;
@@ -539,7 +546,7 @@ export function decodePrivateRecord(
'read'
]) ||
!unsigned(fields.revision) ||
- outer(fields.outer, owner) !== id ||
+ outer(fields.outer, owner, true) !== id ||
!unsigned(fields.observedAtMilliseconds) ||
!Array.isArray(fields.sources) ||
fields.sources.length === 0 ||
diff --git a/web/tests/conformance/private-hostile.test.ts b/web/tests/conformance/private-hostile.test.ts
@@ -170,10 +170,10 @@ await test('finite adversarial nested matrix refuses altered IDs, signatures, au
15
],
[
- 'unknown-tag',
+ 'unsupported-expiration',
[
['p', f.owner],
- ['order', 'unsupported']
+ ['expiration', '1700000001']
],
14
]
@@ -190,6 +190,23 @@ await test('finite adversarial nested matrix refuses altered IDs, signatures, au
});
}
assert.equal(candidates.length, 40);
+ // D09 accepts this former negative as inert incoming metadata. Detached
+ // parsing proves no ciphertext correspondence, order flow or effect authority.
+ const inert: UnsignedEvent = {
+ ...f.rumor,
+ tags: [
+ ['p', f.owner],
+ ['order', 'unsupported']
+ ]
+ };
+ assert.equal(
+ f.check([
+ f.wire[0],
+ f.wire[1],
+ JSON.stringify({ ...inert, id: getEventHash(inert) })
+ ]),
+ true
+ );
for (const row of candidates) {
const values = [...f.wire];
values[row.layer] = row.value;
diff --git a/web/tests/conformance/received-envelopes.test.ts b/web/tests/conformance/received-envelopes.test.ts
@@ -9,7 +9,7 @@ import {
} from 'applesauce-core/helpers';
import { nip44 } from 'applesauce-core/helpers/encryption';
import { verifyReceivedLayerData } from '../../src/lib/nostr/unwrap-admission.ts';
-function fixture() {
+function fixture(extended = false) {
const keys = [generateSecretKey(), generateSecretKey(), generateSecretKey()],
sender = getPublicKey(keys[0]),
owner = getPublicKey(keys[1]);
@@ -17,7 +17,15 @@ function fixture() {
pubkey: sender,
kind: 14,
created_at: 1700000000,
- tags: [['p', owner]],
+ tags: extended
+ ? [
+ ['subject', 'Original subject'],
+ ['q', 'b'.repeat(64), 'wss://untrusted.example.org', sender],
+ ['vendor', 'inert metadata'],
+ ['e', 'a'.repeat(64), 'wss://untrusted.example.org'],
+ ['p', owner, 'wss://untrusted.example.org']
+ ]
+ : [['p', owner]],
content: 'Bounded inbound enquiry'
};
const rumor = { id: getEventHash(template), ...template };
@@ -43,7 +51,12 @@ function fixture() {
{
kind: 1059,
created_at: 1699999800,
- tags: [['p', owner]],
+ tags: extended
+ ? [
+ ['nonce', '3', '8'],
+ ['p', owner, 'wss://untrusted.example.org']
+ ]
+ : [['p', owner]],
content: outerContent
},
keys[2]
@@ -85,6 +98,56 @@ await vector(
'actual signed layers and freshly hashed unsigned pair rumor pass detached inspection',
(f) => assert.equal(f.check(), true)
);
+await test('fresh SDK nested extended wires retain original hashes and reject ambiguous/unsupported routing', () => {
+ const f = fixture(true);
+ try {
+ assert.equal(f.check(), true);
+ assert.equal(getEventHash(f.rumor), f.rumor.id);
+ for (const extra of [
+ ['p', f.owner],
+ ['p', f.sender],
+ ['subject', 'Another subject'],
+ ['e', 'c'.repeat(64)],
+ ['expiration', '1700000001'],
+ ['file', 'https://untrusted.example.org'],
+ ['imeta', 'url https://untrusted.example.org']
+ ]) {
+ const rumor = { ...f.rumor, tags: [...f.rumor.tags, extra] };
+ rumor.id = getEventHash(rumor);
+ assert.equal(f.check(rumor), false);
+ }
+ for (const tags of [
+ [
+ ['p', f.owner],
+ ['p', f.owner]
+ ],
+ [
+ ['p', f.owner],
+ ['p', f.sender]
+ ],
+ [['p', f.sender, 'wss://untrusted.example.org']],
+ [
+ ['p', f.owner],
+ ['expiration', '1700000001']
+ ]
+ ])
+ assert.equal(
+ f.check(
+ f.rumor,
+ f.seal,
+ finalizeEvent({ ...f.outer, tags }, f.keys[2])
+ ),
+ false
+ );
+ assert.equal(
+ f.check({ ...f.rumor, tags: [...f.rumor.tags].reverse() }),
+ false
+ );
+ assert.equal(f.check(f.rumor, { ...f.seal, sig: '0'.repeat(128) }), false);
+ } finally {
+ f.close();
+ }
+});
await vector(
'forged inner author fails even after recomputing rumor hash',
(f) => {
@@ -154,6 +217,32 @@ await vector(
false
)
);
+await vector(
+ 'signed original layers accept exact raw UTF8 byte limits and reject one-byte excess',
+ (f) => {
+ const wires = [
+ JSON.stringify(f.outer),
+ JSON.stringify(f.seal),
+ JSON.stringify(f.rumor)
+ ];
+ const limits = [32768, 16384, 8192];
+ for (let index = 0; index < wires.length; index++) {
+ const boundary = [...wires];
+ boundary[index] += ' '.repeat(
+ limits[index] - Buffer.byteLength(boundary[index])
+ );
+ assert.equal(
+ verifyReceivedLayerData(boundary[0], boundary[1], boundary[2], f.owner),
+ true
+ );
+ boundary[index] += ' ';
+ assert.equal(
+ verifyReceivedLayerData(boundary[0], boundary[1], boundary[2], f.owner),
+ false
+ );
+ }
+ }
+);
await vector('body and layer bounds are checked without truncation', (f) => {
const r = { ...f.rumor, content: 'x'.repeat(4097) };
r.id = getEventHash(r);
diff --git a/web/tests/e2e/harness/room-admission.ts b/web/tests/e2e/harness/room-admission.ts
@@ -29,6 +29,13 @@ import {
type Scenario =
| 'inbound'
| 'self_archive'
+ | 'inbound_extensions'
+ | 'self_archive_extensions'
+ | 'duplicate_recipient'
+ | 'duplicate_subject'
+ | 'duplicate_parent'
+ | 'expiration'
+ | 'file'
| 'third_party'
| 'hostile_text'
| 'missing_parent';
@@ -43,18 +50,35 @@ export async function makeFixture(scenario: Scenario = 'inbound') {
peerSecret = new Uint8Array(32).fill(84),
outerSecret = new Uint8Array(32).fill(85),
other = getPublicKey(outerSecret);
- const senderSecret = scenario === 'self_archive' ? ownerSecret : peerSecret,
+ const archive =
+ scenario === 'self_archive' || scenario === 'self_archive_extensions',
+ extensions =
+ scenario === 'inbound_extensions' ||
+ scenario === 'self_archive_extensions';
+ const senderSecret = archive ? ownerSecret : peerSecret,
sender = getPublicKey(senderSecret),
- recipient =
- scenario === 'self_archive'
- ? f.peer
- : scenario === 'third_party'
- ? other
- : f.owner;
+ recipient = archive ? f.peer : scenario === 'third_party' ? other : f.owner;
const tags: string[][] = [['p', recipient]];
+ if (scenario === 'duplicate_recipient') tags.push(['p', recipient]);
+ if (scenario === 'duplicate_subject')
+ tags.push(['subject', 'A'], ['subject', 'B']);
+ if (scenario === 'duplicate_parent')
+ tags.push(['e', 'a'.repeat(64)], ['e', 'b'.repeat(64)]);
+ if (scenario === 'expiration') tags.push(['expiration', '1700000001']);
+ if (scenario === 'file') tags.push(['file', 'https://untrusted.example.org']);
if (scenario === 'hostile_text') tags.push(['subject', other]);
if (scenario === 'missing_parent')
tags.push(['e', 'a'.repeat(64), 'wss://untrusted.example.org']);
+ if (extensions)
+ tags.splice(
+ 0,
+ tags.length,
+ ['subject', 'Original subject'],
+ ['q', 'b'.repeat(64), 'wss://untrusted.example.org', other],
+ ['vendor', 'untrusted inert metadata'],
+ ['e', 'a'.repeat(64), 'wss://untrusted.example.org'],
+ ['p', recipient, 'wss://untrusted.example.org']
+ );
const template: UnsignedEvent = {
pubkey: sender,
kind: 14,
@@ -87,7 +111,12 @@ export async function makeFixture(scenario: Scenario = 'inbound') {
{
kind: 1059,
created_at: 1699999800,
- tags: [['p', f.owner]],
+ tags: extensions
+ ? [
+ ['nonce', '3', '8'],
+ ['p', f.owner, 'wss://untrusted.example.org']
+ ]
+ : [['p', f.owner]],
content: encrypt(outerSecret, f.owner, JSON.stringify(seal))
},
outerSecret
diff --git a/web/tests/e2e/room-admission.spec.ts b/web/tests/e2e/room-admission.spec.ts
@@ -72,6 +72,69 @@ test('actual SDK-authenticated inbound selects sender and rejects forged or copi
expect(r.forged).toBeUndefined();
expect(r.copied).toBeUndefined();
});
+
+for (const [scenario, role] of [
+ ['inbound_extensions', 'inbound'],
+ ['self_archive_extensions', 'self_archive']
+] as const)
+ test(`actual received ${scenario} retains original identity across hints/q/tag-order projection`, async ({
+ page
+ }) => {
+ const sockets: string[] = [];
+ page.on('websocket', (socket) => sockets.push(socket.url()));
+ const result = await page.evaluate(
+ async ({ scenario, role }) => {
+ const f = (window.hcp092Fixture =
+ await window.hcp092.makeFixture(scenario));
+ return {
+ unlocked: await f.unlock(),
+ room: f.admit(role),
+ id: f.rumorId,
+ owner: f.owner,
+ peer: f.peer,
+ opposite: f.admit(role === 'inbound' ? 'self_archive' : 'inbound')
+ };
+ },
+ { scenario, role }
+ );
+ expect(result.unlocked).toBe('authenticated');
+ expect(result.room).toMatchObject({
+ owner: result.owner,
+ peer: result.peer,
+ role,
+ rumorId: result.id,
+ content: 'Actual encrypted room fixture',
+ subject: 'Original subject',
+ replyTo: 'a'.repeat(64)
+ });
+ expect(result.opposite).toBeUndefined();
+ expect(sockets.some((url) => url.includes('untrusted.example.org'))).toBe(
+ false
+ );
+ });
+for (const scenario of [
+ 'duplicate_recipient',
+ 'duplicate_subject',
+ 'duplicate_parent',
+ 'expiration',
+ 'file'
+] as const)
+ test(`fresh genuine encryption cannot admit unsupported/ambiguous ${scenario}`, async ({
+ page
+ }) => {
+ const result = await page.evaluate(async (scenario) => {
+ const f = (window.hcp092Fixture =
+ await window.hcp092.makeFixture(scenario));
+ return {
+ unlocked: await f.unlock(),
+ inbound: f.admit('inbound'),
+ archive: f.admit('self_archive')
+ };
+ }, scenario);
+ expect(result.unlocked).toBe('mismatch');
+ expect(result.inbound).toBeUndefined();
+ expect(result.archive).toBeUndefined();
+ });
test('actual authenticated self archive preserves original peer recipient and rumor id', async ({
page
}) => {
diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs
@@ -1950,7 +1950,8 @@ import { auditOutput } from '../../tools/check-output.mjs';
'src/lib/messaging/inbox-setup-view.ts',
'src/lib/messaging/reply-controller.ts',
'src/lib/messaging/product-request-controller.ts',
- 'src/lib/nostr/inbox-preference-publication.ts'
+ 'src/lib/nostr/inbox-preference-publication.ts',
+ 'src/lib/nostr/incoming-wire.ts'
]) {
await check(
'operational flags rejects changed owned producer ' + producer,
diff --git a/web/tests/unit/incoming-wire.test.ts b/web/tests/unit/incoming-wire.test.ts
@@ -0,0 +1,253 @@
+import { expect, it } from 'vitest';
+import { getEventHash, type UnsignedEvent } from 'applesauce-core/helpers';
+import {
+ incomingGiftWrapTags,
+ parseIncomingLayer,
+ readIncomingPairRumor
+} from '../../src/lib/nostr/incoming-wire.ts';
+import { inspectConversationData } from '../../src/lib/messaging/admit-conversation.ts';
+import { messageToWireParts } from '../../src/lib/contracts/message-v1/index.ts';
+const sender =
+ '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
+ owner = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5';
+function rumor(tags: string[][], content = ' Original readable question\n') {
+ const template: UnsignedEvent = {
+ kind: 14,
+ pubkey: sender,
+ created_at: 1700000000,
+ tags,
+ content
+ };
+ return { id: getEventHash(template), ...template };
+}
+it('accepts bounded intended-recipient hints and inert outer metadata without routing authority', () => {
+ for (const tags of [
+ [['p', owner]],
+ [['p', owner, 'wss://untrusted.example.org']],
+ [
+ ['nonce', '7', '8'],
+ ['p', owner, ''],
+ ['vendor', 'untrusted inert text']
+ ]
+ ])
+ expect(incomingGiftWrapTags(tags, owner)).toBe(true);
+ for (const tags of [
+ [],
+ [['p', sender]],
+ [
+ ['p', owner],
+ ['p', owner]
+ ],
+ [
+ ['p', owner],
+ ['p', sender]
+ ],
+ [['p', owner, 'hint', 'extra']],
+ [
+ ['p', owner],
+ ['expiration', '1900000000']
+ ],
+ [['p', owner], []],
+ [
+ ['p', owner],
+ ['vendor', 'x'.repeat(4097)]
+ ],
+ [['p', owner], ...Array.from({ length: 1024 }, () => ['vendor'])],
+ [
+ ['p', owner],
+ ['vendor', '\ud800']
+ ]
+ ])
+ expect(incomingGiftWrapTags(tags, owner)).toBe(false);
+});
+it('hashes original tag order and preserves bounded q/unknown metadata while projecting semantics', () => {
+ const tags = [
+ ['subject', 'Question'],
+ ['q', 'b'.repeat(64), 'wss://untrusted.example.org', sender],
+ ['vendor', 'untrusted inert text'],
+ ['e', 'a'.repeat(64), 'wss://parent.example.org'],
+ ['p', owner, 'wss://recipient.example.org']
+ ];
+ const value = rumor(tags),
+ raw = '\n' + JSON.stringify(value, null, 2) + '\n';
+ const incoming = readIncomingPairRumor(raw, sender);
+ expect(incoming?.original).toEqual(value);
+ expect(incoming?.message).toEqual({
+ recipients: [
+ { public_key: owner, relay_url: 'wss://recipient.example.org' }
+ ],
+ content: value.content,
+ reply_to: { id: 'a'.repeat(64), relays: 'wss://parent.example.org' },
+ subject: 'Question'
+ });
+ expect(inspectConversationData(raw, owner, 'inbound')).toMatchObject({
+ owner,
+ peer: sender,
+ sender,
+ recipient: owner,
+ rumorId: value.id,
+ content: value.content,
+ subject: 'Question',
+ replyTo: 'a'.repeat(64)
+ });
+ expect(inspectConversationData(raw, sender, 'self_archive')).toMatchObject({
+ peer: owner,
+ rumorId: value.id,
+ content: value.content
+ });
+ expect(inspectConversationData(raw, owner, 'self_archive')).toBeUndefined();
+ expect(
+ inspectConversationData(raw, '0'.repeat(64), 'inbound')
+ ).toBeUndefined();
+ // The receive profile does not rewrite the strict Lib189 outgoing writer.
+ expect(messageToWireParts(JSON.stringify(incoming?.message))?.tags).toEqual([
+ ['p', owner, 'wss://recipient.example.org'],
+ ['e', 'a'.repeat(64), 'wss://parent.example.org'],
+ ['subject', 'Question']
+ ]);
+});
+it('rejects ambiguous recipients/parents/subjects and unsupported retention/file semantics', () => {
+ const badTags = [
+ [],
+ [['p', sender]],
+ [
+ ['p', owner],
+ ['p', owner]
+ ],
+ [
+ ['p', owner],
+ ['p', sender]
+ ],
+ [['p', owner, 'hint', 'extra']],
+ [
+ ['p', owner],
+ ['subject', 'A'],
+ ['subject', 'B']
+ ],
+ [
+ ['p', owner],
+ ['subject', 'A', 'extra']
+ ],
+ [
+ ['p', owner],
+ ['e', 'a'.repeat(64)],
+ ['e', 'b'.repeat(64)]
+ ],
+ [
+ ['p', owner],
+ ['e', 'a'.repeat(64), 'hint', 'marker']
+ ],
+ [
+ ['p', owner],
+ ['e', 'malformed']
+ ],
+ [
+ ['p', owner],
+ ['expiration', '1900000000']
+ ],
+ [
+ ['p', owner],
+ ['file', 'untrusted']
+ ],
+ [
+ ['p', owner],
+ ['imeta', 'untrusted']
+ ],
+ [
+ ['p', owner],
+ ['vendor', '\ud800']
+ ]
+ ];
+ for (const tags of badTags)
+ expect(readIncomingPairRumor(JSON.stringify(rumor(tags)))).toBeUndefined();
+});
+it('altered original tags/hash/author/kind and noncanonical numeric spellings never grant admission', () => {
+ const value = rumor([
+ ['subject', 'Question'],
+ ['p', owner]
+ ]);
+ const raw = JSON.stringify(value);
+ for (const modified of [
+ { ...value, id: '0'.repeat(64) },
+ { ...value, tags: [...value.tags].reverse() },
+ { ...value, pubkey: owner },
+ { ...value, kind: 15 },
+ { ...value, sig: '0'.repeat(128) },
+ { ...value, verified: true },
+ { ...value, content: 'x'.repeat(4097) }
+ ])
+ expect(readIncomingPairRumor(JSON.stringify(modified))).toBeUndefined();
+ expect(readIncomingPairRumor(raw, owner)).toBeUndefined();
+ for (const modified of [
+ raw.replace('"kind":14', '"kind":14.0'),
+ raw.replace('1700000000', '1700000000.0'),
+ raw.replace('1700000000', '1.7e9'),
+ '{"kind":14,' + raw.slice(1),
+ '{"\\u006bkind":14,' + raw.slice(1),
+ '{"\\u006b\\u0069\\u006e\\u0064":14,' + raw.slice(1)
+ ]) {
+ expect(readIncomingPairRumor(modified)).toBeUndefined();
+ expect(inspectConversationData(modified, owner, 'inbound')).toBeUndefined();
+ }
+});
+it('accepts exact body/tag/element limits and rejects their immediate excess', () => {
+ expect(
+ readIncomingPairRumor(
+ JSON.stringify(rumor([['p', owner]], 'x'.repeat(4096)))
+ )
+ ).toBeDefined();
+ expect(
+ readIncomingPairRumor(
+ JSON.stringify(rumor([['p', owner]], 'x'.repeat(4097)))
+ )
+ ).toBeUndefined();
+ const count = [['p', owner], ...Array.from({ length: 1023 }, () => ['x'])];
+ expect(readIncomingPairRumor(JSON.stringify(rumor(count)))).toBeDefined();
+ expect(incomingGiftWrapTags(count, owner)).toBe(true);
+ expect(incomingGiftWrapTags([...count, ['x']], owner)).toBe(false);
+ const elements = [
+ ['p', owner, ''],
+ ['vendor', '', '', '', ''],
+ ...Array.from({ length: 1022 }, () => ['vendor', '', '', ''])
+ ];
+ expect(incomingGiftWrapTags(elements, owner)).toBe(true);
+ expect(
+ incomingGiftWrapTags(
+ elements.map((row, i) => (i === 1 ? [...row, ''] : row)),
+ owner
+ )
+ ).toBe(false);
+ expect(
+ incomingGiftWrapTags(
+ [
+ ['p', owner],
+ ['vendor', 'x'.repeat(4096)]
+ ],
+ owner
+ )
+ ).toBe(true);
+ expect(
+ incomingGiftWrapTags(
+ [
+ ['p', owner],
+ ['vendor', 'x'.repeat(4097)]
+ ],
+ owner
+ )
+ ).toBe(false);
+});
+it('raw objects cannot invoke conversion and layer/key/UTF8 limits remain defensive', () => {
+ let calls = 0;
+ const input = {
+ toJSON() {
+ calls++;
+ return rumor([['p', owner]]);
+ }
+ };
+ expect(readIncomingPairRumor(input)).toBeUndefined();
+ expect(parseIncomingLayer(input, 8192)).toBeUndefined();
+ expect(calls).toBe(0);
+ expect(parseIncomingLayer('x'.repeat(8193), 8192)).toBeUndefined();
+ expect(parseIncomingLayer('{"a":1,"a":2}', 8192)).toBeUndefined();
+ expect(parseIncomingLayer('{"a":"\\ud800"}', 8192)).toBeUndefined();
+});
diff --git a/web/tests/unit/private-records.test.ts b/web/tests/unit/private-records.test.ts
@@ -12,7 +12,7 @@ import {
inspectPrivateSendReservation
} from '../../src/lib/persistence/private-records.ts';
const command = '12345678-1234-4234-8234-123456789abc';
-function fixture() {
+function fixture(extended = false) {
const key = generateSecretKey(),
peerKey = generateSecretKey();
let conversation: Uint8Array | undefined,
@@ -25,7 +25,12 @@ function fixture() {
{
kind: 1059,
created_at: 1700000000,
- tags: [['p', owner]],
+ tags: extended
+ ? [
+ ['nonce', '3', '8'],
+ ['p', owner, 'wss://untrusted.example.org']
+ ]
+ : [['p', owner]],
content: nip44.v2.encrypt(
'Ciphertext structure fixture only',
conversation
@@ -87,6 +92,23 @@ function fixture() {
}
}
describe('ciphertext-only private local codecs, not nested authenticity or effect authority', () => {
+ it('retains original receive hints/inert tags but keeps stored outgoing artifacts strict', () => {
+ const f = fixture(true),
+ decoded = decodePrivateRecord(
+ JSON.stringify(f.received),
+ f.owner,
+ f.received.id
+ );
+ expect(decoded.ok).toBe(true);
+ if (!decoded.ok) throw Error('bounded extended received wire rejected');
+ expect(
+ privateRecordSnapshot(decoded.value, f.owner, f.received.id)
+ ).toEqual(f.received);
+ expect(
+ decodePrivateRecord(JSON.stringify(f.operation), f.owner, f.operation.id)
+ .ok
+ ).toBe(false);
+ });
it('rejects duplicate or escaped-alias outer content fields instead of persisting hidden plaintext', () => {
const f = fixture();
for (const name of ['"content"', '"\\u0063ontent"']) {
diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs
@@ -301,7 +301,9 @@ async function operationalFlagsAdmission(web) {
'src/lib/contracts/public-key.ts':
'd36d4001c2f4835e24d50f00460a35c9cf9fa6b86cc3b4a6937d8b4e10afdeb9',
'src/lib/messaging/admit-conversation.ts':
- '3e1b0044c2269eb62fa0485db61d3159e4e779e36c309278863d56cba9f677c3',
+ '1ad085aa530beeabbc9488676e77cf7cd0901e61e7705471f79a47bb1df70cf1',
+ 'src/lib/nostr/incoming-wire.ts':
+ 'd471dde674b31a62961bb6fcb503cd9e54791d83c7afdaabcb74686cc78a3723',
'src/lib/messaging/cited-product.ts':
'bed076c0902f6453971f88f107766c331b5737e488cb8c3c5f6c941e3428778d',
'src/lib/messaging/conversation-thread.ts':
@@ -367,7 +369,7 @@ async function operationalFlagsAdmission(web) {
'src/lib/persistence/local-public-hides.ts':
'4f0e64ed0fabbe9f86014a0512e7dbd3c81711f28ca2c626e2dafa5cda349f83',
'src/lib/persistence/private-records.ts':
- '5892589a11e7009d4f4c27620a7da7ca34913552d9aa513431941050742ff386',
+ '3389fe2fc5b9b84b292b4340fe1edda16a2eb365c1b0fb00d965827dd9b02ce6',
'src/lib/persistence/private-storage.ts':
'e45a95c9b12e8534a6ab0a4ec8ee1c86e730bba2f252dc1947954439720f2bfc',
'src/lib/persistence/public-publications.ts':
@@ -545,7 +547,9 @@ async function privacyDataAdmission(web) {
'src/lib/contracts/public-key.ts':
'd36d4001c2f4835e24d50f00460a35c9cf9fa6b86cc3b4a6937d8b4e10afdeb9',
'src/lib/messaging/admit-conversation.ts':
- '3e1b0044c2269eb62fa0485db61d3159e4e779e36c309278863d56cba9f677c3',
+ '1ad085aa530beeabbc9488676e77cf7cd0901e61e7705471f79a47bb1df70cf1',
+ 'src/lib/nostr/incoming-wire.ts':
+ 'd471dde674b31a62961bb6fcb503cd9e54791d83c7afdaabcb74686cc78a3723',
'src/lib/messaging/cited-product.ts':
'bed076c0902f6453971f88f107766c331b5737e488cb8c3c5f6c941e3428778d',
'src/lib/messaging/conversation-thread.ts':
@@ -603,7 +607,7 @@ async function privacyDataAdmission(web) {
'src/lib/persistence/local-public-hides.ts':
'4f0e64ed0fabbe9f86014a0512e7dbd3c81711f28ca2c626e2dafa5cda349f83',
'src/lib/persistence/private-records.ts':
- '5892589a11e7009d4f4c27620a7da7ca34913552d9aa513431941050742ff386',
+ '3389fe2fc5b9b84b292b4340fe1edda16a2eb365c1b0fb00d965827dd9b02ce6',
'src/lib/persistence/private-storage.ts':
'e45a95c9b12e8534a6ab0a4ec8ee1c86e730bba2f252dc1947954439720f2bfc',
'src/lib/persistence/public-publications.ts':
@@ -750,7 +754,9 @@ async function supportReportingAdmission(web) {
'src/lib/contracts/public-key.ts':
'd36d4001c2f4835e24d50f00460a35c9cf9fa6b86cc3b4a6937d8b4e10afdeb9',
'src/lib/messaging/admit-conversation.ts':
- '3e1b0044c2269eb62fa0485db61d3159e4e779e36c309278863d56cba9f677c3',
+ '1ad085aa530beeabbc9488676e77cf7cd0901e61e7705471f79a47bb1df70cf1',
+ 'src/lib/nostr/incoming-wire.ts':
+ 'd471dde674b31a62961bb6fcb503cd9e54791d83c7afdaabcb74686cc78a3723',
'src/lib/messaging/cited-product.ts':
'bed076c0902f6453971f88f107766c331b5737e488cb8c3c5f6c941e3428778d',
'src/lib/messaging/conversation-thread.ts':
@@ -933,7 +939,9 @@ async function privateBlockingAdmission(web) {
'src/lib/contracts/public-key.ts':
'd36d4001c2f4835e24d50f00460a35c9cf9fa6b86cc3b4a6937d8b4e10afdeb9',
'src/lib/messaging/admit-conversation.ts':
- '3e1b0044c2269eb62fa0485db61d3159e4e779e36c309278863d56cba9f677c3',
+ '1ad085aa530beeabbc9488676e77cf7cd0901e61e7705471f79a47bb1df70cf1',
+ 'src/lib/nostr/incoming-wire.ts':
+ 'd471dde674b31a62961bb6fcb503cd9e54791d83c7afdaabcb74686cc78a3723',
'src/lib/messaging/cited-product.ts':
'bed076c0902f6453971f88f107766c331b5737e488cb8c3c5f6c941e3428778d',
'src/lib/messaging/conversation-thread.ts':