Skip to content

API Uploads and Knowledge Documents PR Review

Ed Mozley edited this page Nov 28, 2026 · 1 revision

API uploads and article documents - PR review

PRs #446 (documents on Knowledge articles) and #447 (chunked uploads) by @Dr4inder Β· Issue #416 Β· Verdict: merge + fix, both Β· 5.12.0

Two PRs built on the agreed design, kept apart on purpose ("one feature per PR"), #447 stacked on #446:

  • #446 - integrations can list, add, attach, change, download and detach the documents in an article's Documents panel, through the REST API.
  • #447 - any picture or document can reach the API in pieces, so files get past PHP's and a proxy's size limits, and a dropped connection resumes. The Files module already did this in the browser; this gives the REST API the same.

How to use them: REST API - Knowledge Β§ Documents and Β§ Big files.

βœ… What was good - keep it this way

What Why it matters
Every route reaches a document through the article, as the key sees it (KnowledgeTransferService::object()), and checks it is on that article Revoking access to article A closes A's route at once, while a real grant through B still works through B
DocumentsService split into parts (prepare/insert/indexAdded, link, unlink/afterUnlink) with add/attach/detach rebuilt from them - diffed line by line, unchanged for the panel and the project API One write path for the page and the API; the project API suite passes untouched
Help Centre off by default, in the same INSERT A document is never, even for a moment, shown to customers by accident
Retries through createOnce - an advisory lock and the reference row in the same transaction; a replay writes nothing; removed since is 404 Safe to stop and run a migration again
The article revision covers documents only when it has some Articles without documents keep the revision they had before the upgrade
api/knowledge/document.php - the same 404 for every refusal, nothing for anyone not signed in, portal readers also need the link's Help Centre flag A hidden document stays hidden even when the article's HTML links to it
Uploads copy every gate of a normal upload - our extension list, magic bytes that fail closed, a random name on disk - and re-check the limit in force at hand-over A PHP file named .png, text named .pdf and SVG never land
An upload belongs to one key; upload_id is random and checked by pattern, the file on disk has a second random name Another key - even the same analyst's - gets 404; no path can be built from the request
append() locks the row, checks the offset, truncates before writing, streams at most limit + 1 bytes Pieces arrive in order, never twice; memory stays flat
Thorough tests over real HTTP - 74 and 52 checks, a fresh-process control proving why the analyst's own rule could not be reused, a 30 MB file through PHP's default limits The behaviour is proved, not described

πŸ”§ What was added and fixed when merging

1. A company-scoped key could attach another company's document πŸ”΄ (#446 - fixed before it reached main)

Attaching an existing document asked documentCanView() first - the analyst's rule, which knows nothing of the key. API keys can be narrowed to fewer companies than their analyst, so a key limited to company B, whose analyst also sees company A, passed that check for a document on one of A's articles or tickets, attached it to a B article - and could then download it through B. Document ids are sequential, so they could be walked.

Now a company-scoped key only gets the second path, which applies the key's own scope:

// TRAP: documentCanView() is the ANALYST's rule and knows nothing of the key's company scope
if ($ctx->companyScope === null && documentCanView($conn, $ctx->actorId, $allowed, $documentId)) return true;
// ...otherwise only through a Knowledge article the KEY can read

Test: a key scoped to one company tries to attach a document on another company's article - 404, and no content - with an unscoped-key control that still reaches it. Without the fix it is 201 and 200. The test's first version used an article with no company, which in Knowledge means shared with every company - so the key was right to reach it. Worth knowing when writing any Knowledge scope test.

2. A picture's retry was tied to the upload, not the bytes 🟠 (#447)

The design says "the same client_reference with the same bytes is a replay". Documents followed that (filename + sha256); pictures from an upload used the request body - which carries the upload_id. A client whose hand-over answer was lost, and who uploaded the same picture again, got 409 reference_conflict instead of its picture back. The identity is now filename + sha256 (+ reference). Test: a second upload of the same bytes under the same reference is 200, replayed: true.

3. Later calls on an upload skipped two checks 🟑 (#447)

POST /uploads checked the purpose's module was on and the key's analyst could still change Knowledge; PUT, GET and DELETE on the upload did not. They now do (apiUploadsRequireReach()). GET /uploads/limits needed no permission at all; it now needs either purpose's create permission.

Smaller (#447)

  • Two pieces of one upload at once: the second waits on the row lock; past innodb_lock_wait_timeout it was a 500. Now 409 upload_busy, telling the client to ask how far it got.
  • api_uploads.api_key_id gets fk_api_uploads_key ... ON DELETE CASCADE, like api_key_rate_limits, in freeitsm.sql and Database Verification. A fresh freeitsm.sql import was run to prove a new install still completes (the 5.8.0 fresh-install fault was a foreign key).

Left as it is

  • A shared document's title and description can't be changed through one article (409 document_shared). Safe for a first version; an importer that attached its own document to two articles must rename it elsewhere. Recorded in the design page's decisions.
  • upload_id on project documents - in the design, not built yet.
  • The PUT badge colour on the API docs page is a hex like its neighbours.

πŸ§ͺ What was checked

Test Result
tests/knowledge-documents-api.php (#446's, + the company check) 76 passed
tests/api-uploads.php (#447's, + the picture replay) 53 passed
tests/knowledge-migration-api.php, project-documents-api.php, knowledge-help-centre-documents.php 67, 45, 19 passed
tests/file-stores.php, web-exposure-guard.php, get-must-not-write.php, analyst-lifecycle.php, constraint-names-unique.php all pass
api/v1/lib/openapi_check.php PASS
A fresh freeitsm.sql import into an empty database completes; 505 tables, the admin, the new FK

All on a disposable test install with Database Verification run first.


See also: Large files and article documents - design Β· REST API - Knowledge Β· Knowledge

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally