Code Traps - edmozley/freeitsm GitHub Wiki
Some code in FreeITSM looks as if it could be simpler, and isn't. Usually a real install once proved why: a bug that lost data, leaked across companies or broke silently. Those rules are marked in the code itself, in one shape, so they're hard to miss and easy to find.
// TRAP: never trust a serviceUrl the token does not vouch for.
// Replies are POSTed to it WITH THE BOT'S ACCESS TOKEN, so an activity that
// could name its own serviceUrl could collect that token.-
TRAP:then the rule, in one sentence, as an instruction. - Then why, in a line or two — what goes wrong if you ignore it.
- The full story, if there is one, lives in the developer guide the file's header points to, not in the comment.
git grep -n "TRAP:" # every trap in the codebase
git grep -n "TRAP:" -- includes/messaging # the ones in one areaBefore changing a file, read its TRAP: lines. They are written for people and for AI coding assistants alike: a trap is the thing a confident edit is most likely to undo.
-
A file that throws
ServiceErrorloadsincludes/service_context.phpitself - never relies on its caller. A missing class is a fatal error, not an exception, so the one caller that hadn't loaded it took a whole page down (#288).tests/service-error-loaded.phpenforces it. -
A centred row that can scroll uses
justify-content: safe center, never plaincenter. Centred content wider than its box spills past both edges, and the part past the start edge is outside the scroll range - no scrolling reaches it - and is not counted inscrollWidth, so a "does the container fit?" test passes while a button is lost (#411). Check each item's rectangle, not the container's width. -
To get text out of HTML in the browser, parse with
DOMParser, neverinnerHTMLon an element of the page. An element made bydocument.createElementbelongs to the live document: even detached, it loads<img src>and firesonerror, so a stored<img onerror>runs on the page. ADOMParserdocument is inert (#405). Where the HTML has already been through the sanitiser this is belt and braces; where it has not, it is the only safe way. -
PHP keeps one
header_register_callback()per request - a second one silently replaces the first.includes/login_return.phpuses it to add the return address to every redirect to the sign-in page; anything else that needs a header callback must chain to it (#407). -
Anything that walks a workflow's
actionsmust walk into branches (branch_if/branch_switchhold their own action lists). Webhook encryption looked only at the top level, so a webhook inside a branch would have been stored in plain text - and the editor would have got it back still encrypted and encrypted it again on the next save. UsewebhookMapWebhooks()orWorkflowEngine::mapActionTree()(#364). -
A war room column added in a release is read only through
warRoomHasCol()(includes/warroom.php). One unguarded clause in the channel list's unread count -wm.thread_root_id- returned HTTP 500 for the whole war room on an install that had not yet run Database Verification. Without the column, the feature switches off and administrators get a notice (War Room - Developer Guide §7f). The same rule is worth following in any module: a page should never depend on a column that only Database Verification makes. -
The shared header (
includes/waffle-menu.php) is on every analyst page, so one wrong tag in it breaks them all. A stray</div>after a dialog closed each page's own layout box in 5.12.0: Tickets → Settings drew nothing (#453 / #454).tests/template-div-balance.php(CI) checks every template's divs balance;tests/page-div-balance.phpchecks 124 real pages on a test install. -
No
autofocuson a page that is shown inside another page's frame. The browser scrolls the outer page to show the focused field: System → Branding opened scrolled past its heading because its sign-in preview autofocused the email box. The sign-in pages skip it when?preview=1. -
Ticket audit lines are written by the server, in the save - never from the browser.
logAudit()ininbox.jsis a deliberate no-op andlog_ticket_audit.phpwrites nothing (#430). Bringing either back writes every line twice; a new ticket field is audited inTicketsService::updateTicket()'s$audits. -
A folder that holds stored files is created through the function that guards it, even when it is only a cache. On an S3 install a custom Files storage folder may never have been created, so the first offline copy's plain
mkdirmade<root>/offlinewith no.htaccessorweb.config.copyKey()andkeepUploadCopy()now callfilesStorageRoot()first, which runsuploadPrepareDir()(PR #488).filesOfflineRoot()must stay side-effect free - every S3 download calls it. -
"Live folders only" in Knowledge goes through
knowledgeLiveFolderSql(), never a hand-writtenis_deleted = 0(includes/knowledge/visibility.php).knowledge_folders.is_deletedarrives with Database Verification but the code arrives with the upgrade, so one bareis_deleted = 0fails every Knowledge request on an install that has not verified yet. The helper adds the clause only once the column exists (#492).
Add a TRAP: when you fix something that a sensible-looking change would bring back — and a test that fails if it does. Keep it short: a trap that runs to a paragraph is a design note, and belongs in the wiki.
Older code marks the same kind of rule with 🔴 or ⚠️ and a longer explanation. Those are just as binding; new ones use TRAP: so they can be listed.
See also: Teams and Mattermost — Developer Guide (where the convention started) · Telegram — Developer Guide: the traps