Skip to content

Commit 8197ff3

Browse files
committed
feat(vscode): improve override mechanism
This will be changed a bit, this code is pretty much written by AI.
1 parent 7ab4d49 commit 8197ff3

File tree

5 files changed

+752
-164
lines changed

5 files changed

+752
-164
lines changed

vscode/src/commands/generate-override.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ function extractVersionFromContent(content: string): string {
3434
return "16.0.0";
3535
}
3636

37+
function getSelectionSummary(generator: ReturnType<typeof useOverrideGenerator>): string {
38+
const activeSection = generator.activeSection.value;
39+
const definition = generator.activeSectionDefinition.value;
40+
41+
if (!activeSection || !definition) {
42+
return "";
43+
}
44+
45+
if (definition.mode === "range" && activeSection.range) {
46+
return `${definition.label}: lines ${activeSection.range.start}-${activeSection.range.end}`;
47+
} else if (definition.mode === "lines" && activeSection.lines.length > 0) {
48+
return `${definition.label}: ${activeSection.lines.length} line(s)`;
49+
}
50+
51+
return `${definition.label}: not set`;
52+
}
53+
3754
export function useGenerateOverrideCommand() {
3855
const activeEditor = useActiveTextEditor();
3956
const generator = useOverrideGenerator();
@@ -47,21 +64,44 @@ export function useGenerateOverrideCommand() {
4764
}
4865

4966
if (generator.mode.value === "selecting") {
50-
const action = await window.showQuickPick(
51-
[
52-
{
53-
label: `$(check) Confirm (lines ${generator.selectionStart.value}-${generator.selectionEnd.value})`,
54-
action: "confirm",
55-
},
56-
{ label: "$(close) Cancel Selection", action: "cancel" },
57-
],
67+
const doneCount = generator.doneSections.value.length;
68+
const totalCount = generator.sections.value.length;
69+
const summary = getSelectionSummary(generator);
70+
71+
const items = [
5872
{
59-
placeHolder: "Override selection is active. Click lines in editor to adjust.",
73+
label: generator.activeSection.value
74+
? `$(check) Confirm ${generator.activeSectionDefinition.value?.label ?? "Section"}`
75+
: `$(check) Finish (${doneCount}/${totalCount} sections)`,
76+
action: "confirm" as const,
77+
description: summary,
6078
},
61-
);
79+
{ label: "$(close) Cancel Selection", action: "cancel" as const },
80+
];
81+
82+
const action = await window.showQuickPick(items, {
83+
placeHolder: generator.activeSection.value
84+
? `Editing ${generator.activeSectionDefinition.value?.label}. Click lines in editor to adjust.`
85+
: "All sections complete. Confirm to generate override.",
86+
});
6287

6388
if (action?.action === "confirm") {
64-
const override = generator.confirm();
89+
if (generator.activeSection.value) {
90+
const confirmed = generator.confirmActiveSection();
91+
if (!confirmed) {
92+
window.showWarningMessage("Selection is not valid. Please adjust the selection.");
93+
return;
94+
}
95+
96+
if (generator.activeSection.value) {
97+
window.showInformationMessage(
98+
`Section confirmed. Now editing: ${generator.activeSectionDefinition.value?.label}`,
99+
);
100+
return;
101+
}
102+
}
103+
104+
const override = generator.confirmAll();
65105
if (override) {
66106
const json = JSON.stringify(override, null, 2);
67107
await env.clipboard.writeText(json);
@@ -86,7 +126,7 @@ export function useGenerateOverrideCommand() {
86126
await vscodeCommands.executeCommand("ucd:selection.focus");
87127

88128
window.showInformationMessage(
89-
`Selection mode active (lines ${detected.start}-${detected.end}). Click to set start, click again to set end. Run command again to confirm.`,
129+
`Selection mode active. Editing: Heading (lines ${detected.start}-${detected.end}). Click to adjust. Run command again to confirm.`,
90130
);
91131
});
92132
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,35 @@ export function useOpenOnUnicodeCommand() {
1919
return;
2020
}
2121

22-
// TODO: This would allow to traverse upwards, this should be blocked.
23-
executeCommand("vscode.open", Uri.parse(`https://unicode.org/Public/${treeViewOrUri.path}`));
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}`));
2451
return;
2552
}
2653

0 commit comments

Comments
 (0)