Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

program
Expand All @@ -23,13 +24,18 @@ program
}
const readable = options.input ? createReadStream(options.input) : process.stdin
const writable = options.output ? createWriteStream(options.output) : process.stdout
const transform =
const getTransform =
action === 'decrypt'
? await agent.getDecryptTransform(key, options.seed, options.decryptEncoding)
: await agent.getEncryptTransform(key, options.seed, options.encryptEncoding)
readable.pipe(transform).pipe(writable)
? agent.getDecryptTransform(key, options.seed, options.decryptEncoding)
: agent.getEncryptTransform(key, options.seed, options.encryptEncoding)
await getTransform.then(transform => pipeline(readable, transform, writable))
} catch (err) {
program.error(`Error: ${(err as Error).message}`)
const error = err as Error
if ('code' in error && error.code === 'ERR_OSSL_BAD_DECRYPT') {
program.error("Bad secret or key, can't decrypt")
} else {
program.error(`Error: ${error.message}`)
}
}
})

Expand Down
13 changes: 1 addition & 12 deletions src/lib/ssh_agent_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,7 @@ export class SSHAgentClient {
const iv = buffer.subarray(0, ivLength)
const encrypted = buffer.subarray(ivLength)
const decipher = crypto.createDecipheriv(this.cipherAlgo, cipherKey, iv)
try {
return Buffer.concat([decipher.update(encrypted), decipher.final()])
} catch (err) {
const error = err as Error
if ('code' in error && error.code === 'ERR_OSSL_BAD_DECRYPT') {
throw new Error("Bad secret or key, can't decrypt", {
cause: err,
})
} else {
throw err
}
}
return Buffer.concat([decipher.update(encrypted), decipher.final()])
})
}

Expand Down
11 changes: 11 additions & 0 deletions test/ssh_agent_cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ describe('ssh-crypt cli tests', () => {
)
chai.assert.strictEqual(output, 'Lorem ipsum dolor')
})
it('should exit with error', () => {
const data =
'ecfd6bb57f4891ba7226886e90d2eb848022a495b15ffd91ffe760bca5605f9062c305ee14226d9daf7faa58460c8f50'
chai
.expect(() =>
execSync(
`echo '${data}' | npm exec -- tsx src/cli.ts -k key_rsa -s wrong_secret --decryptEncoding hex decrypt`,
),
)
.to.throw(/bad secret or key, can't decrypt/iu)
})
})
2 changes: 1 addition & 1 deletion test/ssh_agent_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('SSHAgentClient tests', () => {
'ecfd6bb57f4891ba7226886e90d2eb848022a495b15ffd91ffe760bca5605f9062c305ee14226d9daf7faa58460c8f50'
return chai
.expect(agent.decrypt(identity, 'wrong_secret', data))
.to.be.rejectedWith(Error, "Bad secret or key, can't decrypt")
.to.be.rejectedWith(Error, /bad decrypt/iu)
})
it('should throw if corrupted encrypted data', async () => {
const agent = new SSHAgentClient()
Expand Down