Add submission relay client support #121
No files matched your search
@@ -38,7 +38,20 @@ const DIRECTORY_RELAYS = [
|
||||
];
|
||||
|
||||
const DEFAULT_READ_RELAYS = ['wss://nostr.kosmos.org'];
|
||||
const DEFAULT_WRITE_RELAYS = []; // TODO nostr.kosmos.org/marco
|
||||
|
||||
// Public, open-write submission relay (see doc/nostr/relay-trust.md).
|
||||
// Marco reads from it as an UNTRUSTED *configured* relay so freshly submitted
|
||||
// content can be fetched and revealed on demand, but it is deliberately kept
|
||||
// out of `requiredReadRelays` (which feeds `trustedRelays`). Content found here
|
||||
// stays untrusted until it is also seen on a trusted relay such as
|
||||
// nostr.kosmos.org. Users can remove it via `nostrReadRelayExclusions`.
|
||||
const SUBMISSION_RELAY_URL = 'wss://submit.nostr.kosmos.org';
|
||||
const DEFAULT_UNTRUSTED_READ_RELAYS = [SUBMISSION_RELAY_URL];
|
||||
|
||||
// Photos (and, later, reviews) are published to the submission relay, which
|
||||
// the moderation service promotes to the trusted Kosmos relay. Kept in sync
|
||||
// with the untrusted read relay above.
|
||||
const DEFAULT_WRITE_RELAYS = [SUBMISSION_RELAY_URL];
|
||||
|
||||
// Content event kinds subject to relay-trust filtering. Adding `30360`
|
||||
// (place reviews) here is the only change needed to extend the feature.
|
||||
@@ -47,6 +60,11 @@ const TRUSTED_CONTENT_KINDS = [360];
|
||||
// localforage store name for persisted relay provenance.
|
||||
const PROVENANCE_STORE = 'event-relay-provenance';
|
||||
|
||||
// Upper bound for the in-memory eventId -> author map used by the kind-5
|
||||
// author check. Evicting an entry just means a later deletion's authorship is
|
||||
// unverifiable, which is treated as "no match" (the fail-safe default).
|
||||
export const MAX_TRACKED_EVENT_AUTHORS = 10_000;
|
||||
|
||||
// Replicates applesauce-relay's `completeWhen`: completes the source when the
|
||||
// `operator` emits a truthy value. `completeWhen` is used internally by
|
||||
// RelayGroup but not re-exported, so we recreate it from rxjs primitives.
|
||||
@@ -80,6 +98,10 @@ export default class NostrDataService extends Service {
|
||||
// later arrives from a trusted one). Hydrated from IDB at init so cached
|
||||
// events render with correct trust state instantly.
|
||||
_eventRelays = new Map();
|
||||
// Maps eventId -> author pubkey for events inserted into the store. Used to
|
||||
// enforce NIP-09 authorship on kind-5 deletions (a deletion is only honored
|
||||
// when its author matches the referenced event's author).
|
||||
_eventAuthors = new Map();
|
||||
_provenanceReady = null;
|
||||
|
||||
// Set of pubkeys from the user's follow list (kind 3 contacts) for O(1)
|
||||
@@ -109,6 +131,7 @@ export default class NostrDataService extends Service {
|
||||
_photosSub = null;
|
||||
_contributionsSub = null;
|
||||
_deletionsSub = null;
|
||||
_insertSub = null;
|
||||
_profileModelSubs = new Map();
|
||||
// Pubkeys whose profiles have already been requested at least once. Used to
|
||||
// distinguish a first lookup (handled by the ProfileModel loader) from a
|
||||
@@ -193,15 +216,26 @@ export default class NostrDataService extends Service {
|
||||
// instantly without waiting for relay connections.
|
||||
this._provenanceReady = this._hydrateProvenance();
|
||||
|
||||
// Track event authors so kind-5 deletions can be authorship-checked. The
|
||||
// store emits every newly inserted (non-deletion) event on `insert$`.
|
||||
this._insertSub = this.store.insert$.subscribe((event) => {
|
||||
this._trackEventAuthor(event?.id, event?.pubkey);
|
||||
});
|
||||
|
||||
// Centralized kind-5 deletion handling: the store routes kind-5 events to
|
||||
// its DeleteManager (never into the event database), so listen to the
|
||||
// deletion stream rather than a timeline. Drop provenance entries for
|
||||
// deleted events so the trust map and IDB don't accumulate dead entries.
|
||||
this._deletionsSub = this.store.deletes.deleted$.subscribe(
|
||||
({ pointer }) => {
|
||||
if (isEventPointer(pointer)) {
|
||||
this._removeProvenance(pointer.id);
|
||||
}
|
||||
if (!isEventPointer(pointer)) return;
|
||||
// NIP-09: a deletion only applies to events by the same author.
|
||||
// `pointer.author` is the deleting (kind-5) event's pubkey. Unknown
|
||||
// referenced authors are never treated as a match, so a forged
|
||||
// cross-author deletion cannot drop provenance.
|
||||
const referencedAuthor = this._eventAuthors.get(pointer.id);
|
||||
if (!referencedAuthor || referencedAuthor !== pointer.author) return;
|
||||
this._removeProvenance(pointer.id);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -234,6 +268,7 @@ export default class NostrDataService extends Service {
|
||||
|
||||
get configuredReadRelays() {
|
||||
const configured = uniqNormalizedRelays([
|
||||
...DEFAULT_UNTRUSTED_READ_RELAYS,
|
||||
...this.mailboxReadRelays,
|
||||
...(this.settings.nostrReadRelays || []),
|
||||
]);
|
||||
@@ -350,9 +385,24 @@ export default class NostrDataService extends Service {
|
||||
});
|
||||
}
|
||||
|
||||
// Track an event's author for the kind-5 authorship check, bounded to
|
||||
// `MAX_TRACKED_EVENT_AUTHORS` entries. The Map's insertion order is the
|
||||
// eviction order (FIFO/LRU): re-inserting refreshes recency and the oldest
|
||||
// entry is dropped once the cap is exceeded.
|
||||
_trackEventAuthor(eventId, pubkey) {
|
||||
if (!eventId || !pubkey) return;
|
||||
this._eventAuthors.delete(eventId);
|
||||
this._eventAuthors.set(eventId, pubkey);
|
||||
while (this._eventAuthors.size > MAX_TRACKED_EVENT_AUTHORS) {
|
||||
const oldestId = this._eventAuthors.keys().next().value;
|
||||
this._eventAuthors.delete(oldestId);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove provenance for a deleted event (called on kind-5 processing).
|
||||
_removeProvenance(eventId) {
|
||||
if (!eventId) return;
|
||||
this._eventAuthors.delete(eventId);
|
||||
if (!this._eventRelays.delete(eventId)) return;
|
||||
this.localForage.remove(PROVENANCE_STORE, eventId).catch((e) => {
|
||||
console.debug('[nostr-data] Failed to remove relay provenance', e);
|
||||
@@ -1159,6 +1209,10 @@ export default class NostrDataService extends Service {
|
||||
}
|
||||
|
||||
// 2. Request new events from the network in the background and dump them into the store
|
||||
// Note: this also queries `activeWriteRelays`, which now includes the
|
||||
// submission relay, so profile lookups reach it too. That is harmless:
|
||||
// kind 0 is only read here (Marco never publishes it), and untrusted
|
||||
// relays cannot make content trusted.
|
||||
const profileRelays = Array.from(
|
||||
new Set([...DIRECTORY_RELAYS, ...this.activeWriteRelays])
|
||||
);
|
||||
@@ -1405,6 +1459,11 @@ export default class NostrDataService extends Service {
|
||||
this._deletionsSub = null;
|
||||
}
|
||||
|
||||
if (this._insertSub) {
|
||||
this._insertSub.unsubscribe();
|
||||
this._insertSub = null;
|
||||
}
|
||||
|
||||
if (this._stopPersisting) {
|
||||
this._stopPersisting();
|
||||
}
|
||||
|
||||
@@ -117,15 +117,31 @@ export function parsePhotoFromEvent(event) {
|
||||
* Applies kind 5 deletion events to a list of kind 360 events, returning the
|
||||
* surviving kind 360 events.
|
||||
*
|
||||
* Per NIP-09, a deletion is only effective when the kind 5 event is authored by
|
||||
* the same pubkey as the event it references. Cross-author deletions are
|
||||
* ignored, so a forged deletion cannot hide someone else's photos.
|
||||
*
|
||||
* @param {Array} events Mixed kind 360 and kind 5 events
|
||||
* @returns {Array} Surviving kind 360 events
|
||||
*/
|
||||
export function applyDeletions(events) {
|
||||
const eventAuthors = new Map();
|
||||
for (const event of events) {
|
||||
if (event.kind === 360 && event.id) {
|
||||
eventAuthors.set(event.id, event.pubkey);
|
||||
}
|
||||
}
|
||||
|
||||
const deletedIds = new Set();
|
||||
for (const event of events) {
|
||||
if (event.kind === 5) {
|
||||
for (const tag of event.tags || []) {
|
||||
if (tag[0] === 'e' && tag[1]) deletedIds.add(tag[1]);
|
||||
if (tag[0] === 'e' && tag[1]) {
|
||||
const referencedAuthor = eventAuthors.get(tag[1]);
|
||||
if (referencedAuthor && referencedAuthor === event.pubkey) {
|
||||
deletedIds.add(tag[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+42
-17
@@ -6,14 +6,15 @@ Marco fetches Nostr content (currently kind 360 place photos, later kind 30360 r
|
||||
|
||||
## Trust Model
|
||||
|
||||
| Rule | Description |
|
||||
| ------------------------ | ------------------------------------------------------------------------------------------------------------ |
|
||||
| **Required relays** | Always trusted (e.g. `wss://nostr.kosmos.org`). Cannot be untrusted. |
|
||||
| **Custom relays** | Untrusted by default. Users toggle them to trusted in **Settings → Nostr → Read Relays**. |
|
||||
| **Own content** | Events authored by the connected user's pubkey are always trusted. |
|
||||
| **Non-content kinds** | Kind 5 deletions and metadata always pass through unfiltered. |
|
||||
| **Provenance upgrades** | If an event is first seen on an untrusted relay and later on a trusted one, it becomes visible. |
|
||||
| **Hiding, not blocking** | Untrusted content is cached and stored; it is filtered at presentation time so it can be revealed on demand. |
|
||||
| Rule | Description |
|
||||
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Required relays** | Always trusted (e.g. `wss://nostr.kosmos.org`). Cannot be untrusted. |
|
||||
| **Custom relays** | Untrusted by default. Users toggle them to trusted in **Settings → Nostr → Read Relays**. |
|
||||
| **Submission relay** | `wss://submit.nostr.kosmos.org` is a built-in **untrusted** configured read relay. It is never a required relay, so its content is not trusted until it is also seen on `wss://nostr.kosmos.org`. |
|
||||
| **Own content** | Events authored by the connected user's pubkey are always trusted. |
|
||||
| **Non-content kinds** | Kind 5 deletions and metadata always pass through unfiltered. |
|
||||
| **Provenance upgrades** | If an event is first seen on an untrusted relay and later on a trusted one, it becomes visible. |
|
||||
| **Hiding, not blocking** | Untrusted content is cached and stored; it is filtered at presentation time so it can be revealed on demand. |
|
||||
|
||||
## How It Works Under the Hood
|
||||
|
||||
@@ -44,18 +45,41 @@ Marco fetches Nostr content (currently kind 360 place photos, later kind 30360 r
|
||||
### 5. Deletions
|
||||
|
||||
- When a kind 5 deletion event enters the store, provenance entries for the referenced event IDs are removed from memory and IndexedDB.
|
||||
- **Author rule (NIP-09):** a deletion is only honored when the kind 5 event's `pubkey` equals the referenced event's author pubkey. A forged cross-author deletion is ignored, in both the store/provenance path (`NostrDataService`) and the contributions grouping (`applyDeletions`). If the referenced event's author cannot be established, the deletion is not treated as effective.
|
||||
|
||||
## Submission Relay
|
||||
|
||||
Marco publishes new place photos (kind 360) to `wss://submit.nostr.kosmos.org`
|
||||
(`DEFAULT_WRITE_RELAYS`). A moderation service validates submissions and promotes
|
||||
approved, author-signed events to `wss://nostr.kosmos.org`.
|
||||
|
||||
Because the submission relay is open-write, Marco also reads from it — but as an
|
||||
**untrusted** configured relay, not a required one:
|
||||
|
||||
- `wss://submit.nostr.kosmos.org` is merged into `configuredReadRelays` (so it is
|
||||
read) but deliberately kept out of `requiredReadRelays` (which feeds
|
||||
`trustedRelays`).
|
||||
- Submitted content therefore stays hidden by default until it is also observed on
|
||||
`wss://nostr.kosmos.org` (the provenance-upgrade rule above), while the user's own
|
||||
uploads remain visible immediately (own-content rule).
|
||||
- Users can remove the relay via `settings.nostrReadRelayExclusions` like any other
|
||||
configured read relay.
|
||||
- `loadProfile` also requests from `activeWriteRelays`, so kind 0 profile lookups
|
||||
reach the submission relay too. This is harmless: Marco only reads kind 0 (it never
|
||||
publishes it), and an untrusted relay cannot make content trusted.
|
||||
|
||||
## Key Code Paths
|
||||
|
||||
| File | What it does |
|
||||
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `app/services/nostr-data.js` | `_requestContentWithProvenance()`, `isTrustedEvent()`, `partitionByTrust()`, `trustedRelays` getter, provenance hydration/persistence |
|
||||
| `app/services/settings.js` | `nostrTrustedRelays` setting (null = required relays only) |
|
||||
| `app/components/app-menu/settings/nostr.gjs` | Per-relay shield toggle in read-relay list |
|
||||
| `app/components/place-details.gjs` | Hidden-content reveal link in meta section |
|
||||
| `app/utils/nostr.js` | `relayUrlMatches()` normalization/matching helper |
|
||||
| `app/utils/icons.js` | `shield` icon used for trusted state |
|
||||
| `app/styles/app.css` | `.btn-trust-relay` styles in relay settings |
|
||||
| File | What it does |
|
||||
| -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `app/services/nostr-data.js` | `_requestContentWithProvenance()`, `isTrustedEvent()`, `partitionByTrust()`, `trustedRelays` getter, provenance hydration/persistence, kind-5 author check |
|
||||
| `app/utils/contributions.js` | `applyDeletions()` — applies kind 5 deletions with the NIP-09 author rule |
|
||||
| `app/services/settings.js` | `nostrTrustedRelays` setting (null = required relays only) |
|
||||
| `app/components/app-menu/settings/nostr.gjs` | Per-relay shield toggle in read-relay list |
|
||||
| `app/components/place-details.gjs` | Hidden-content reveal link in meta section |
|
||||
| `app/utils/nostr.js` | `relayUrlMatches()` normalization/matching helper |
|
||||
| `app/utils/icons.js` | `shield` icon used for trusted state |
|
||||
| `app/styles/app.css` | `.btn-trust-relay` styles in relay settings |
|
||||
|
||||
## Extending the Feature
|
||||
|
||||
@@ -66,6 +90,7 @@ Marco fetches Nostr content (currently kind 360 place photos, later kind 30360 r
|
||||
|
||||
## Known Limitations / Future Work
|
||||
|
||||
- The kind-5 author map (`_eventAuthors`) is capped at `MAX_TRACKED_EVENT_AUTHORS` (10,000) with FIFO/LRU eviction; an evicted entry just makes a later deletion's authorship unverifiable, which is treated as no-match (fail-safe).
|
||||
- Provenance store is currently unbounded; add LRU/TTL cleanup.
|
||||
- Untrusting a relay hides its content immediately, but does not purge the `nostr-idb` event cache.
|
||||
- Relay settings UI is currently the only way to manage trust; no global "trust all" toggle.
|
||||
@@ -2,7 +2,9 @@ import { module, test } from 'qunit';
|
||||
import { setupTest } from 'marco/tests/helpers';
|
||||
import { Subject, EMPTY, of } from 'rxjs';
|
||||
import Service from '@ember/service';
|
||||
import NostrDataService from 'marco/services/nostr-data';
|
||||
import NostrDataService, {
|
||||
MAX_TRACKED_EVENT_AUTHORS,
|
||||
} from 'marco/services/nostr-data';
|
||||
import { getGeohashPrefixesInBbox } from 'marco/utils/geohash-coverage';
|
||||
|
||||
function makePubkey(n) {
|
||||
@@ -373,15 +375,21 @@ module('Unit | Service | nostr-data | relay configuration', function (hooks) {
|
||||
);
|
||||
});
|
||||
|
||||
test('configuredReadRelays merges mailbox and custom relays with dedupe and exclusions', function (assert) {
|
||||
test('configuredReadRelays includes the untrusted submission relay and merges mailbox and custom relays with dedupe and exclusions', function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
assert.deepEqual(
|
||||
service.configuredReadRelays,
|
||||
['wss://submit.nostr.kosmos.org'],
|
||||
'submission relay is a configured (untrusted) read relay by default'
|
||||
);
|
||||
|
||||
service.mailboxes = { inboxes: ['wss://a.example'], outboxes: [] };
|
||||
service.settings.nostrReadRelays = ['wss://a.example/', 'wss://b.example'];
|
||||
|
||||
assert.deepEqual(
|
||||
service.configuredReadRelays,
|
||||
['wss://a.example', 'wss://b.example'],
|
||||
['wss://submit.nostr.kosmos.org', 'wss://a.example', 'wss://b.example'],
|
||||
'merges and deduplicates (normalized)'
|
||||
);
|
||||
|
||||
@@ -389,9 +397,33 @@ module('Unit | Service | nostr-data | relay configuration', function (hooks) {
|
||||
|
||||
assert.deepEqual(
|
||||
service.configuredReadRelays,
|
||||
['wss://b.example'],
|
||||
['wss://submit.nostr.kosmos.org', 'wss://b.example'],
|
||||
'exclusions remove mailbox relays'
|
||||
);
|
||||
|
||||
service.settings.nostrReadRelayExclusions = [
|
||||
'wss://submit.nostr.kosmos.org',
|
||||
];
|
||||
|
||||
assert.deepEqual(
|
||||
service.configuredReadRelays,
|
||||
['wss://a.example', 'wss://b.example'],
|
||||
'the submission relay can be excluded by the user'
|
||||
);
|
||||
});
|
||||
|
||||
test('requiredReadRelays does not include the untrusted submission relay', function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
assert.deepEqual(
|
||||
service.requiredReadRelays,
|
||||
['wss://nostr.kosmos.org'],
|
||||
'required read relays stay kosmos-only'
|
||||
);
|
||||
assert.notOk(
|
||||
service.requiredReadRelays.includes('wss://submit.nostr.kosmos.org'),
|
||||
'submission relay is never a required (and therefore trusted) relay'
|
||||
);
|
||||
});
|
||||
|
||||
test('configuredWriteRelays merges mailbox outboxes with custom write relays', function (assert) {
|
||||
@@ -420,26 +452,40 @@ module('Unit | Service | nostr-data | relay configuration', function (hooks) {
|
||||
|
||||
assert.deepEqual(
|
||||
service.activeReadRelays,
|
||||
['wss://nostr.kosmos.org'],
|
||||
'default is required only'
|
||||
['wss://nostr.kosmos.org', 'wss://submit.nostr.kosmos.org'],
|
||||
'default is required plus the untrusted submission relay'
|
||||
);
|
||||
|
||||
service.settings.nostrReadRelays = ['wss://custom.example'];
|
||||
|
||||
assert.deepEqual(
|
||||
service.activeReadRelays,
|
||||
['wss://nostr.kosmos.org', 'wss://custom.example'],
|
||||
'required first, custom appended'
|
||||
[
|
||||
'wss://nostr.kosmos.org',
|
||||
'wss://submit.nostr.kosmos.org',
|
||||
'wss://custom.example',
|
||||
],
|
||||
'required first, then configured defaults, then custom'
|
||||
);
|
||||
});
|
||||
|
||||
test('activeWriteRelays returns empty when nothing configured', function (assert) {
|
||||
test('requiredWriteRelays defaults to the submission relay', function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
assert.deepEqual(
|
||||
service.requiredWriteRelays,
|
||||
['wss://submit.nostr.kosmos.org'],
|
||||
'photos are published to the submission relay by default'
|
||||
);
|
||||
});
|
||||
|
||||
test('activeWriteRelays returns the submission relay when nothing else is configured', function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
assert.deepEqual(
|
||||
service.activeWriteRelays,
|
||||
[],
|
||||
'no required write relays by default'
|
||||
['wss://submit.nostr.kosmos.org'],
|
||||
'submission relay is the only default write relay'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -489,11 +535,14 @@ module('Unit | Service | nostr-data | provenance', function (hooks) {
|
||||
);
|
||||
});
|
||||
|
||||
test('kind 5 deletion events remove provenance for referenced events', async function (assert) {
|
||||
test('kind 5 deletion events remove provenance for referenced events by the same author', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
const deadId = makeEventId(901);
|
||||
const author = makePubkey(50);
|
||||
|
||||
// The referenced event must be known to the store so its authorship can
|
||||
// be verified against the deletion.
|
||||
service.store.add(makePhotoEvent(author, 'osm:node:901', { id: deadId }));
|
||||
service._recordProvenance(deadId, 'wss://one.example');
|
||||
|
||||
// The store routes kind-5 events to its DeleteManager, which emits
|
||||
@@ -511,6 +560,73 @@ module('Unit | Service | nostr-data | provenance', function (hooks) {
|
||||
assert.strictEqual(persisted, null, 'provenance removed from localForage');
|
||||
});
|
||||
|
||||
test('kind 5 deletion events by a different author do not remove provenance', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
const victimId = makeEventId(903);
|
||||
const victimAuthor = makePubkey(51);
|
||||
const attackerAuthor = makePubkey(52);
|
||||
|
||||
service.store.add(
|
||||
makePhotoEvent(victimAuthor, 'osm:node:903', { id: victimId })
|
||||
);
|
||||
service._recordProvenance(victimId, 'wss://one.example');
|
||||
|
||||
// Attacker tries to delete the victim's event.
|
||||
service.store.add(makeDeletionEvent(attackerAuthor, [victimId]));
|
||||
|
||||
assert.true(
|
||||
service._eventRelays.has(victimId),
|
||||
'provenance is preserved for a cross-author deletion'
|
||||
);
|
||||
const persisted = await service.localForage.get(
|
||||
'event-relay-provenance',
|
||||
victimId
|
||||
);
|
||||
assert.deepEqual(
|
||||
persisted,
|
||||
['wss://one.example'],
|
||||
'persisted provenance is preserved'
|
||||
);
|
||||
});
|
||||
|
||||
test('kind 5 deletion events referencing an unknown event do not remove provenance', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
const unknownId = makeEventId(904);
|
||||
const author = makePubkey(53);
|
||||
|
||||
service._recordProvenance(unknownId, 'wss://one.example');
|
||||
|
||||
service.store.add(makeDeletionEvent(author, [unknownId]));
|
||||
|
||||
assert.true(
|
||||
service._eventRelays.has(unknownId),
|
||||
'provenance is preserved when authorship cannot be verified'
|
||||
);
|
||||
});
|
||||
|
||||
test('_eventAuthors is bounded and evicts the oldest entries', function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
for (let i = 0; i <= MAX_TRACKED_EVENT_AUTHORS; i++) {
|
||||
service._trackEventAuthor(makeEventId(i), makePubkey(i));
|
||||
}
|
||||
|
||||
assert.strictEqual(
|
||||
service._eventAuthors.size,
|
||||
MAX_TRACKED_EVENT_AUTHORS,
|
||||
'map stays at the cap'
|
||||
);
|
||||
assert.false(
|
||||
service._eventAuthors.has(makeEventId(0)),
|
||||
'oldest entry was evicted'
|
||||
);
|
||||
assert.strictEqual(
|
||||
service._eventAuthors.get(makeEventId(MAX_TRACKED_EVENT_AUTHORS)),
|
||||
makePubkey(MAX_TRACKED_EVENT_AUTHORS),
|
||||
'newest entry is retained'
|
||||
);
|
||||
});
|
||||
|
||||
test('_hydrateProvenance restores persisted provenance', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
const eventId = makeEventId(902);
|
||||
@@ -695,6 +811,45 @@ module('Unit | Service | nostr-data | trust evaluation', function (hooks) {
|
||||
);
|
||||
});
|
||||
|
||||
test('submission-relay content stays untrusted until seen on nostr.kosmos.org', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
const userPubkey = makePubkey(1);
|
||||
const randomPubkey = makePubkey(99);
|
||||
|
||||
await service.loadProfile(userPubkey);
|
||||
|
||||
const photoEvent = makePhotoEvent(randomPubkey, 'osm:node:123', {
|
||||
id: makeEventId(205),
|
||||
});
|
||||
service.store.add(photoEvent);
|
||||
|
||||
const submissionRelay = 'wss://submit.nostr.kosmos.org';
|
||||
service._recordProvenance(photoEvent.id, submissionRelay);
|
||||
|
||||
assert.deepEqual(
|
||||
service.activeReadRelays,
|
||||
['wss://nostr.kosmos.org', submissionRelay],
|
||||
'submission relay is configured for reads'
|
||||
);
|
||||
assert.notOk(
|
||||
service.trustedRelays.includes(submissionRelay),
|
||||
'submission relay is not trusted'
|
||||
);
|
||||
assert.false(
|
||||
service.isTrustedEvent(photoEvent),
|
||||
'content seen only on the submission relay is not trusted'
|
||||
);
|
||||
|
||||
// Now the event is also observed on the trusted relay
|
||||
service._recordProvenance(photoEvent.id, 'wss://nostr.kosmos.org');
|
||||
|
||||
assert.true(
|
||||
service.isTrustedEvent(photoEvent),
|
||||
'content is trusted once seen on nostr.kosmos.org'
|
||||
);
|
||||
});
|
||||
|
||||
test('isTrustedEvent re-evaluates when contacts change', async function (assert) {
|
||||
const service = this.owner.lookup('service:nostr-data');
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ import { groupPhotoContributions } from 'marco/utils/contributions';
|
||||
function makePhotoEvent({
|
||||
id,
|
||||
created_at,
|
||||
pubkey = 'pubkey-1',
|
||||
placeIdentifier = 'osm:node:123',
|
||||
url = 'https://example.com/photo.jpg',
|
||||
tags = [],
|
||||
}) {
|
||||
return {
|
||||
id,
|
||||
pubkey: 'pubkey-1',
|
||||
pubkey,
|
||||
kind: 360,
|
||||
created_at,
|
||||
tags: [
|
||||
@@ -21,10 +22,10 @@ function makePhotoEvent({
|
||||
};
|
||||
}
|
||||
|
||||
function makeDeletionEvent({ id, targetId }) {
|
||||
function makeDeletionEvent({ id, targetId, pubkey = 'pubkey-1' }) {
|
||||
return {
|
||||
id,
|
||||
pubkey: 'pubkey-1',
|
||||
pubkey,
|
||||
kind: 5,
|
||||
created_at: 999,
|
||||
tags: [['e', targetId]],
|
||||
@@ -232,6 +233,48 @@ module('Unit | Utility | contributions', function () {
|
||||
assert.strictEqual(entries[0].photos[0].url, 'https://x.com/2.jpg');
|
||||
});
|
||||
|
||||
test('groupPhotoContributions ignores cross-author kind 5 deletions (NIP-09)', function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
pubkey: 'author-a',
|
||||
url: 'https://x.com/1.jpg',
|
||||
}),
|
||||
makePhotoEvent({
|
||||
id: 'e2',
|
||||
created_at: 2000,
|
||||
pubkey: 'author-a',
|
||||
url: 'https://x.com/2.jpg',
|
||||
}),
|
||||
// Attacker (author-b) tries to delete author-a's photo.
|
||||
makeDeletionEvent({
|
||||
id: 'd1',
|
||||
targetId: 'e1',
|
||||
pubkey: 'author-b',
|
||||
}),
|
||||
];
|
||||
|
||||
const entries = groupPhotoContributions(events);
|
||||
assert.strictEqual(entries.length, 1, 'one entry remains');
|
||||
assert.strictEqual(
|
||||
entries[0].photos.length,
|
||||
2,
|
||||
'both photos survive a forged deletion'
|
||||
);
|
||||
});
|
||||
|
||||
test('groupPhotoContributions ignores deletions of unknown events', function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({ id: 'e1', created_at: 1000 }),
|
||||
makeDeletionEvent({ id: 'd1', targetId: 'never-seen' }),
|
||||
];
|
||||
|
||||
const entries = groupPhotoContributions(events);
|
||||
assert.strictEqual(entries.length, 1, 'entry is kept');
|
||||
assert.strictEqual(entries[0].photos[0].eventId, 'e1');
|
||||
});
|
||||
|
||||
test('groupPhotoContributions skips events without an i tag', function (assert) {
|
||||
const events = [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user