From 8ed4c6faa5b1bee181c6b18c254822527bdc95c4 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 7 Oct 2026 10:22:38 +0100 Subject: [PATCH 1/4] prune any dangling aliases we find --- index.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5bb7760..c2ef081 100644 --- a/index.js +++ b/index.js @@ -630,9 +630,7 @@ class Corestore extends ReadyResource { if (opts.preload) opts = { ...opts, ...(await opts.preload) } if (this.opened === false) await this.ready() - const discoveryKey = opts.name - ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) - : null + const discoveryKey = opts.name ? await this._getAlias(opts.name) : null this._maybeClosed() const core = this._openCore(discoveryKey, opts) @@ -648,6 +646,18 @@ class Corestore extends ReadyResource { } } + async _getAlias(name) { + const alias = { name, namespace: this.ns } + const discoveryKey = await this.storage.getAlias(alias) + if (discoveryKey === null) return null + if (this.cores.get(toHex(discoveryKey)) !== null) return discoveryKey + if (await this.storage.hasCore(discoveryKey)) return discoveryKey + + // the core was deleted underneath the alias, drop it so the name mints fresh + await this.storage.deleteAlias(alias) + return null + } + _auth(discoveryKey, opts) { const result = { keyPair: null, From 27b1f96f825df76033a8f70fa9bcc1e0faeb3dbe Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 7 Oct 2026 10:35:36 +0100 Subject: [PATCH 2/4] pass discoveryKey to deleteAlias --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c2ef081..6905d3d 100644 --- a/index.js +++ b/index.js @@ -654,7 +654,7 @@ class Corestore extends ReadyResource { if (await this.storage.hasCore(discoveryKey)) return discoveryKey // the core was deleted underneath the alias, drop it so the name mints fresh - await this.storage.deleteAlias(alias) + await this.storage.deleteAlias(alias, discoveryKey) return null } From 04304633f00dd0ccd5e185934624f403c6078ea6 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 14 Oct 2026 19:36:24 +0100 Subject: [PATCH 3/4] throw storage error when encountering a dangling alias --- index.js | 4 +--- test/alias.js | 39 +++++++++++++++++++++++++++++++++++++++ test/all.js | 1 + 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/alias.js diff --git a/index.js b/index.js index 6905d3d..651f63d 100644 --- a/index.js +++ b/index.js @@ -653,9 +653,7 @@ class Corestore extends ReadyResource { if (this.cores.get(toHex(discoveryKey)) !== null) return discoveryKey if (await this.storage.hasCore(discoveryKey)) return discoveryKey - // the core was deleted underneath the alias, drop it so the name mints fresh - await this.storage.deleteAlias(alias, discoveryKey) - return null + throw STORAGE_EMPTY('Alias ' + name + ' points at a core that no longer exists', discoveryKey) } _auth(discoveryKey, opts) { diff --git a/test/alias.js b/test/alias.js new file mode 100644 index 0000000..47865bb --- /dev/null +++ b/test/alias.js @@ -0,0 +1,39 @@ +const test = require('brittle') +const crypto = require('hypercore-crypto') + +const { create } = require('./helpers') + +test('alias - a dangling alias fails the open instead of resolving to nothing', async function (t) { + const store = await create(t) + + const fine = store.get({ name: 'fine' }) + await fine.append('ok') + await fine.close() + + // an alias row whose core is gone, as left behind by storages that deleted + // cores without their alias + const dangling = crypto.discoveryKey(crypto.keyPair().publicKey) + const getAlias = store.storage.getAlias.bind(store.storage) + store.storage.getAlias = (alias) => (alias.name === 'bee' ? dangling : getAlias(alias)) + + const bee = store.get({ name: 'bee' }) + const err = await bee.ready().then( + () => null, + (err) => err + ) + t.ok(err, 'the open fails') + t.is(err.code, 'STORAGE_EMPTY') + t.ok(/bee/.test(err.message), 'names the alias: ' + err.message) + t.is( + await store.storage.getAlias({ name: 'bee', namespace: store.ns }), + dangling, + 'the alias is left alone' + ) + + const again = store.get({ name: 'fine' }) + await again.ready() + t.is(again.length, 1, 'other names still open') + await again.close() + + await store.close() +}) diff --git a/test/all.js b/test/all.js index 89cdfec..78f1349 100644 --- a/test/all.js +++ b/test/all.js @@ -7,6 +7,7 @@ async function runTests() { test.pause() + await import('./alias.js') await import('./basic.js') await import('./groups.js') await import('./notify.js') From 53447c1ded7fce5b2cda6ec06091466542265758 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 14 Oct 2026 20:08:52 +0100 Subject: [PATCH 4/4] audit cleans up any dangling aliases --- lib/audit.js | 9 +++++++++ test/alias.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ test/basic.js | 9 ++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/audit.js b/lib/audit.js index 75a5110..b0c9b98 100644 --- a/lib/audit.js +++ b/lib/audit.js @@ -13,4 +13,13 @@ module.exports = async function* audit(store, { dryRun = false } = {}) { // ignore if failed, we are auditing... } } + + // an alias whose core is gone makes get({ name }) fail until it is dropped + for await (const { alias, discoveryKey } of store.storage.createAliasStream(null)) { + const dangling = !(await store.storage.hasCore(discoveryKey)) + const pruned = + dangling && !dryRun ? await store.storage.deleteAlias(alias, discoveryKey) : false + + yield { discoveryKey, alias, dangling, pruned } + } } diff --git a/test/alias.js b/test/alias.js index 47865bb..d14c180 100644 --- a/test/alias.js +++ b/test/alias.js @@ -37,3 +37,51 @@ test('alias - a dangling alias fails the open instead of resolving to nothing', await store.close() }) + +// a dangling row as left behind by storages that deleted cores without their alias +function injectDanglingAlias(store, name) { + const discoveryKey = crypto.discoveryKey(crypto.keyPair().publicKey) + const createAliasStream = store.storage.createAliasStream.bind(store.storage) + store.storage.createAliasStream = async function* (namespace) { + yield* createAliasStream(namespace) + yield { alias: { name, namespace: store.ns }, discoveryKey } + } + return discoveryKey +} + +test('alias - audit reports every alias and flags the dangling ones', async function (t) { + const store = await create(t) + + const fine = store.get({ name: 'fine' }) + await fine.append('ok') + await fine.close() + + injectDanglingAlias(store, 'ghost') + const deleted = [] + store.storage.deleteAlias = (alias) => { + deleted.push(alias.name) + return Promise.resolve(true) + } + + const seen = [] + for await (const entry of store.audit({ dryRun: true })) { + if (entry.alias) seen.push([entry.alias.name, entry.dangling, entry.pruned]) + } + t.alike(seen, [ + ['fine', false, false], + ['ghost', true, false] + ]) + t.alike(deleted, [], 'a dry run prunes nothing') + + const pruned = [] + for await (const entry of store.audit()) { + if (entry.alias) pruned.push([entry.alias.name, entry.dangling, entry.pruned]) + } + t.alike(pruned, [ + ['fine', false, false], + ['ghost', true, true] + ]) + t.alike(deleted, ['ghost'], 'the dangling alias is pruned') + + await store.close() +}) diff --git a/test/basic.js b/test/basic.js index 82cedcb..ca8bd09 100644 --- a/test/basic.js +++ b/test/basic.js @@ -349,7 +349,13 @@ test('audit', async function (t) { } let n = 0 - for await (const { audit } of store.audit()) { + let aliases = 0 + for await (const { audit, alias, dangling } of store.audit()) { + if (alias) { + aliases++ + if (dangling) t.fail('dangling alias') + continue + } n++ if (audit.droppedBits || audit.droppedBlocks || audit.droppedTreeNodes || audit.corrupt) { t.fail('bad core') @@ -357,6 +363,7 @@ test('audit', async function (t) { } t.is(n, 4) + t.is(aliases, 2, 'one alias per named core') await a.close() await b.close()