Skip to content

Commit 4c1d385

Browse files
committed
fix(security): 规范化 SRI 配置类型,优化算法处理逻辑
1 parent 9eab2f9 commit 4c1d385

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { cheerio, logger } from '@winner-fed/utils';
55
import type { IApi } from '@winner-fed/winjs';
66

77
interface SecurityConfig {
8-
sri: boolean | {
9-
algorithm: 'sha256' | 'sha384' | 'sha512';
10-
};
8+
sri:
9+
| boolean
10+
| {
11+
algorithm: 'sha256' | 'sha384' | 'sha512';
12+
};
1113
}
1214

1315
export default (api: IApi) => {
@@ -20,9 +22,11 @@ export default (api: IApi) => {
2022
sri: zod.union([
2123
zod.boolean(),
2224
zod.object({
23-
algorithm: zod.enum(['sha256', 'sha384', 'sha512']).default('sha512'),
24-
})
25-
])
25+
algorithm: zod
26+
.enum(['sha256', 'sha384', 'sha512'])
27+
.default('sha512'),
28+
}),
29+
]),
2630
});
2731
},
2832
},
@@ -42,7 +46,11 @@ export default (api: IApi) => {
4246
let sriConfig: { algorithm: 'sha256' | 'sha384' | 'sha512' };
4347
if (config.sri === true) {
4448
sriConfig = { algorithm: 'sha512' };
45-
} else if (typeof config.sri === 'object' && config.sri !== null && typeof (config.sri as any).algorithm === 'string') {
49+
} else if (
50+
typeof config.sri === 'object' &&
51+
config.sri !== null &&
52+
typeof (config.sri as any).algorithm === 'string'
53+
) {
4654
sriConfig = { algorithm: (config.sri as any).algorithm };
4755
} else {
4856
// 未指定算法时,默认 sha512
@@ -86,7 +94,9 @@ export default (api: IApi) => {
8694
}
8795

8896
if (source) {
89-
const hash = createHash(sriConfig.algorithm).update(source).digest('base64');
97+
const hash = createHash(sriConfig.algorithm)
98+
.update(source)
99+
.digest('base64');
90100

91101
$el.attr('integrity', `${sriConfig.algorithm}-${hash}`);
92102

0 commit comments

Comments
 (0)