mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-09-12 21:58:57 +02:00
fix: decode and validate webview file paths
This commit is contained in:
@@ -83,13 +83,19 @@ async function webView(req: express.Request, res: express.Response) {
|
||||
const wRoot = repo.options.pageSource.path;
|
||||
|
||||
const indexRepoId = req.path.indexOf(req.params.repoId);
|
||||
const filePath = req.path.substring(
|
||||
const rawPath = req.path.substring(
|
||||
indexRepoId + req.params.repoId.length + 1
|
||||
);
|
||||
let filePath: string;
|
||||
try {
|
||||
filePath = decodeURIComponent(rawPath);
|
||||
} catch {
|
||||
throw new AnonymousError("invalid_path", { httpStatus: 400 });
|
||||
}
|
||||
// Reject traversal in the URL-derived segment before joining it onto the
|
||||
// page-source root. Stripping a single leading "/" or "." is not enough
|
||||
// to stop "../../" sequences from climbing out of the repo (CWE-22).
|
||||
if (filePath.split(/[\\/]/).some((segment) => segment === "..")) {
|
||||
if (/^[\\/]/.test(filePath) || filePath.split(/[\\/]/).some((segment) => segment === "..")) {
|
||||
throw new AnonymousError("invalid_path", {
|
||||
httpStatus: 400,
|
||||
object: filePath,
|
||||
|
||||
Reference in New Issue
Block a user