Skip to content

Commit bb09ab5

Browse files
committed
fix: 🐛 optionality of host source compose params
1 parent e6947a0 commit bb09ab5

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

src/csp.types.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,17 @@ export const directiveValuesByCategory = {
233233
'Protocol': hostProtocolScheme,
234234
},
235235
compose: (args: {
236-
'Port': number,
237-
'Hostname': string,
238-
'Protocol': HostProtocolSchemes,
239-
}) =>
240-
<HostSource>`${args?.['Protocol'] || ''}${args?.['Hostname'] || ''}${args?.['Port'] ? ':' + args?.['Port'] : ''}`,
236+
'Port'?: number,
237+
'Hostname'?: string,
238+
'Protocol'?: HostProtocolSchemes,
239+
}) => <HostSource>(
240+
(args?.Protocol || '') +
241+
(args?.Hostname || '') +
242+
(args?.Port
243+
? ':' + args?.Port
244+
: ''
245+
)
246+
),
241247
},
242248
],
243249
schemeSource,

tests/mapping.test.ts

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,98 @@ describe('DirectiveMap.get()',() => {
1111
describe('Dynamic Options',() => {
1212
it('Handles Hostname/URL Source',() => {
1313
const src = DirectiveMap.get('child-src');
14-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15-
const start: any = undefined;
16-
const result = src?.values.reduce((_,v) => {
14+
let result1: unknown = '';
15+
let result2: unknown = '';
16+
for (const item of src?.values || []) {
1717
if (
18-
isObject(v) &&
19-
hasOwnProperty(v,'displayName') &&
20-
v.displayName === 'Hostname/URL Source'
18+
isObject(item) &&
19+
hasOwnProperty(item,'displayName') &&
20+
item.displayName === 'Hostname/URL Source'
2121
) {
22-
return v?.compose?.({
22+
result1 = item.compose?.({
2323
'Hostname': 'example.com',
2424
'Port': 443,
2525
'Protocol':'https://',
2626
});
2727
}
28-
return _;
29-
},start);
30-
expect(result).toBe('https://example.com:443');
28+
}
29+
for (const item of src?.values || []) {
30+
if (
31+
isObject(item) &&
32+
hasOwnProperty(item,'displayName') &&
33+
item.displayName === 'Hostname/URL Source'
34+
) {
35+
result2 = item.compose?.({});
36+
}
37+
}
38+
expect(result1).toBe('https://example.com:443');
39+
expect(result2).toBe('');
3140
});
3241
it('Crypto Nonce/Hash Source',() => {
3342
const src = DirectiveMap.get('child-src');
34-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
35-
const start: any = undefined;
36-
const result = src?.values.reduce((_,v) => {
43+
let result: unknown = '';
44+
for (const item of src?.values || []) {
3745
if (
38-
isObject(v) &&
39-
hasOwnProperty(v,'displayName') &&
40-
v.displayName === 'Crypto Nonce/Hash Source'
46+
isObject(item) &&
47+
hasOwnProperty(item,'displayName') &&
48+
item.displayName === 'Crypto Nonce/Hash Source'
4149
) {
42-
return v?.compose?.({
50+
result = item.compose?.({
4351
'Algorithm':'sha256',
4452
'Hash':'SomeBase64String',
4553
});
4654
}
47-
return _;
48-
},start);
55+
}
4956
expect(result).toBe('sha256-SomeBase64String');
5057
});
5158
it('Handles URI Source',() => {
52-
const src = DirectiveMap.get('child-src');
53-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
54-
const start: any = undefined;
55-
const result = src?.values.reduce((_,v) => {
59+
const src = DirectiveMap.get('report-uri');
60+
let result: unknown = '';
61+
for (const item of src?.values || []) {
5662
if (
57-
isObject(v) &&
58-
hasOwnProperty(v,'displayName') &&
59-
v.displayName === 'URI Source'
63+
isObject(item) &&
64+
hasOwnProperty(item,'displayName') &&
65+
item.displayName === 'URI Source'
6066
) {
61-
return v?.compose?.({
67+
result = item.compose?.({
6268
'Beginning Delineator':'/',
6369
'Remaining Path':'send/reports/to',
6470
});
6571
}
66-
return _;
67-
},start);
72+
}
6873
expect(result).toBe('/send/reports/to');
6974
});
7075
it('Handles Plugin MIME Type Source',() => {
71-
const src = DirectiveMap.get('child-src');
72-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
const start: any = undefined;
74-
const result = src?.values.reduce((_,v) => {
76+
const src = DirectiveMap.get('plugin-types');
77+
let result: unknown = '';
78+
for (const item of src?.values || []) {
7579
if (
76-
isObject(v) &&
77-
hasOwnProperty(v,'displayName') &&
78-
v.displayName === 'Plugin MIME Type Source'
80+
isObject(item) &&
81+
hasOwnProperty(item,'displayName') &&
82+
item.displayName === 'Plugin MIME Type Source'
7983
) {
80-
return v?.compose?.({
84+
result = item.compose?.({
8185
'MIME Category':'application',
8286
'MIME Implementation':'xml',
8387
});
8488
}
85-
return _;
86-
},start);
89+
}
8790
expect(result).toBe('application/xml');
8891
});
8992
it('Handles Any String',() => {
90-
const src = DirectiveMap.get('child-src');
91-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
92-
const start: any = undefined;
93-
const result = src?.values.reduce((_,v) => {
93+
const src = DirectiveMap.get('report-to');
94+
let result: unknown = '';
95+
for (const item of src?.values || []) {
9496
if (
95-
isObject(v) &&
96-
hasOwnProperty(v,'displayName') &&
97-
v.displayName === 'Any String'
97+
isObject(item) &&
98+
hasOwnProperty(item,'displayName') &&
99+
item.displayName === 'Any String'
98100
) {
99-
return v?.compose?.({
101+
result = item.compose?.({
100102
'String':'hello world',
101103
});
102104
}
103-
return _;
104-
},start);
105+
}
105106
expect(result).toBe('hello world');
106107
});
107108
});

0 commit comments

Comments
 (0)