Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(security): replace regular expressions in path builders #3504

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/webpack/browser.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const browserMin = {
devtool: 'source-map',
performance: {
hints: 'error',
maxEntrypointSize: 440000,
maxEntrypointSize: 460000,
maxAssetSize: 50000000,
},
output: {
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"js-yaml": "^4.1.0",
"node-abort-controller": "^3.1.1",
"node-fetch-commonjs": "^3.3.2",
"openapi-path-templating": "^1.5.1",
"qs": "^6.10.2",
"traverse": "=0.6.8"
},
Expand Down
1 change: 1 addition & 0 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export function buildRequest(options) {
value,
operation,
spec,
pathName,
});
}
});
Expand Down
39 changes: 26 additions & 13 deletions src/execute/oas3/parameter-builders.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { resolve as resolvePathTemplate } from 'openapi-path-templating';

import stylize, { encodeCharacters } from './style-serializer.js';
import serialize from './content-serializer.js';

export function path({ req, value, parameter }) {
export function path({ req, value, parameter, pathName }) {
const { name, style, explode, content } = parameter;

if (value === undefined) return;

let resolvedPathname;

if (content) {
const effectiveMediaType = Object.keys(content)[0];

req.url = req.url
.split(`{${name}}`)
.join(encodeCharacters(serialize(value, effectiveMediaType)));
resolvedPathname = resolvePathTemplate(
pathName,
{ [name]: value },
{ encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) }
);
} else {
const styledValue = stylize({
key: parameter.name,
value,
style: style || 'simple',
explode: explode || false,
escape: 'reserved',
});

req.url = req.url.replace(new RegExp(`{${name}}`, 'g'), styledValue);
resolvedPathname = resolvePathTemplate(
pathName,
{ [name]: value },
{
encoder: (val) =>
stylize({
key: parameter.name,
value: val,
style: style || 'simple',
explode: explode || false,
escape: 'reserved',
}),
}
);
}

req.url = req.url.replace(pathName, resolvedPathname);
}

export function query({ req, value, parameter }) {
Expand Down
8 changes: 6 additions & 2 deletions src/execute/swagger2/parameter-builders.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve as resolvePathTemplate } from 'openapi-path-templating';

// These functions will update the request.
// They'll be given {req, value, paramter, spec, operation}.

Expand Down Expand Up @@ -49,9 +51,11 @@ function headerBuilder({ req, parameter, value }) {
}

// Replace path paramters, with values ( ie: the URL )
function pathBuilder({ req, value, parameter }) {
function pathBuilder({ req, value, parameter, pathName }) {
if (value !== undefined) {
req.url = req.url.replace(new RegExp(`{${parameter.name}}`, 'g'), encodeURIComponent(value));
const resolvedPathname = resolvePathTemplate(pathName, { [parameter.name]: value });

req.url = req.url.replace(pathName, resolvedPathname);
}
}

Expand Down
Loading