Skip to content

Commit 4a5d2b8

Browse files
authored
fix: fix reference length bug in manifest unmarshalling (#1065)
* fix: fix reference length bug in manifest unmarshalling * chore: make lint happy
1 parent 2b41e78 commit 4a5d2b8

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

src/manifest/manifest.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ export class Fork {
107107
const type = Binary.uint8ToNumber(reader.read(1))
108108
const prefixLength = Binary.uint8ToNumber(reader.read(1))
109109
const prefix = reader.read(prefixLength)
110-
reader.read(30 - prefixLength)
110+
111+
if (prefixLength < 30) {
112+
reader.read(30 - prefixLength)
113+
}
111114
const selfAddress = reader.read(addressLength)
112115
debug('unmarshalling fork', {
116+
type,
113117
prefixLength,
114118
prefix: DECODER.decode(prefix),
115119
addressLength,
@@ -304,12 +308,12 @@ export class MantarayNode {
304308
throw new Error('MantarayNode#unmarshal invalid version hash')
305309
}
306310
const targetAddressLength = Binary.uint8ToNumber(reader.read(1))
307-
const targetAddress = targetAddressLength === 0 ? NULL_ADDRESS : reader.read(targetAddressLength)
311+
const targetAddress = targetAddressLength ? reader.read(targetAddressLength) : NULL_ADDRESS
308312
const node = new MantarayNode({ selfAddress, targetAddress, obfuscationKey })
309313
const forkBitmap = reader.read(32)
310314
for (let i = 0; i < 256; i++) {
311315
if (Binary.getBit(forkBitmap, i, 'LE')) {
312-
const newFork = Fork.unmarshal(reader, targetAddressLength)
316+
const newFork = Fork.unmarshal(reader, selfAddress.length)
313317
node.forks.set(i, newFork)
314318
newFork.node.parent = node
315319
}

test/integration/manifest.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,20 @@ test('Manifest no feed to resolve', async () => {
114114

115115
expect(feedUpdate.value).toBeNull()
116116
})
117+
118+
test('Manifest add fork with foreign path', async () => {
119+
const bee = makeBee()
120+
121+
const manifest = new MantarayNode()
122+
manifest.addFork('c/中文/index.xml', arbitraryReference())
123+
manifest.addFork('c/中文/index.html', arbitraryReference())
124+
125+
const result = await manifest.saveRecursively(bee, batch())
126+
127+
const unmarshaled = await MantarayNode.unmarshal(bee, result.reference)
128+
await unmarshaled.loadRecursively(bee)
129+
130+
const items = unmarshaled.collectAndMap()
131+
expect(items['c/中文/index.xml']).toBeDefined()
132+
expect(items['c/中文/index.html']).toBeDefined()
133+
})
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { MantarayNode } from '../../src'
2+
import { arbitraryReference, batch, makeBee } from '../utils'
3+
4+
test('should not throw Bytes#checkByteLength', async () => {
5+
const bee = makeBee()
6+
7+
const paths = [
8+
'c/grants/index.xml',
9+
'c/grants/index.html',
10+
'c/ecosystem/index.xml',
11+
'c/ecosystem/index.html',
12+
'c/development-updates/index.xml',
13+
'c/development-updates/index.html',
14+
'c/events/index.xml',
15+
'c/events/index.html',
16+
'c/tutorials/index.xml',
17+
'c/tutorials/index.html',
18+
'css/main.bundle.min.4e55be0357e7dec25cf1bea80877847773ee67f75d09281900b7bd8c8d07b4cded5555fb3b9c01a3826e021712d0fd63866586c0aa832ebb86de60c217a2f288.css',
19+
]
20+
21+
const manifest = new MantarayNode()
22+
for (const path of paths) {
23+
manifest.addFork(path, arbitraryReference())
24+
}
25+
26+
const result = await manifest.saveRecursively(bee, batch())
27+
28+
const unmarshaled = await MantarayNode.unmarshal(bee, result.reference)
29+
await unmarshaled.loadRecursively(bee)
30+
31+
const items = unmarshaled.collectAndMap()
32+
expect(Object.keys(items).length).toBe(paths.length)
33+
})
34+
35+
test('should not throw SyntaxError', async () => {
36+
const bee = makeBee()
37+
38+
const paths = [
39+
'c/ecosystem/index.xml',
40+
'c/ecosystem/index.html',
41+
'c/development-updates/index.xml',
42+
'c/development-updates/index.html',
43+
'c/events/index.xml',
44+
'c/events/index.html',
45+
'css/main.bundle.min.4e55be0357e7dec25cf1bea80877847773ee67f75d09281900b7bd8c8d07b4cded5555fb3b9c01a3826e021712d0fd63866586c0aa832ebb86de60c217a2f288.css',
46+
]
47+
48+
const manifest = new MantarayNode()
49+
for (const path of paths) {
50+
manifest.addFork(path, arbitraryReference())
51+
}
52+
53+
const result = await manifest.saveRecursively(bee, batch())
54+
55+
const unmarshaled = await MantarayNode.unmarshal(bee, result.reference)
56+
await unmarshaled.loadRecursively(bee)
57+
58+
const items = unmarshaled.collectAndMap()
59+
expect(Object.keys(items).length).toBe(paths.length)
60+
})

0 commit comments

Comments
 (0)