Code Traps - edmozley/freeitsm GitHub Wiki

Code traps

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.

Finding them

git grep -n "TRAP:"                         # every trap in the codebase
git grep -n "TRAP:" -- includes/messaging   # the ones in one area

Before 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 trap that applies everywhere

  • A file that throws ServiceError loads includes/service_context.php itself - 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.php enforces it.

  • A centred row that can scroll uses justify-content: safe center, never plain center. 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 in scrollWidth, 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, never innerHTML on an element of the page. An element made by document.createElement belongs to the live document: even detached, it loads <img src> and fires onerror, so a stored <img onerror> runs on the page. A DOMParser document 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.php uses 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 actions must walk into branches (branch_if / branch_switch hold 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. Use webhookMapWebhooks() or WorkflowEngine::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.php checks 124 real pages on a test install.

  • No autofocus on 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() in inbox.js is a deliberate no-op and log_ticket_audit.php writes nothing (#430). Bringing either back writes every line twice; a new ticket field is audited in TicketsService::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 mkdir made <root>/offline with no .htaccess or web.config. copyKey() and keepUploadCopy() now call filesStorageRoot() first, which runs uploadPrepareDir() (PR #488). filesOfflineRoot() must stay side-effect free - every S3 download calls it.

  • "Live folders only" in Knowledge goes through knowledgeLiveFolderSql(), never a hand-written is_deleted = 0 (includes/knowledge/visibility.php). knowledge_folders.is_deleted arrives with Database Verification but the code arrives with the upgrade, so one bare is_deleted = 0 fails every Knowledge request on an install that has not verified yet. The helper adds the clause only once the column exists (#492).

Writing one

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

⚠️ **GitHub.com Fallback** ⚠️