Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/cli/qmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import {
setGlobalContext,
listAllContexts,
setConfigIndexName,
normalizeIndexName,
loadConfig,
} from "../collections.js";
import { getEmbeddedQmdSkillContent, getEmbeddedQmdSkillFiles } from "../embedded-skills.js";
Expand Down Expand Up @@ -161,15 +162,7 @@ function getDbPath(): string {
}

function setIndexName(name: string | null): void {
let normalizedName = name;
// Normalize relative paths to prevent malformed database paths
if (name && name.includes('/')) {
const { resolve } = require('path');
const { cwd } = require('process');
const absolutePath = resolve(cwd(), name);
// Replace path separators with underscores to create a valid filename
normalizedName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
}
const normalizedName = name ? normalizeIndexName(name) : name;
storeDbPathOverride = normalizedName ? getDefaultDbPath(normalizedName) : undefined;
// Reset open handle so next use opens the new index
closeDb();
Expand Down
27 changes: 17 additions & 10 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ let currentIndexName: string = "index";
// SDK mode: optional in-memory config or custom config path
let configSource: { type: 'file'; path?: string } | { type: 'inline'; config: CollectionConfig } = { type: 'file' };

export function normalizeIndexName(name: string): string {
const isWindowsAbsolute = /^[a-zA-Z]:[\\/]/.test(name) || /^\\\\/.test(name);
const isPathLike = isWindowsAbsolute || /[\\/]/.test(name);

if (!isPathLike) return name;

let absolutePath = name;
if (!isWindowsAbsolute) {
const { resolve } = require("path");
const { cwd } = require("process");
absolutePath = resolve(cwd(), name);
}

return absolutePath.replace(/[:\\/]+/g, "_").replace(/^_+/, "");
}

/**
* Set the config source for SDK mode.
* - File path: load/save from a specific YAML file
Expand Down Expand Up @@ -99,16 +115,7 @@ export function setConfigSource(source?: { configPath?: string; config?: Collect
* Config file will be ~/.config/qmd/{indexName}.yml
*/
export function setConfigIndexName(name: string): void {
// Resolve relative paths to absolute paths and sanitize for use as filename
if (name.includes('/')) {
const { resolve } = require('path');
const { cwd } = require('process');
const absolutePath = resolve(cwd(), name);
// Replace path separators with underscores to create a valid filename
currentIndexName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
} else {
currentIndexName = name;
}
currentIndexName = normalizeIndexName(name);
}

function getConfigDir(): string {
Expand Down
14 changes: 13 additions & 1 deletion test/collections-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { homedir } from "os";
import { getConfigPath, setConfigIndexName } from "../src/collections.js";
import { getConfigPath, normalizeIndexName, setConfigIndexName } from "../src/collections.js";

// Save/restore env vars around each test
let savedEnv: Record<string, string | undefined>;
Expand Down Expand Up @@ -71,4 +71,16 @@ describe("getConfigDir via getConfigPath", () => {
setConfigIndexName("myindex");
expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "myindex.yml"));
});

test("normalizes Windows absolute paths into safe index names", () => {
expect(normalizeIndexName("C:\\Users\\axulo\\Documents\\ppttest")).toBe(
"C_Users_axulo_Documents_ppttest"
);
});

test("uses a normalized filename when QMD_CONFIG_DIR is set and index name is a Windows path", () => {
process.env.QMD_CONFIG_DIR = "/custom/qmd-config";
setConfigIndexName("C:\\Users\\axulo\\Documents\\ppttest");
expect(getConfigPath()).toBe(join("/custom/qmd-config", "C_Users_axulo_Documents_ppttest.yml"));
});
});