Skip to content

Commit 0b87e26

Browse files
Update apps/cli-e2e-tests/src/helpers/gateways/PackmindHttpClient.ts
Unguarded JSON.parse and missing decoded.jwt guard JSON.parse(Buffer.from(this.apiKey, 'base64').toString('utf-8')) has no error handling. If the API key is not valid base64-encoded JSON (e.g. a plain token), this throws a raw SyntaxError with a confusing message before even reaching the !orgId guard that was added to handle failures gracefully. Additionally, if the parsed JSON doesn't have a jwt field, decoded.jwt is undefined. Calling this.decodeJwt(undefined) then immediately throws a TypeError: Cannot read properties of undefined (reading 'split') at line 56 — again bypassing the explicit !orgId guard and producing an opaque error. Wrapping the entire parsing block in a try-catch gives a single clear error message for all failure modes: Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 229c281 commit 0b87e26

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

apps/cli-e2e-tests/src/helpers/gateways/PackmindHttpClient.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@ export class PackmindHttpClient {
3939
}
4040

4141
getOrganizationId(): string {
42-
// Decode the API key to get organization ID
43-
const decoded = JSON.parse(
44-
Buffer.from(this.apiKey, 'base64').toString('utf-8'),
45-
);
46-
const jwtPayload = this.decodeJwt(decoded.jwt);
47-
const orgId = jwtPayload?.organization?.id;
48-
if (!orgId) {
49-
throw new Error('Could not extract organization ID from API key.');
42+
try {
43+
const decoded = JSON.parse(
44+
Buffer.from(this.apiKey, 'base64').toString('utf-8'),
45+
);
46+
if (!decoded?.jwt) {
47+
throw new Error('API key does not contain a jwt field.');
48+
}
49+
const jwtPayload = this.decodeJwt(decoded.jwt);
50+
const orgId = jwtPayload?.organization?.id;
51+
if (!orgId) {
52+
throw new Error('Could not extract organization ID from API key.');
53+
}
54+
return orgId;
55+
} catch (err) {
56+
throw new Error(
57+
`Invalid API key: ${err instanceof Error ? err.message : 'unknown error'}`,
58+
);
5059
}
51-
return orgId;
5260
}
5361

5462
private decodeJwt(jwt: string): { organization?: { id?: string } } | null {

0 commit comments

Comments
 (0)