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
10 changes: 5 additions & 5 deletions server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,15 +624,15 @@ function initDb(dataDir) {
}

function getItemsByTag({ app_id, tag }) {
// SQLite JSON: tags is stored as '["bug","ui"]', search with LIKE
// Use json_each() for proper JSON array searching (avoids LIKE injection)
if (app_id) {
return db.prepare(
`SELECT * FROM items WHERE app_id = ? AND tags LIKE ? ORDER BY created_at DESC`
).all(app_id, `%"${tag}"%`);
`SELECT i.* FROM items i, json_each(i.tags) t WHERE i.app_id = ? AND t.value = ? ORDER BY i.created_at DESC`
).all(app_id, tag);
}
return db.prepare(
`SELECT * FROM items WHERE tags LIKE ? ORDER BY created_at DESC`
).all(`%"${tag}"%`);
`SELECT i.* FROM items i, json_each(i.tags) t WHERE t.value = ? ORDER BY i.created_at DESC`
).all(tag);
}

function getDistinctUrls(app_id = 'default') {
Expand Down
19 changes: 19 additions & 0 deletions test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,25 @@ describe('DB — V2 queries', () => {
assert.equal(items.length, 1);
});

it('getItemsByTag is not vulnerable to LIKE wildcards', () => {
dbApi.createItem({
doc: 'd', created_by: 'A',
tags: ['bug', 'ui'],
});
dbApi.createItem({
doc: 'd', created_by: 'B',
tags: ['feature'],
});

// '%' should not match all items (was vulnerable with LIKE pattern)
const wildcard = dbApi.getItemsByTag({ tag: '%' });
assert.equal(wildcard.length, 0);

// Partial match should not work — must be exact
const partial = dbApi.getItemsByTag({ tag: 'bu' });
assert.equal(partial.length, 0);
});

it('getDistinctUrls returns unique source_urls with counts', () => {
dbApi.createItem({ doc: 'd', created_by: 'A', source_url: 'https://a.com' });
dbApi.createItem({ doc: 'd', created_by: 'B', source_url: 'https://a.com' });
Expand Down