Skip to content

Knowledge Recycle Tags PR Review

Ed Mozley edited this page Dec 5, 2026 · 2 revisions

Tags in the Knowledge recycle bin - PR review (#517)

PR #517 by @Dr4inder Β· Knowledge Β· merged 5 December 2026, in 6.0.0

Verdict: merge + fix. A good idea with a good screen; the loading moved into the service layer, and the test had to stop using eval().

What it set out to do

Archived articles keep their tags, but the recycle bin did not show them. A tag could show (0) in the sidebar - it counts live articles - yet stay in use, and Delete unused tags would not remove it, with no way to see which archived article was holding it. Now every archived article shows its tags after its title.

Before After
Before: archived articles with no tags After: wifi, p1 and runbook beside the archived articles

What was good - copy this

  • Visibility first, tags second. The tags are loaded only for the article ids the recycle bin's visibility filter already allowed, so a restricted article's tags cannot leak through the new query.
  • One query for the whole bin, parameterised, not one per article.
  • The existing escaped tag badges (.article-tag) reused, with titles and tags wrapping on a narrow screen.
  • A browser test of the real renderer and styles (tests/knowledge-recycle-tags-browser.php, 7 checks) that includes a tag named <img src=x onerror=alert(1)>, Unicode, and the layout at 320px.
  • It deliberately kept a destructive job out of its test - the recycle bin's list purges expired articles first, and the PR said so.

What had to be fixed

1. The test ran eval() on code cut out of the endpoint. To call the list without the endpoint's HTTP bootstrap, the database test did this:

// Run the actual read adapter without its HTTP bootstrap or unrelated destructive retention job.
function purgeExpired($conn) {}
function getRetentionDays($conn) { return 0; }
$source = file_get_contents($root . '/api/knowledge/knowledge_archive.php');
if (!preg_match('/^function handleList\([^\n]*\).*?^\}/ms', $source, $match)) throw new RuntimeException('Cannot find handleList');
eval($match[0]);

It was not dangerous - it only ever read FreeITSM's own file - but it is fragile and it tests something that never runs: a regex that finds the function's end by the first } at the start of a line breaks on any reformatting, the stubs replace real behaviour, and an eval() in the codebase is the first thing a security review flags. The rewritten test checks the service function directly and then calls the real endpoint over HTTP as a ZZ analyst who is not an administrator, with its fixtures removed in a finally block. It carries the rule as a trap:

 * TRAP: never eval() a function out of an endpoint file to test it. The first version
 * of this test did, and it broke on any reformatting of the file and skipped the
 * endpoint's own bootstrap. Test the service function, and the endpoint over HTTP.

2. The loading belongs in KnowledgeService. The query lived in the endpoint. It is now KnowledgeService::tagsForArticles(), and the main article list uses it too - that list ran one query per article:

public static function tagsForArticles(PDO $conn, array $articleIds): array
{
    $ids = array_values(array_unique(array_filter(array_map('intval', $articleIds))));
    if (!$ids) return [];
    $st = $conn->prepare("SELECT kat.article_id, t.id, t.name
                            FROM knowledge_article_tags kat
                            JOIN knowledge_tags t ON t.id = kat.tag_id
                           WHERE kat.article_id IN (" . implode(',', array_fill(0, count($ids), '?')) . ")
                           ORDER BY t.name, t.id");
    $st->execute($ids);
    ...
}

Lesson: when two screens need the same data, the second one is the moment to move it into the service; and test an endpoint by calling it.

How it was tested

On a test install: the rewritten database test passes 10 of 10 (the service function, then the endpoint: tagged and untagged articles listed, both tags in name order, a restricted article and its tag absent, a live article not in the bin), the browser test 7 of 7, and the main article list still returns each article's tags.

Files

File Change
πŸ”΄ includes/services/knowledge.php tagsForArticles() (fix)
🟑 api/knowledge/knowledge_archive.php Tags on each archived article
🟑 api/knowledge/knowledge_articles.php One query for a page's tags (fix)
🟑 assets/js/knowledge.js, assets/css/knowledge.css The tags after the title
🟒 tests/knowledge-recycle-tags.php Rewritten without eval() (fix)
🟒 tests/knowledge-recycle-tags-browser.php New

What this means for you

Open the recycle bin to see which archived article holds a tag; restore or delete it to free the tag. See Knowledge.

Related

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally