Skip to content

Commit 083e076

Browse files
committed
check binary exists + more logging
1 parent fc81a33 commit 083e076

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/crypto/gpg.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,62 @@ export class GpgWrapper {
8787
}
8888

8989
public async encryptFile({ inputPath, outputPath, recipient, signer, signerPassphrase }: EncryptFileInput): Promise<EncryptFileResult> {
90+
// TODO: move this into the constructor?
91+
try {
92+
log(`[DEBUG] Checking GPG binary at path: '${this.binPath}'`);
93+
await access(this.binPath, constants.X_OK);
94+
log(`[DEBUG] GPG binary is executable.`);
95+
}
96+
catch {
97+
log(`[ERROR] GPG binary not found or not executable at path: '${this.binPath}'`);
98+
return { success: false, error: `GPG binary not found or not executable at path: '${this.binPath}'`, code: GpgErrorCode.GPG_NOT_FOUND }
99+
}
90100

91101
// check read permissions on input
92-
try { await access(inputPath, constants.R_OK) }
102+
try {
103+
log(`[DEBUG] Checking read access for input file at path: '${inputPath}'`);
104+
await access(inputPath, constants.R_OK);
105+
log(`[DEBUG] Read access confirmed for input file.`);
106+
}
93107
catch {
94108
log(`[ERROR] Unable to read input file, insufficient permissions for path: '${inputPath}'`);
95109
return { success: false, error: `Unable to read input file: '${inputPath}'`, code: "INPUT_NOT_READABLE" }
96110
}
97111

98112
// check write permissions on output
99113
const outputDir = dirname(outputPath);
100-
try { await access(outputDir, constants.W_OK) }
114+
try {
115+
log(`[DEBUG] Checking write access for output directory at path: '${outputDir}'`);
116+
await access(outputDir, constants.W_OK);
117+
log(`[DEBUG] Write access confirmed for output directory.`);
118+
}
101119
catch {
102120
log(`[ERROR] Unable to write to output directory, insufficient permissions for path: '${outputDir}'`);
103121
return { success: false, error: `Unable to write to output path: '${outputPath}'`, code: "OUTPUT_NOT_WRITABLE" }
104122
}
105123

106124
// check recipient key is in keyring and valid
107125
if (this.options.verifyRecipientKey) {
126+
log(`[DEBUG] Verifying recipient key exists in keyring: '${recipient}'`);
108127
const okay = this.keyExists(recipient, "RECIPIENT");
109128
if (!okay) {
110129
const msg = `Recipient key not found in local keyring: '${recipient}'`;
111130
log(`[ERROR] ${msg}`);
112131
return { success: false, error: msg, code: GpgErrorCode.RECIPIENT_KEY_NOT_FOUND }
113132
}
133+
log(`[DEBUG] Recipient key exists in keyring.`);
114134
}
115135

116136
// check signer key is in the keyring and valid
117137
if (signer && this.options.verifySignerKey) {
138+
log(`[DEBUG] Verifying signer secret key exists in keyring: '${signer}'`);
118139
const okay = this.keyExists(signer, "SIGNER");
119140
if (!okay) {
120141
const msg = `Signer secret key not found in local keyring: '${signer}'`
121142
log(`[ERROR] ${msg}`);
122143
return { success: false, error: msg, code: GpgErrorCode.SIGNER_KEY_NOT_FOUND }
123144
}
145+
log(`[DEBUG] Signer secret key exists in keyring.`);
124146
}
125147

126148
// https://www.gnupg.org/documentation/manuals/gnupg/GPG-Configuration-Options.html
@@ -156,7 +178,7 @@ export class GpgWrapper {
156178
const result = spawnSync(this.binPath, finalArgs, {
157179
stdio: ['pipe', 'pipe', 'pipe'],
158180
env: { ...process.env },
159-
timeout: this.options.timeoutMs ?? 30_000,
181+
timeout: this.options.timeoutMs ?? 60_000,
160182
encoding: "utf-8",
161183
input: inputData
162184
});

src/crypto/gpgError.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum GpgErrorCode {
2+
GPG_NOT_FOUND = "GPG_NOT_FOUND",
23
RECIPIENT_KEY_NOT_FOUND = "RECIPIENT_KEY_NOT_FOUND",
34
RECIPIENT_KEY_REVOKED = "RECIPIENT_KEY_REVOKED",
45
RECIPIENT_KEY_EXPIRED = "RECIPIENT_KEY_EXPIRED",
@@ -83,6 +84,11 @@ export const GpgErrorMap: Record<GpgErrorCode, GpgErrorDetail> = {
8384
message: 'An unexpected GPG error occurred.'
8485
},
8586

87+
[GpgErrorCode.GPG_NOT_FOUND]: {
88+
code: GpgErrorCode.GPG_NOT_FOUND,
89+
regex: /gpg:\s+.*not found/i, // TODO: this probably won't work
90+
message: 'The GPG binary was not found or is not executable.'
91+
},
8692
}
8793

8894
export function identifyError(stderrRaw: string): GpgErrorDetail {

0 commit comments

Comments
 (0)