Skip to content

Commit d4e8f9f

Browse files
Fix the error handling (#3342)
* Fix the error handling * Update package/src/index.ts Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> * Fix the build errors * Fix error message --------- Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com>
1 parent 7ed1c26 commit d4e8f9f

8 files changed

Lines changed: 40 additions & 19 deletions

File tree

package/.swcrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"module": {
1515
"type": "es6"
1616
},
17-
"sourceMaps": true,
17+
"sourceMaps": false,
1818
"exclude": [
1919
"tests",
2020
".*.test.ts$",

package/esm/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ declare const jwtGenCore: {
2323
token: typeof token;
2424
};
2525
export default jwtGenCore;
26-
//# sourceMappingURL=index.d.ts.map

package/esm/index.d.ts.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

package/esm/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@ const payload = (issuerId, duration)=>({
3131
* @param duration
3232
* @returns
3333
*/ export async function token(privateKey, issuerId, privateKeyId, duration = 500) {
34-
const key = await importPKCS8(privateKey.toString(), 'ES256');
35-
return new SignJWT(payload(issuerId, duration)).setProtectedHeader({
36-
alg: 'ES256',
37-
kid: privateKeyId
38-
}).sign(key);
34+
try {
35+
const key = await importPKCS8(privateKey.toString(), 'ES256');
36+
return new SignJWT(payload(issuerId, duration)).setProtectedHeader({
37+
alg: 'ES256',
38+
kid: privateKeyId
39+
}).sign(key);
40+
} catch (error) {
41+
if (error instanceof Error) {
42+
// Use predefined error messages to avoid information leakage
43+
if (error.message.includes('PKCS8')) {
44+
throw new Error('JWT token generation failed: Invalid key format');
45+
} else if (error.message.includes('sign')) {
46+
throw new Error('JWT token generation failed: Signing operation failed');
47+
}
48+
throw new Error('JWT token generation failed: Internal error');
49+
}
50+
throw new Error('JWT token generation failed: Unknown error occurred');
51+
}
3952
}
4053
const jwtGenCore = {
4154
tokenSync,
4255
token
4356
};
4457
export default jwtGenCore;
4558

46-
47-
//# sourceMappingURL=index.js.map

package/esm/index.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

package/src/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,23 @@ export async function token(
5151
privateKeyId: string,
5252
duration = 500,
5353
): Promise<string> {
54-
const key = await importPKCS8(privateKey.toString(), 'ES256');
55-
return new SignJWT(payload(issuerId, duration))
56-
.setProtectedHeader({ alg: 'ES256', kid: privateKeyId })
57-
.sign(key);
54+
try {
55+
const key = await importPKCS8(privateKey.toString(), 'ES256');
56+
return new SignJWT(payload(issuerId, duration))
57+
.setProtectedHeader({ alg: 'ES256', kid: privateKeyId })
58+
.sign(key);
59+
} catch (error) {
60+
if (error instanceof Error) {
61+
// Use predefined error messages to avoid information leakage
62+
if (error.message.includes('PKCS8')) {
63+
throw new Error('JWT token generation failed: Invalid key format');
64+
} else if (error.message.includes('sign')) {
65+
throw new Error('JWT token generation failed: Signing operation failed');
66+
}
67+
throw new Error('JWT token generation failed: Internal error');
68+
}
69+
throw new Error('JWT token generation failed: Unknown error occurred');
70+
}
5871
};
5972

6073
const jwtGenCore = {

package/tsconfig-types.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"outDir": "./@types",
1010
"moduleResolution": "bundler",
1111
"declaration": true,
12-
"declarationMap": true,
13-
"sourceMap": true,
12+
"declarationMap": false,
13+
"sourceMap": false,
1414
"strict": true,
1515
"noImplicitAny": true,
1616
"esModuleInterop": true,

package/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"moduleResolution": "bundler",
1010
"declaration": true,
1111
"declarationDir": "./esm",
12-
"declarationMap": true,
13-
"sourceMap": true,
12+
"declarationMap": false,
13+
"sourceMap": false,
1414
"strict": true,
1515
"noImplicitAny": true,
1616
"esModuleInterop": true,

0 commit comments

Comments
 (0)