← All case studies

// engineering

Engineering Notes: Migrating a Live Portal Under Deadline

MigrationDebuggingWeb security

Overview

A consulting client ran a portal where staff complete structured client reviews that sync into a CRM. The portal's original domain could no longer be managed, so everything had to move to a new domain on a fixed deadline, without losing the live CRM integration or interrupting agents who used the portal during client calls.

I rebuilt the portal on the new domain, verified the CRM routing end to end, and moved agents across with no data loss. These are four moments from that work that show how I approach a migration: debug from evidence, ship carefully, and always know the blast radius of a change.

1. Tracing a silent CRM-sync failure to an environment-variable change

During the migration I ran a routine end-to-end test: submit a review, confirm it reaches the CRM. It didn't. No error was shown, the review saved fine, it just never arrived in the CRM.

The dangerous part was that the failure was silent. The app told the agent "review saved", and it was, to the app's own database. The CRM sync happened afterward and failed quietly. From the outside nothing looked broken. This is the kind of bug that goes live and loses real client data before anyone notices.

My first assumption was that the CRM access token had expired. Reasonable, but I didn't want to act on a guess, so I pulled the full history of submitted reviews and their routing outcomes:

routing_history id | user | routed_to | status | day
----+------+-----------+--------+-----
 10 |   2  | SUB_A     | synced |  0
 11 |   2  | SUB_A     | synced |  0
 17 |   7  | SUB_B     | synced |  4
 18 |   2  | (none)    | failed | 19   <- the failure

Two things stood out. Account 2 worked on day 0 and failed on day 19. Account 7 worked throughout. So routing wasn't globally broken; something changed for one account between those points.

The decisive detail was the exact error. It wasn't "token rejected" (which the CRM returns as a 401). It was "no valid credentials", which the code throws when the token can't even be decrypted. Tokens are stored encrypted and decrypted with a key held in an environment variable. So I tested decryption directly:

decryption_teststored token is well-formed: correct IV, auth tag, ciphertext length
but decryption throws:
"Unsupported state or unable to authenticate data"

That error means authentication failed. A wrong key produces it, though corrupted data would too, so the error alone did not settle it. The token's structure was intact, which pointed away from corruption, and the deploy platform's environment-variable history did the rest: the encryption key had been changed three weeks earlier, deliberately, to align with the CRM vendor's setup. On that date the active agents' tokens were re-encrypted with the new key. One test account was missed, so its token stayed encrypted under the old key and failed silently from then on. The token was fine. The key no longer matched it.

The finding wasn't "one test account is broken." It was confirmation that token encryption is tied to a single environment variable, and that changing it silently breaks every token not re-encrypted at the same time. I verified every live agent's token decrypted under the current key, which cleared the migration for go-live.

The fastest path to the real cause wasn't reading code, it was reading the data (the routing history) and the infrastructure timeline (the env-var change log). The bug was invisible in the application and obvious in the audit trail.

2. Migrating a live portal without a maintenance window

Agents used the portal daily, during live client calls. There was no window to take it down, so every change had to reach the live domain without an interruption anyone would notice.

I built the new portal one working piece at a time, login, then route protection, then the home page, then each remaining page. Each piece was built locally, tested end to end, committed, and pushed only when it was a complete, working unit. The live site updated once per finished piece, never in a half-built state.

For example, the login change was three edits: switch where the session token is stored, add the login page, and change route protection to use it. I committed them locally but held the push until all three were done and tested together:

release_batchingbuilt and tested locally, then pushed as one unit:
  - switch session storage
  - add /login page
  - protect routes via /login

one push -> one deploy -> live and working in a single step

If I'd pushed each edit as I made it, the live site would have spent time in states where login half-worked, exactly the thing an agent would hit mid-call. Batching to complete units meant the live domain only ever moved between working states.

The tradeoff is discipline: more local testing before each push, and resisting the urge to ship a change the moment it compiles. For a portal people depend on during live calls, that's the right trade.

