Skip to content

Commit 57ef526

Browse files
committed
refactor(vscode): simplify path sanitization in openOnUnicode command
1 parent 8197ff3 commit 57ef526

File tree

1 file changed

+3
-29
lines changed

1 file changed

+3
-29
lines changed

vscode/src/commands/open-on-unicode.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TreeViewNode } from "reactive-vscode";
22
import type { UCDTreeItem } from "../composables/useUCDExplorer";
3+
import { resolveSafePath } from "@ucdjs/path-utils";
34
import { executeCommand, useCommand } from "reactive-vscode";
45
import { Uri } from "vscode";
56
import * as Meta from "../generated/meta";
@@ -19,35 +20,8 @@ export function useOpenOnUnicodeCommand() {
1920
return;
2021
}
2122

22-
// Sanitize path to prevent directory traversal attacks
23-
const rawPath = treeViewOrUri.path;
24-
25-
// Normalize the path and check for traversal attempts
26-
// Use a simple approach: split, filter out dangerous segments, rejoin
27-
const segments = rawPath.split("/").filter((segment) => {
28-
// Block empty segments, current dir refs, and parent dir refs
29-
return segment !== "" && segment !== "." && segment !== "..";
30-
});
31-
32-
// If no valid segments remain, block the request
33-
if (segments.length === 0) {
34-
logger.error("Invalid path provided to openOnUnicode command: path is empty or invalid.");
35-
return;
36-
}
37-
38-
// Check if any segment still contains traversal patterns (encoded or otherwise)
39-
const hasTraversal = segments.some((segment) => {
40-
const decoded = decodeURIComponent(segment);
41-
return decoded === ".." || decoded === "." || decoded.includes("../") || decoded.includes("..\\");
42-
});
43-
44-
if (hasTraversal) {
45-
logger.error("Invalid path provided to openOnUnicode command: path traversal detected.");
46-
return;
47-
}
48-
49-
const sanitizedPath = segments.join("/");
50-
executeCommand("vscode.open", Uri.parse(`https://unicode.org/Public/${sanitizedPath}`));
23+
const resolvedPath = resolveSafePath("/Public/", treeViewOrUri.path);
24+
executeCommand("vscode.open", Uri.parse(`https://unicode.org/${resolvedPath}`));
5125
return;
5226
}
5327

0 commit comments

Comments
 (0)