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

Catch broken regular expressions #7

Merged
merged 1 commit into from
May 29, 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
13 changes: 12 additions & 1 deletion server/src/middleware/validatescripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import { getText } from '../util/text.js';

export const validateScripting = (request, response, next) => {
const testDomain = nconf.get('validTestDomains');
const validRegEx = new RegExp(testDomain);
let validRegEx;
try {
validRegEx = new RegExp(testDomain);
} catch (error) {
log.error('Could not use regular expression', error);
request.inputValidationError = getText(
'error.nonmatchingdomain',
'',
testDomain
);
return next();
}

if (request.body.scripting) {
try {
Expand Down
28 changes: 19 additions & 9 deletions server/src/middleware/validateurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const logger = log.getLogger('sitespeedio.server');

export const validateURL = (request, response, next) => {
const testDomain = nconf.get('validTestDomains');
const validRegEx = new RegExp(testDomain);

// From the web or the API
let url = request.body.url;

Expand All @@ -31,19 +31,29 @@ export const validateURL = (request, response, next) => {
}

const urlObject = new URL(url);
if (!validRegEx.test(urlObject.hostname)) {
logger.error(
'Non valid domain %s matching regex for url %s',
urlObject.hostname,
url
);
try {
const validRegEx = new RegExp(testDomain);
if (!validRegEx.test(urlObject.hostname)) {
logger.error(
'Non valid domain %s matching regex for url %s',
urlObject.hostname,
url
);
request.inputValidationError = getText(
'error.nonmatchingdomain',
url,
testDomain
);

return next();
}
} catch (error) {
log.error('Could not use the regular expression', error);
request.inputValidationError = getText(
'error.nonmatchingdomain',
url,
testDomain
);

return next();
}
}
next();
Expand Down
Loading