Skip to content

Commit 4e63358

Browse files
committed
use pipeline instead of pipe as recommended
1 parent afe03f5 commit 4e63358

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/cli.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Argument, Option, program } from '@commander-js/extra-typings'
44
import { createReadStream, createWriteStream } from 'node:fs'
5+
import { pipeline } from 'node:stream/promises'
56
import { SSHAgentClient } from './lib/ssh_agent_client.js'
67

78
program
@@ -23,13 +24,18 @@ program
2324
}
2425
const readable = options.input ? createReadStream(options.input) : process.stdin
2526
const writable = options.output ? createWriteStream(options.output) : process.stdout
26-
const transform =
27+
const getTransform =
2728
action === 'decrypt'
28-
? await agent.getDecryptTransform(key, options.seed, options.decryptEncoding)
29-
: await agent.getEncryptTransform(key, options.seed, options.encryptEncoding)
30-
readable.pipe(transform).pipe(writable)
29+
? agent.getDecryptTransform(key, options.seed, options.decryptEncoding)
30+
: agent.getEncryptTransform(key, options.seed, options.encryptEncoding)
31+
await getTransform.then(transform => pipeline(readable, transform, writable))
3132
} catch (err) {
32-
program.error(`Error: ${(err as Error).message}`)
33+
const error = err as Error
34+
if ('code' in error && error.code === 'ERR_OSSL_BAD_DECRYPT') {
35+
program.error("Bad secret or key, can't decrypt")
36+
} else {
37+
program.error(`Error: ${error.message}`)
38+
}
3339
}
3440
})
3541

src/lib/ssh_agent_client.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,7 @@ export class SSHAgentClient {
219219
const iv = buffer.subarray(0, ivLength)
220220
const encrypted = buffer.subarray(ivLength)
221221
const decipher = crypto.createDecipheriv(this.cipherAlgo, cipherKey, iv)
222-
try {
223-
return Buffer.concat([decipher.update(encrypted), decipher.final()])
224-
} catch (err) {
225-
const error = err as Error
226-
if ('code' in error && error.code === 'ERR_OSSL_BAD_DECRYPT') {
227-
throw new Error("Bad secret or key, can't decrypt", {
228-
cause: err,
229-
})
230-
} else {
231-
throw err
232-
}
233-
}
222+
return Buffer.concat([decipher.update(encrypted), decipher.final()])
234223
})
235224
}
236225

test/ssh_agent_cli.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ describe('ssh-crypt cli tests', () => {
4848
)
4949
chai.assert.strictEqual(output, 'Lorem ipsum dolor')
5050
})
51+
it('should exit with error', () => {
52+
const data =
53+
'ecfd6bb57f4891ba7226886e90d2eb848022a495b15ffd91ffe760bca5605f9062c305ee14226d9daf7faa58460c8f50'
54+
chai
55+
.expect(() =>
56+
execSync(
57+
`echo '${data}' | npm exec -- tsx src/cli.ts -k key_rsa -s wrong_secret --decryptEncoding hex decrypt`,
58+
),
59+
)
60+
.to.throw(/bad secret or key, can't decrypt/iu)
61+
})
5162
})

test/ssh_agent_client.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('SSHAgentClient tests', () => {
8888
'ecfd6bb57f4891ba7226886e90d2eb848022a495b15ffd91ffe760bca5605f9062c305ee14226d9daf7faa58460c8f50'
8989
return chai
9090
.expect(agent.decrypt(identity, 'wrong_secret', data))
91-
.to.be.rejectedWith(Error, "Bad secret or key, can't decrypt")
91+
.to.be.rejectedWith(Error, /bad decrypt/iu)
9292
})
9393
it('should throw if corrupted encrypted data', async () => {
9494
const agent = new SSHAgentClient()

0 commit comments

Comments
 (0)