Skip to content

Commit 642a87c

Browse files
glowcloudchar0n
andauthored
fix(security): replace regular expressions in path builders (#3504)
Refs #3503 --------- Co-authored-by: Vladimir Gorej <[email protected]>
1 parent 10cbc2e commit 642a87c

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

config/webpack/browser.config.babel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const browserMin = {
6363
devtool: 'source-map',
6464
performance: {
6565
hints: 'error',
66-
maxEntrypointSize: 440000,
66+
maxEntrypointSize: 460000,
6767
maxAssetSize: 50000000,
6868
},
6969
output: {

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
"js-yaml": "^4.1.0",
123123
"node-abort-controller": "^3.1.1",
124124
"node-fetch-commonjs": "^3.3.2",
125+
"openapi-path-templating": "^1.5.1",
125126
"qs": "^6.10.2",
126127
"traverse": "=0.6.8"
127128
},

src/execute/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export function buildRequest(options) {
265265
value,
266266
operation,
267267
spec,
268+
pathName,
268269
});
269270
}
270271
});

src/execute/oas3/parameter-builders.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
1+
import { resolve as resolvePathTemplate } from 'openapi-path-templating';
2+
13
import stylize, { encodeCharacters } from './style-serializer.js';
24
import serialize from './content-serializer.js';
35

4-
export function path({ req, value, parameter }) {
6+
export function path({ req, value, parameter, pathName }) {
57
const { name, style, explode, content } = parameter;
68

79
if (value === undefined) return;
810

11+
let resolvedPathname;
12+
913
if (content) {
1014
const effectiveMediaType = Object.keys(content)[0];
1115

12-
req.url = req.url
13-
.split(`{${name}}`)
14-
.join(encodeCharacters(serialize(value, effectiveMediaType)));
16+
resolvedPathname = resolvePathTemplate(
17+
pathName,
18+
{ [name]: value },
19+
{ encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) }
20+
);
1521
} else {
16-
const styledValue = stylize({
17-
key: parameter.name,
18-
value,
19-
style: style || 'simple',
20-
explode: explode || false,
21-
escape: 'reserved',
22-
});
23-
24-
req.url = req.url.replace(new RegExp(`{${name}}`, 'g'), styledValue);
22+
resolvedPathname = resolvePathTemplate(
23+
pathName,
24+
{ [name]: value },
25+
{
26+
encoder: (val) =>
27+
stylize({
28+
key: parameter.name,
29+
value: val,
30+
style: style || 'simple',
31+
explode: explode || false,
32+
escape: 'reserved',
33+
}),
34+
}
35+
);
2536
}
37+
38+
req.url = req.url.replace(pathName, resolvedPathname);
2639
}
2740

2841
export function query({ req, value, parameter }) {

src/execute/swagger2/parameter-builders.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { resolve as resolvePathTemplate } from 'openapi-path-templating';
2+
13
// These functions will update the request.
24
// They'll be given {req, value, paramter, spec, operation}.
35

@@ -49,9 +51,11 @@ function headerBuilder({ req, parameter, value }) {
4951
}
5052

5153
// Replace path paramters, with values ( ie: the URL )
52-
function pathBuilder({ req, value, parameter }) {
54+
function pathBuilder({ req, value, parameter, pathName }) {
5355
if (value !== undefined) {
54-
req.url = req.url.replace(new RegExp(`{${parameter.name}}`, 'g'), encodeURIComponent(value));
56+
const resolvedPathname = resolvePathTemplate(pathName, { [parameter.name]: value });
57+
58+
req.url = req.url.replace(pathName, resolvedPathname);
5559
}
5660
}
5761

0 commit comments

Comments
 (0)