-
Notifications
You must be signed in to change notification settings - Fork 43
feat(vscode): part 1 - foundation utils and gateway bridge #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bernaferrari
wants to merge
8
commits into
Kilo-Org:dev
Choose a base branch
from
bernaferrari:codex/split-01-foundation-utils-gateway
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
113a1d3
split(vscode): foundation utils and gateway bridge
bernaferrari c8b5f1f
split(vscode): scope part 1 to foundation and defer remote/session utils
bernaferrari 1127421
fix(split-01): clarify org context and drop unused logger util
bernaferrari 6f9aac9
chore(split-01): restore logger utility
bernaferrari f6b0461
fix(split-01): use selected-org helper in profile route
bernaferrari 2912ab3
chore(split-01): keep profile currentOrgId assignment minimal
bernaferrari b2118dc
refactor(split-01): simplify gateway auth helpers and settings parsing
bernaferrari 4374269
chore(split-01): remove extension-settings gateway changes
bernaferrari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import * as vscode from "vscode" | ||
| import { inspect } from "node:util" | ||
|
|
||
| type LogLevel = "debug" | "info" | "warn" | "error" | ||
|
|
||
| // Central logger keeps console output while also writing to a VS Code output channel. | ||
| let outputChannel: vscode.OutputChannel | undefined | ||
| let debugEnabled = false | ||
|
|
||
| export function initializeLogger(channel: vscode.OutputChannel): void { | ||
| outputChannel = channel | ||
| } | ||
|
|
||
| export function setLoggerDebugEnabled(enabled: boolean): void { | ||
| debugEnabled = enabled | ||
| } | ||
|
|
||
| function formatArg(value: unknown): string { | ||
| if (typeof value === "string") { | ||
| return value | ||
| } | ||
| return inspect(value, { depth: 6, colors: false, compact: true, breakLength: 120 }) | ||
| } | ||
|
|
||
| function consoleMethod(level: LogLevel): (...data: unknown[]) => void { | ||
| switch (level) { | ||
| case "error": | ||
| return console.error | ||
| case "warn": | ||
| return console.warn | ||
| default: | ||
| return console.log | ||
| } | ||
| } | ||
|
|
||
| function write(level: LogLevel, ...args: unknown[]): void { | ||
| if (args.length === 0) { | ||
| return | ||
| } | ||
| if (level === "debug" && !debugEnabled) { | ||
| return | ||
| } | ||
|
|
||
| const timestamp = new Date().toISOString() | ||
| const message = args.map((arg) => formatArg(arg)).join(" ") | ||
| outputChannel?.appendLine(`[${timestamp}] [${level.toUpperCase()}] ${message}`) | ||
| consoleMethod(level)(...args) | ||
| } | ||
|
|
||
| export const logger = { | ||
| debug: (...args: unknown[]) => write("debug", ...args), | ||
| info: (...args: unknown[]) => write("info", ...args), | ||
| warn: (...args: unknown[]) => write("warn", ...args), | ||
| error: (...args: unknown[]) => write("error", ...args), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { z } from "zod" | ||
|
|
||
| export const ALLOWED_OPEN_EXTERNAL_SCHEMES = new Set(["https:", "vscode:"]) | ||
|
|
||
| export function parseAllowedOpenExternalUrl(rawUrl: unknown): string | null { | ||
| const parsedInput = z.string().trim().min(1).safeParse(rawUrl) | ||
| if (!parsedInput.success) { | ||
| return null | ||
| } | ||
|
|
||
| let parsedUrl: URL | ||
| try { | ||
| parsedUrl = new URL(parsedInput.data) | ||
| } catch { | ||
| return null | ||
| } | ||
|
|
||
| if (!ALLOWED_OPEN_EXTERNAL_SCHEMES.has(parsedUrl.protocol)) { | ||
| return null | ||
| } | ||
|
|
||
| return parsedUrl.toString() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import fs from "node:fs/promises" | ||
| import path from "node:path" | ||
|
|
||
| export async function realpathOrResolved(targetPath: string): Promise<string> { | ||
| try { | ||
| return await fs.realpath(targetPath) | ||
| } catch { | ||
| return path.resolve(targetPath) | ||
| } | ||
| } | ||
|
|
||
| export function normalizePathForCompare(targetPath: string): string { | ||
| const resolved = path.resolve(targetPath) | ||
| return process.platform === "win32" ? resolved.toLowerCase() : resolved | ||
| } | ||
|
|
||
| export async function isPathInsideAnyRoot(candidatePath: string, roots: readonly string[]): Promise<boolean> { | ||
| const candidateCanonical = normalizePathForCompare(await realpathOrResolved(candidatePath)) | ||
|
|
||
| for (const root of roots) { | ||
| if (!root) { | ||
| continue | ||
| } | ||
| const rootCanonical = normalizePathForCompare(await realpathOrResolved(root)) | ||
| if (candidateCanonical === rootCanonical || candidateCanonical.startsWith(`${rootCanonical}${path.sep}`)) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, expect, it } from "bun:test" | ||
| import { parseAllowedOpenExternalUrl } from "../../src/utils/open-external" | ||
|
|
||
| describe("parseAllowedOpenExternalUrl", () => { | ||
| it("accepts https urls", () => { | ||
| expect(parseAllowedOpenExternalUrl("https://example.com/path?q=1")).toBe("https://example.com/path?q=1") | ||
| }) | ||
|
|
||
| it("accepts vscode urls", () => { | ||
| expect(parseAllowedOpenExternalUrl("vscode://file/c:/tmp/foo.ts")).toBe("vscode://file/c:/tmp/foo.ts") | ||
| }) | ||
|
|
||
| it("rejects unsupported schemes", () => { | ||
| expect(parseAllowedOpenExternalUrl("javascript:alert(1)")).toBeNull() | ||
| expect(parseAllowedOpenExternalUrl("file:///tmp/a.txt")).toBeNull() | ||
| }) | ||
|
|
||
| it("rejects invalid payloads", () => { | ||
| expect(parseAllowedOpenExternalUrl("")).toBeNull() | ||
| expect(parseAllowedOpenExternalUrl("not-a-url")).toBeNull() | ||
| expect(parseAllowedOpenExternalUrl(undefined)).toBeNull() | ||
| expect(parseAllowedOpenExternalUrl(42)).toBeNull() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import fs from "node:fs/promises" | ||
| import os from "node:os" | ||
| import path from "node:path" | ||
| import { describe, expect, it } from "bun:test" | ||
| import { isPathInsideAnyRoot } from "../../src/utils/path-security" | ||
|
|
||
| describe("isPathInsideAnyRoot", () => { | ||
| it("allows paths inside the declared root", async () => { | ||
| const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "kilo-path-security-")) | ||
| const nested = path.join(tempRoot, "a", "b", "file.txt") | ||
| await fs.mkdir(path.dirname(nested), { recursive: true }) | ||
| await fs.writeFile(nested, "ok", "utf8") | ||
|
|
||
| expect(await isPathInsideAnyRoot(nested, [tempRoot])).toBe(true) | ||
| }) | ||
|
|
||
| it("rejects traversal outside the declared root", async () => { | ||
| const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "kilo-path-security-")) | ||
| const outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), "kilo-path-security-outside-")) | ||
| const outsideFile = path.join(outsideRoot, "outside.txt") | ||
| await fs.writeFile(outsideFile, "outside", "utf8") | ||
|
|
||
| const traversal = path.join(tempRoot, "..", path.basename(outsideRoot), "outside.txt") | ||
| expect(await isPathInsideAnyRoot(traversal, [tempRoot])).toBe(false) | ||
| }) | ||
|
|
||
| it("rejects symlink escapes", async () => { | ||
| const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "kilo-path-security-")) | ||
| const outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), "kilo-path-security-outside-")) | ||
| const outsideFile = path.join(outsideRoot, "outside.txt") | ||
| await fs.writeFile(outsideFile, "outside", "utf8") | ||
|
|
||
| const symlinkPath = path.join(tempRoot, "escape-link") | ||
| try { | ||
| await fs.symlink(outsideRoot, symlinkPath) | ||
| } catch (error) { | ||
| if (process.platform === "win32") { | ||
| // Windows CI environments may block symlink creation depending on privileges. | ||
| return | ||
| } | ||
| throw error | ||
| } | ||
| const escapedFile = path.join(symlinkPath, "outside.txt") | ||
|
|
||
| expect(await isPathInsideAnyRoot(escapedFile, [tempRoot])).toBe(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