mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
tests: Reuse sanitizePathname
when serving frontend specs
This commit is contained in:
parent
ade17490e0
commit
b85a040f13
1 changed files with 5 additions and 14 deletions
|
@ -3,6 +3,7 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const fsp = fs.promises;
|
const fsp = fs.promises;
|
||||||
|
const sanitizePathname = require('../../utils/sanitizePathname');
|
||||||
const settings = require('../../utils/Settings');
|
const settings = require('../../utils/Settings');
|
||||||
|
|
||||||
exports.expressCreateServer = (hookName, args, cb) => {
|
exports.expressCreateServer = (hookName, args, cb) => {
|
||||||
|
@ -30,25 +31,15 @@ exports.expressCreateServer = (hookName, args, cb) => {
|
||||||
|
|
||||||
const rootTestFolder = path.join(settings.root, 'src/tests/frontend/');
|
const rootTestFolder = path.join(settings.root, 'src/tests/frontend/');
|
||||||
|
|
||||||
const sanitizePath = (subPath) => {
|
|
||||||
if (subPath === '') {
|
|
||||||
subPath = 'index.html';
|
|
||||||
}
|
|
||||||
let filePath = path.join(rootTestFolder, subPath);
|
|
||||||
// make sure we jail the paths to the test folder, otherwise serve index
|
|
||||||
if (filePath.indexOf(rootTestFolder) !== 0) {
|
|
||||||
filePath = path.join(rootTestFolder, 'index.html');
|
|
||||||
}
|
|
||||||
return filePath;
|
|
||||||
};
|
|
||||||
|
|
||||||
// The regexp /[\d\D]{0,}/ is equivalent to the regexp /.*/. The Express route path used here
|
// The regexp /[\d\D]{0,}/ is equivalent to the regexp /.*/. The Express route path used here
|
||||||
// uses the more verbose /[\d\D]{0,}/ pattern instead of /.*/ because path-to-regexp v0.1.7 (the
|
// uses the more verbose /[\d\D]{0,}/ pattern instead of /.*/ because path-to-regexp v0.1.7 (the
|
||||||
// version used with Express v4.x) interprets '.' and '*' differently than regexp.
|
// version used with Express v4.x) interprets '.' and '*' differently than regexp.
|
||||||
args.app.get('/tests/frontend/:file([\\d\\D]{0,})', (req, res, next) => {
|
args.app.get('/tests/frontend/:file([\\d\\D]{0,})', (req, res, next) => {
|
||||||
(async () => {
|
(async () => {
|
||||||
const file = sanitizePath(req.params.file);
|
let relFile = sanitizePathname(req.params.file);
|
||||||
if (req.params.file.startsWith('specs/') && file.endsWith('.js')) {
|
if (['', '.', './'].includes(relFile)) relFile = 'index.html';
|
||||||
|
const file = path.join(rootTestFolder, relFile);
|
||||||
|
if (relFile.startsWith('specs/') && file.endsWith('.js')) {
|
||||||
const content = await fsp.readFile(file);
|
const content = await fsp.readFile(file);
|
||||||
res.setHeader('content-type', 'application/javascript');
|
res.setHeader('content-type', 'application/javascript');
|
||||||
res.send(`describe(${JSON.stringify(path.basename(file))}, function () {\n${content}\n});`);
|
res.send(`describe(${JSON.stringify(path.basename(file))}, function () {\n${content}\n});`);
|
||||||
|
|
Loading…
Reference in a new issue