Skip to content

Commit

Permalink
Catch broken regular expressions (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored May 29, 2024
1 parent 665ff28 commit 39f5fcc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
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

0 comments on commit 39f5fcc

Please sign in to comment.