Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(cli): validate query parameters limit and folder_id in CLI bridge endpoints (#2014) (#2015)

Henry Sua81d63659065
diff --git a/src/helpers/cliBridge.js b/src/helpers/cliBridge.js
index b3e18a858..11a54d45e 100644
--- a/src/helpers/cliBridge.js
+++ b/src/helpers/cliBridge.js
@@ -84,6 +84,18 @@ function parseIdParam(value) {
return id;
}
+function parsePositiveIntQuery(query, key, fallback) {
+ const raw = query.get(key);
+ if (raw === null || raw === "") return fallback;
+ const num = Number(raw);
+ if (!Number.isInteger(num) || num <= 0) {
+ const err = new Error(`Query parameter '${key}' must be a positive integer`);
+ err.code = "VALIDATION";
+ throw err;
+ }
+ return num;
+}
+
function unwrapMutationResult(result, label) {
if (!result?.success || !result[label]) {
throw new Error(result?.error || `Failed to write ${label}`);
@@ -372,8 +384,8 @@ class CliBridge {
exact("GET", "/v1/health", () => ({ data: { ok: true, version: 1 } })),
exact("GET", "/v1/notes/list", ({ query }) => {
const noteType = query.get("note_type") || null;
- const limit = query.get("limit") ? Number(query.get("limit")) : 100;
- const folderId = query.get("folder_id") ? Number(query.get("folder_id")) : null;
+ const limit = parsePositiveIntQuery(query, "limit", 100);
+ const folderId = parsePositiveIntQuery(query, "folder_id", null);
const notes = db.getNotes(noteType, limit, folderId);
return { data: notes, has_more: false, next_cursor: null };
}),
@@ -384,7 +396,7 @@ class CliBridge {
err.code = "VALIDATION";
throw err;
}
- const limit = query.get("limit") ? Number(query.get("limit")) : 20;
+ const limit = parsePositiveIntQuery(query, "limit", 20);
const notes = db.searchNotes(q, limit);
return { data: notes, has_more: false, next_cursor: null };
}),
@@ -523,7 +535,7 @@ class CliBridge {
return { data: { text: result.text, provider, model } };
}),
exact("GET", "/v1/transcriptions/list", ({ query }) => {
- const limit = query.get("limit") ? Number(query.get("limit")) : 50;
+ const limit = parsePositiveIntQuery(query, "limit", 50);
return {
data: db.getTranscriptions(limit),
has_more: false,
diff --git a/test/helpers/cliBridgeQueryValidation.test.js b/test/helpers/cliBridgeQueryValidation.test.js
new file mode 100644
index 000000000..4ec978684
--- /dev/null
+++ b/test/helpers/cliBridgeQueryValidation.test.js
@@ -0,0 +1,143 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const Module = require("node:module");
+
+const originalLoad = Module._load;
+Module._load = function patchedLoad(request, parent, isMain) {
+ if (request === "electron") {
+ return {
+ app: {
+ getPath: () => "/tmp",
+ getAppPath: () => process.cwd(),
+ isReady: () => false,
+ },
+ };
+ }
+ if (request === "./windowBroadcast") {
+ return {
+ broadcastToWindows: () => {},
+ };
+ }
+ return originalLoad.call(this, request, parent, isMain);
+};
+
+const CliBridge = require("../../src/helpers/cliBridge.js");
+
+Module._load = originalLoad;
+
+function createBridge(dbMocks = {}) {
+ const bridge = new CliBridge({
+ databaseManager: {
+ getNotes: () => [],
+ searchNotes: () => [],
+ getTranscriptions: () => [],
+ ...dbMocks,
+ },
+ });
+ bridge.token = "test-token";
+ bridge.port = 8200;
+ return bridge;
+}
+
+class MockResponse {
+ constructor() {
+ this.statusCode = null;
+ this.headers = {};
+ this.body = "";
+ }
+ writeHead(code, headers) {
+ this.statusCode = code;
+ this.headers = headers;
+ }
+ end(chunk) {
+ if (chunk) this.body += chunk;
+ }
+}
+
+async function request(bridge, url) {
+ const req = {
+ socket: { remoteAddress: "127.0.0.1" },
+ headers: { authorization: "Bearer test-token" },
+ method: "GET",
+ url,
+ };
+ const res = new MockResponse();
+ await bridge._handleRequest(req, res);
+ const parsed = res.body ? JSON.parse(res.body) : null;
+ return { status: res.statusCode, data: parsed };
+}
+
+test("GET /v1/notes/list accepts valid limit and folder_id parameters", async () => {
+ let passedLimit = null;
+ let passedFolderId = null;
+ const bridge = createBridge({
+ getNotes(_type, limit, folderId) {
+ passedLimit = limit;
+ passedFolderId = folderId;
+ return [{ id: 1, title: "Note 1" }];
+ },
+ });
+
+ const res = await request(bridge, "/v1/notes/list?limit=25&folder_id=7");
+ assert.equal(res.status, 200);
+ assert.equal(passedLimit, 25);
+ assert.equal(passedFolderId, 7);
+ assert.deepEqual(res.data.data, [{ id: 1, title: "Note 1" }]);
+});
+
+test("GET /v1/notes/list uses defaults when parameters are omitted", async () => {
+ let passedLimit = null;
+ let passedFolderId = null;
+ const bridge = createBridge({
+ getNotes(_type, limit, folderId) {
+ passedLimit = limit;
+ passedFolderId = folderId;
+ return [];
+ },
+ });
+
+ const res = await request(bridge, "/v1/notes/list");
+ assert.equal(res.status, 200);
+ assert.equal(passedLimit, 100);
+ assert.equal(passedFolderId, null);
+});
+
+test("GET /v1/notes/list rejects non-numeric or non-positive limit with 400", async () => {
+ const bridge = createBridge();
+
+ for (const badLimit of ["abc", "-5", "0", "1.5"]) {
+ const res = await request(bridge, `/v1/notes/list?limit=${badLimit}`);
+ assert.equal(res.status, 400, `limit=${badLimit} should return 400`);
+ assert.equal(res.data.error.code, "validation_error");
+ assert.match(res.data.error.message, /must be a positive integer/);
+ }
+});
+
+test("GET /v1/notes/list rejects non-numeric or non-positive folder_id with 400", async () => {
+ const bridge = createBridge();
+
+ for (const badFolder of ["abc", "-1", "0", "2.5"]) {
+ const res = await request(bridge, `/v1/notes/list?folder_id=${badFolder}`);
+ assert.equal(res.status, 400, `folder_id=${badFolder} should return 400`);
+ assert.equal(res.data.error.code, "validation_error");
+ assert.match(res.data.error.message, /must be a positive integer/);
+ }
+});
+
+test("GET /v1/notes/search rejects invalid limit with 400", async () => {
+ const bridge = createBridge();
+
+ const res = await request(bridge, "/v1/notes/search?q=test&limit=invalid");
+ assert.equal(res.status, 400);
+ assert.equal(res.data.error.code, "validation_error");
+ assert.match(res.data.error.message, /must be a positive integer/);
+});
+
+test("GET /v1/transcriptions/list rejects invalid limit with 400", async () => {
+ const bridge = createBridge();
+
+ const res = await request(bridge, "/v1/transcriptions/list?limit=zero");
+ assert.equal(res.status, 400);
+ assert.equal(res.data.error.code, "validation_error");
+ assert.match(res.data.error.message, /must be a positive integer/);
+});