Skip to content

Commit 58dce4f

Browse files
committed
prevent bad link in opstream from crashing sidecar
1 parent b9fe9ce commit 58dce4f

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

subsystems/sidecar/lib/opstream.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ module.exports = class Opstream extends streamx.Readable {
2424
if (autosession) return this.session.close()
2525
}
2626

27-
if (params.link) params.link = plink.normalize(params.link)
27+
if (params.link) {
28+
try {
29+
params.link = plink.normalize(params.link)
30+
} catch (e) {
31+
e.code = 'INVALID_LINK'
32+
error(e)
33+
return close()
34+
}
35+
}
2836
op(params).catch(error).finally(close)
2937
}
3038
})

test/15-opstream.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const test = require('brittle')
2+
const Helper = require('./helper')
3+
4+
test('running ops with invalid link param does not crash the sidecar', async (t) => {
5+
const opMethods = [
6+
'data',
7+
'drop',
8+
'dump',
9+
'gc',
10+
'info',
11+
'release',
12+
'run',
13+
'seed',
14+
'shift',
15+
'stage',
16+
'touch'
17+
]
18+
t.plan(opMethods.length * 2)
19+
20+
const helper = new Helper()
21+
t.teardown(() => helper.close(), { order: Infinity })
22+
await helper.ready()
23+
24+
for (const method of opMethods) {
25+
const testOp = async () => {
26+
const params = { link: { id: 'not-a-link' } }
27+
const stream = helper[method](params)
28+
try {
29+
await Helper.pick(stream, { tag: 'final' })
30+
t.fail('expected op to fail (op passed)')
31+
} catch (e) {
32+
if (e.code === 'INVALID_LINK') {
33+
t.pass()
34+
} else {
35+
t.fail('expected op to fail with INVALID_LINK error')
36+
}
37+
}
38+
}
39+
40+
t.comment(`calling "${method}" with invalid link param throws an error`)
41+
await testOp()
42+
t.comment(`"${method}" can be called again (sidecar not crashed)`)
43+
await testOp()
44+
}
45+
})

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async function runTests() {
2121
await import('./12-assets.test.js')
2222
await import('./13-stage.test.js')
2323
await import('./14-preflight.test.js')
24+
await import('./15-opstream.test.js')
2425

2526
test.resume()
2627
}

0 commit comments

Comments
 (0)