-
Notifications
You must be signed in to change notification settings - Fork 50
/
request-filters.test.js
232 lines (216 loc) · 5.91 KB
/
request-filters.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import {
invalidRequestMethodFilter,
localIpFilter,
pathInvalidCharacterFilter,
pathWhitelistFilter,
} from './request-filters';
describe('invalidRequestMethodFilter tests', () => {
const successTestCases = [
{
description: 'should not throw an error for GET requests',
req: { originalUrl: '/some-path', method: 'GET' },
},
{
description:
'should not throw an error for OPTIONS requests that include "proxy" in path',
req: { originalUrl: '/proxy/some-path', method: 'OPTIONS' },
},
{
description:
'should not throw an error for HEAD requests that include "proxy" in path',
req: { originalUrl: '/proxy/some-path', method: 'HEAD' },
},
];
const errorTestCases = [
{
description: 'should throw an error for TRACE requests',
req: { originalUrl: '/some-path', method: 'TRACE' },
},
{
description:
'should throw an error for OPTIONS requests not including "proxy" in path',
req: { originalUrl: '/some-path', method: 'OPTIONS' },
},
{
description:
'should throw an error for HEAD requests not including "proxy" in path',
req: { originalUrl: '/some-path', method: 'HEAD' },
},
];
test.each(successTestCases)('$description', ({ req }) => {
expect(() => invalidRequestMethodFilter(req)).not.toThrow();
});
test.each(errorTestCases)('$description', ({ req }) => {
expect(() => invalidRequestMethodFilter(req)).toThrowError(
'Invalid request method',
);
});
});
describe('pathInvalidCharacterFilter tests', () => {
const successTestCases = [
{
description: 'should not throw an error for a valid characters',
req: {
originalUrl: '/valid/path-#&?-123',
},
},
{
description: 'should handle single encoded characters correctly',
req: {
originalUrl: '%2E', // encoded single dot "."
},
},
];
const errorTestCases = [
{
description: 'should throw an error for a path containing ..',
req: {
originalUrl: '/valid/../invalid/path',
},
expectedError: 'Path contains invalid characters',
},
{
description: 'should throw an error when path contains NULL character',
req: { originalUrl: '/valid/path%00' },
expectedError: 'Path contains invalid characters',
},
{
description: 'should throw an error for an improperly encoded path',
req: {
originalUrl: '/invalid%path',
},
expectedError: 'Path contains invalid encoding',
},
{
description: 'should throw an error for double encoded characters',
req: {
originalUrl: '/%252e%252e',
},
expectedError: 'Decoded path contains illegal % characters',
},
];
test.each(successTestCases)('$description', ({ req }) => {
expect(() => pathInvalidCharacterFilter(req)).not.toThrow();
});
test.each(errorTestCases)('$description', ({ req, expectedError }) => {
expect(() => pathInvalidCharacterFilter(req)).toThrowError(expectedError);
});
});
describe('pathWhitelistFilter tests', () => {
const successTestCases = [
{
description: 'should allow whitelisted paths',
req: {
originalUrl: '/backend/api/users',
},
},
{
description: 'should allow whitelisted root path',
req: {
originalUrl: '/backend/.well-known',
},
},
];
const errorTestCases = [
{
description: 'should throw error for non-whitelisted path',
req: {
originalUrl: '/backend/notallowed',
},
},
{
description: 'should throw error for an empty path',
req: {
originalUrl: '/backend/',
},
},
{
description: 'should throw error for undefined paths',
req: {
originalUrl: '',
},
},
];
test.each(successTestCases)('$description', ({ req }) => {
expect(() => pathWhitelistFilter(req)).not.toThrow();
});
test.each(errorTestCases)('$description', ({ req }) => {
expect(() => pathWhitelistFilter(req)).toThrowError(
`Path ${req.originalUrl.replace(/^\/backend/, '')} is not whitelisted.`,
);
});
});
describe('localIpFilter tests', () => {
const successTestCases = [
{
description: 'should allow a public IP address',
headersData: {
targetApiServer: {
host: '203.0.113.1',
},
},
},
{
description: 'should allow an external cluster address',
headersData: {
targetApiServer: {
host: 'myservice.cluster.external',
},
},
},
];
const errorTestCases = [
{
description:
'should throw an error for a local IP address in the 10.0.0.0/8 range',
headersData: {
targetApiServer: {
host: '10.0.0.1',
},
},
},
{
description:
'should throw an error for a local IP address in the 172.16.0.0/12 range',
headersData: {
targetApiServer: {
host: '172.16.0.1',
},
},
},
{
description:
'should throw an error for a local IP address in the 192.168.0.0/16 range',
headersData: {
targetApiServer: {
host: '192.168.1.1',
},
},
},
{
description:
'should throw an error for a local IP address in the 169.254.0.0/16 range',
headersData: {
targetApiServer: {
host: '169.254.1.1',
},
},
},
{
description: 'should throw an error for a local cluster address',
headersData: {
targetApiServer: {
host: 'myservice.cluster.local',
},
},
},
];
test.each(successTestCases)('$description', ({ headersData }) => {
expect(() => localIpFilter({}, headersData)).not.toThrow();
});
test.each(errorTestCases)('$description', ({ headersData }) => {
expect(() => localIpFilter({}, headersData)).toThrowError(
'Local IP addresses are not allowed.',
);
});
});