Skip to content

Commit

Permalink
add validateFilepath util to check req.path is correct format
Browse files Browse the repository at this point in the history
  • Loading branch information
harley-harris committed May 7, 2024
1 parent f486216 commit 6ba74bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const COOKIES = 'cookies';
export const CONTACT_US = 'contact-us';

// Routing paths
export const ROOT = '/';
export const START_URL = '/start';
export const HOME_URL = '/home';
export const ADD_MEMBER_URL = '/add-member';
Expand Down
7 changes: 4 additions & 3 deletions src/middleware/validation.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { validationResult, FieldValidationError } from 'express-validator';

import * as config from '../config';
import { log } from '../utils/logger';
import { validateFilepath } from '../utils/validateFilepath';
import { FormattedValidationErrors } from '../model/validation.model';

export const checkValidations = (req: Request, res: Response, next: NextFunction) => {
try {
const errorList = validationResult(req);
const sanitisedPath = validateFilepath(req, res);

if (!errorList.isEmpty()) {
const path = req.path;
if (!errorList.isEmpty() && sanitisedPath) {
const id = req.params[config.ID];
// Removing trailing slash and 36 characters from UUID length
const template_path = (id) ? path.substring(0, path.length - 37).substring(1) : path.substring(1);
const template_path = (id) ? sanitisedPath.substring(0, sanitisedPath.length - 37).substring(1) : sanitisedPath.substring(1);
const errors = formatValidationError(errorList.array() as FieldValidationError[]);

log.info(`Validation error on ${template_path} page`);
Expand Down
17 changes: 17 additions & 0 deletions src/utils/validateFilepath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path';
import * as config from '../config';
import { Request, Response } from 'express';

// https://codeql.github.com/codeql-query-help/javascript/js-path-injection/

export const validateFilepath = (req: Request, res: Response): string | void => {

const unsanitizedPath = req.path;

// Check if the normalised path is within the root directory
if (path.resolve(unsanitizedPath).startsWith(config.ROOT)) {
return req.path;
} else {
return res.render(config.ERROR_PAGE);

Check warning on line 15 in src/utils/validateFilepath.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 16 in src/utils/validateFilepath.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
};

0 comments on commit 6ba74bb

Please sign in to comment.