Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5cc0e08

Browse files
authored
fix: validate ipns records with inline public keys (#3224)
* fix: validate with inline keys * test: check inlined pubkey support * chore: lock ipfs-utils at 2.3.1 * chore: test with ipfs-utils fix * chore: update ipfs-utils to 3.0.0
1 parent 3b90e9c commit 5cc0e08

File tree

7 files changed

+28
-36
lines changed

7 files changed

+28
-36
lines changed

packages/interface-ipfs-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"dirty-chai": "^2.0.1",
4242
"ipfs-unixfs": "^1.0.3",
4343
"ipfs-unixfs-importer": "^2.0.2",
44-
"ipfs-utils": "^2.2.2",
44+
"ipfs-utils": "^3.0.0",
4545
"ipld-block": "^0.9.2",
4646
"ipld-dag-cbor": "^0.16.0",
4747
"ipld-dag-pb": "^0.19.0",

packages/ipfs-core-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"buffer": "^5.6.0",
3434
"cids": "^0.8.3",
3535
"err-code": "^2.0.0",
36-
"ipfs-utils": "^2.2.2",
36+
"ipfs-utils": "^3.0.0",
3737
"it-all": "^1.0.1",
3838
"it-map": "^1.0.0",
3939
"it-peekable": "0.0.1"

packages/ipfs-http-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"debug": "^4.1.0",
4949
"form-data": "^3.0.0",
5050
"ipfs-core-utils": "^0.3.1",
51-
"ipfs-utils": "^2.2.2",
51+
"ipfs-utils": "^3.0.0",
5252
"ipld-block": "^0.9.2",
5353
"ipld-dag-cbor": "^0.16.0",
5454
"ipld-dag-pb": "^0.19.0",

packages/ipfs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"ipfs-unixfs": "^1.0.3",
102102
"ipfs-unixfs-exporter": "^2.0.2",
103103
"ipfs-unixfs-importer": "^2.0.2",
104-
"ipfs-utils": "^2.2.2",
104+
"ipfs-utils": "^3.0.0",
105105
"ipld": "^0.26.2",
106106
"ipld-bitcoin": "^0.3.0",
107107
"ipld-block": "^0.9.2",

packages/ipfs/src/core/ipns/resolver.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const ipns = require('ipns')
4-
const crypto = require('libp2p-crypto')
54
const PeerId = require('peer-id')
65
const errcode = require('err-code')
76
const debug = require('debug')
@@ -99,34 +98,7 @@ class IpnsResolver {
9998
throw errcode(new Error('found ipns record that we couldn\'t convert to a value'), 'ERR_INVALID_RECORD_RECEIVED')
10099
}
101100

102-
// if the record has a public key validate it
103-
if (ipnsEntry.pubKey) {
104-
return this._validateRecord(peerId, ipnsEntry)
105-
}
106-
107-
// Otherwise, try to get the public key from routing
108-
let pubKey
109-
try {
110-
pubKey = await this._routing.get(routingKey.toBuffer())
111-
} catch (err) {
112-
log.error(err)
113-
114-
if (err.code === ERR_NOT_FOUND) {
115-
throw errcode(new Error(`public key requested for ${name} was not found in the network`), 'ERR_NO_RECORD_FOUND')
116-
}
117-
118-
throw errcode(new Error(`unexpected error getting the public key for the ipns record ${peerId.id}`), 'ERR_UNEXPECTED_ERROR_GETTING_PUB_KEY')
119-
}
120-
121-
try {
122-
// Insert it into the peer id, in order to be validated by IPNS validator
123-
peerId.pubKey = crypto.keys.unmarshalPublicKey(pubKey)
124-
} catch (err) {
125-
log.error(err)
126-
127-
throw errcode(new Error('found public key record that we couldn\'t convert to a value'), 'ERR_INVALID_PUB_KEY_RECEIVED')
128-
}
129-
101+
// We should have the public key by now (inline, or in the entry)
130102
return this._validateRecord(peerId, ipnsEntry)
131103
}
132104

packages/ipfs/test/core/name-pubsub.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-env mocha */
33
'use strict'
44

5-
const { nanoid } = require('nanoid')
65
const { Buffer } = require('buffer')
76
const { expect } = require('interface-ipfs-core/src/utils/mocha')
87
const base64url = require('base64url')
@@ -18,6 +17,12 @@ const factory = require('../utils/factory')
1817
const namespace = '/record/'
1918
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
2019

20+
const daemonsOptions = {
21+
ipfsOptions: {
22+
EXPERIMENTAL: { ipnsPubsub: true }
23+
}
24+
}
25+
2126
describe('name-pubsub', function () {
2227
const df = factory()
2328
// TODO make this work in the browser and between daemon and in-proc in nodes
@@ -33,8 +38,8 @@ describe('name-pubsub', function () {
3338
this.timeout(40 * 1000)
3439

3540
nodes = await Promise.all([
36-
df.spawn({ type: 'proc', ipfsOptions: { pass: nanoid(), EXPERIMENTAL: { ipnsPubsub: true } } }),
37-
df.spawn({ type: 'proc', ipfsOptions: { pass: nanoid(), EXPERIMENTAL: { ipnsPubsub: true } } })
41+
df.spawn({ ...daemonsOptions }),
42+
df.spawn({ ...daemonsOptions })
3843
])
3944

4045
nodeA = nodes[0].api

packages/ipfs/test/core/name.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { Key } = require('interface-datastore')
99
const last = require('it-last')
1010
const PeerId = require('peer-id')
1111
const errCode = require('err-code')
12+
const ipns = require('ipns')
1213
const getIpnsRoutingConfig = require('../../src/core/ipns/routing/config')
1314
const IpnsPublisher = require('../../src/core/ipns/publisher')
1415
const IpnsRepublisher = require('../../src/core/ipns/republisher')
@@ -199,6 +200,20 @@ describe('name', function () {
199200
})
200201

201202
describe('resolver', () => {
203+
it('should resolve an inlined public key', async () => {
204+
const peerId = await PeerId.create({ keyType: 'ed25519' })
205+
const value = `/ipfs/${peerId.toB58String()}`
206+
const record = await ipns.create(peerId.privKey, value, 1, 10e3)
207+
208+
const routing = {
209+
get: sinon.stub().returns(ipns.marshal(record))
210+
}
211+
const resolver = new IpnsResolver(routing)
212+
213+
const resolved = await resolver.resolve(`/ipns/${peerId.toB58String()}`)
214+
expect(resolved).to.equal(value)
215+
})
216+
202217
it('should fail to resolve if the received name is not a string', () => {
203218
const resolver = new IpnsResolver()
204219
return expect(resolver.resolve(false))

0 commit comments

Comments
 (0)