Files
wekan/tests/searchHasAttachmentPredicate.test.cjs
T
Lauri Ojansivu 582a22c291 Fix GitHub code scanning code-quality findings.
Real faults: a card's original-position description formatted a promise
because it did not await the history; the attachment swipe distances and
two drag-scroll locals were undeclared globals; a file that cannot be read
during a GridFS upload left the upload open; api.py called exit() instead of
sys.exit(); a rules test redefined a property and a security-log test kept
an unreachable branch. Also 31 missing semicolons, unused imports in the
card details, exporters and attachment API, unused Python imports, and
comments on empty except clauses. The list-sync scanner test cut its source
at the last Meteor.startup, which the new index startup of #6855 had moved.

Thanks to GitHub CodeQL and xet7 !
2026-11-02 14:15:02 +02:00

54 lines
2.7 KiB
JavaScript

'use strict';
// Found by the Haitian attachment-search browser test: the global search's
// has:attachment read each attachment's card from `cardId` - an attachment
// names it in meta.cardId - so it found no card at all; and the negated
// predicates (has:-attachment, has:-checklist: `exists: false`) were ignored,
// so they returned the cards WITH one. Soft-deleted attachments do not count.
// The same top-level cardId made a search by attachment FILENAME (free text
// and attachment:) find nothing either.
// Run: node tests/searchHasAttachmentPredicate.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const root = path.join(__dirname, '..');
const read = p => fs.readFileSync(path.join(root, p), 'utf8');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('searchHasAttachmentPredicate:');
const pub = read('server/publications/cards.js');
const hasBlock = pub.slice(pub.indexOf('case PREDICATE_ATTACHMENT: {'), pub.indexOf('case PREDICATE_DESCRIPTION:'));
test('has:attachment finds cards by meta.cardId, live attachments only', () => {
assert.match(hasBlock, /getAttachments\(attachmentScope, \{ fields: \{ 'meta\.cardId': 1, deletedAt: 1 \} \}\)\)\n\s+\.filter\(isLiveAttachment\)\.map\(a => a\.meta && a\.meta\.cardId\)\.filter\(Boolean\);/);
});
test('a negated has: asks for the cards without one', () => {
assert.strictEqual((hasBlock.match(/selector\.\$and\.push\(\{ _id: has\.exists \? \{ \$in: ids \} : \{ \$nin: ids \} \}\);/g) || []).length, 2, 'attachment and checklist');
// The parser sets exists from the minus sign.
assert.match(read('config/query-classes.js'), /exists: !negated,/);
});
test('a filename search maps attachments to their cards by meta.cardId', () => {
assert.strictEqual((pub.match(/attachments\.map\(attach => attach\.meta && attach\.meta\.cardId\)\.filter\(Boolean\)/g) || []).length, 2);
assert.ok(!/attach\.cardId\b/.test(pub), 'negative: no attach.cardId left');
});
test('negative: no server code reads an attachment\'s card from a top-level cardId', () => {
const walk = dir => fs.readdirSync(path.join(root, dir), { withFileTypes: true }).flatMap(e => {
const rel = path.join(dir, e.name);
if (e.isDirectory()) return ['node_modules', 'tests', '.npm'].includes(e.name) ? [] : walk(rel);
return /\.js$/.test(e.name) ? [rel] : [];
});
for (const file of [...walk('server'), ...walk('models')]) {
const src = read(file);
assert.ok(!/getAttachments\(([^;]*?)\{ fields: \{ cardId: 1 \} \}/.test(src),
`${file}: asks attachments for a top-level cardId - it is meta.cardId`);
}
});
console.log(`searchHasAttachmentPredicate: ${passed} passed`);