3. An open-redirect guard, and the bypasses the first two versions missed

After login, the app sends the agent back to wherever they were trying to go, passed as a next parameter in the URL. A protected page redirects to /login?next=/history, and login sends them on to /history afterward.

The risk is that next comes from the URL, which an attacker controls. A naive version lets ?next=https://evil.comthrough, and the login page cheerfully redirects a just-authenticated user to a malicious site. That is a classic open redirect, and it is phishing gold.

The obvious guard is to only honour next when it is a path on this site. My first version was a shape check: the value has to start with a single slash, and not // or /\.

safe-next.js (version 1)function safeNext(raw) {
  if (typeof raw !== 'string' || !/^\/(?![/\\])/.test(raw)) return '/review';
  return raw;
}

That catches the textbook bypasses, but it checks the shape of the string rather than where it actually leads. Browsers strip tab and newline characters when they parse a URL, so a slash, a tab, then/evil.com passes the check as written and becomes//evil.com once the browser reads it.

The second version asked the URL parser instead of guessing: resolve the value against the site's own origin, confirm the origin didn't change, and return the parsed path.

safe-next.js (version 2)function safeNext(raw) {
  if (typeof raw !== 'string' || !/^\/(?![/\\])/.test(raw)) return '/review';
  try {
    const url = new URL(raw, window.location.origin);
    if (url.origin !== window.location.origin) return '/review';
    return url.pathname + url.search + url.hash;   // <- the hole
  } catch {
    return '/review';
  }
}

That closes the tab and newline case, and it looked airtight. Testing it against dot-segments showed otherwise. ?next=/.//evil.compasses the opening check, resolves to this site's own origin, and then normalises to a pathname of //evil.com. Used as a URL, for example assigned to window.location, that is protocol-relative, and the browser leaves for https://evil.com. Still an open redirect.

The fix is to also check the value that actually gets used, not just the parse:

safe-next.js (version 3, the added lines)const out = url.pathname + url.search + url.hash;
if (!out.startsWith('/') || out.startsWith('//') || out.startsWith('/\\')) return '/review';
return out;

I tested the fixed version against legitimate paths and the bypasses I could find: //evil.com, /\evil.com,/.//evil.com, /%2e//evil.com,/a/..//evil.com, https://evil.com,javascript: URIs, and tab and newline variants. Only genuine same-site paths pass.

The lesson

A guard that validates the input isn't the same as one that validates the output.

The origin check proved the URL resolved to my site; it didn't prove the string I then handed back was safe. Checking the thing you actually use, not just the thing you were given, is the difference.

4. Renaming a route without breaking a live integration

A URL in the app needed renaming, a cosmetic change for consistency.

The trap was that the same word appeared in a dozen other places: a component folder, variable names, draft-storage keys, backend field names, and a CRM tag. A careless find-and-replace would have renamed all of them. Most would have been harmless. One would not: a tag written to the CRM on every submission, which a live automation keyed off. Rename that tag and the automation silently stops firing.

So I scoped the change to the user-facing route and the internal links pointing at it, and explicitly left everything a system depends on untouched:

rename_scopeRENAME:  the page route, and the links to it
LEAVE:   the component folder name   (cosmetic, invisible to users)
         the draft-storage keys      (agents have in-progress work under them)
         the backend field names     (the API contract)
         the CRM tag                 (a live automation depends on it)

Then I added a redirect from the old path to the new one, so existing bookmarks and any externally-stored links kept working.

The instinct to "clean it all up while I'm here" is how a one-line cosmetic change takes down a billing or routing workflow. The skill isn't making the change, it's knowing the blast radius and drawing the line at the user-facing layer. History (old links still work) and the live integration (untouched) both survived.

Outcome

All client names, domains, account identifiers and infrastructure details have been removed or anonymised. Code snippets are illustrative, not production source.