fix(api): stop PHP export values closing their string and keep sibling keys nested [T-2512]
What was wrong
PHP export (GET .../exports?format=php, api/src/controllers/exports.controller.ts:170-171) builds the lang file in api/src/formatters/php.ts, and it had two defects. Any editor or editor API client can set the translation values involved.
-
A translation could close its PHP string early (
php.ts:45-48on develop 5fcb410).escape()used/((\\(?![n\']))|\')/g, which skips a backslash when a quote follows it. If termzzzhas the value\'.system("id")];#, the export isreturn ['zzz'=>'\\'.system("id")];#',];That is valid PHP.
\\is one backslash, the next'closes the string, andsystem("id")runs when the file is included. The repo's own import parser shows the same thing: on this output it fails withUnexpected PHP value: "bin", a.concat of'\\'and asystemcall. Other values break the app that loads the file with a syntax error. -
Only the first child stayed under its parent (
php.ts:23-37). The exporter opened'key'=>[only when the parent key was new, then closed it right after that one child. Termsapp.desc+app.titlecame out asreturn ['app'=>['desc'=>'D',],'title'=>'T',];. This affects every project with two terms under the same dotted prefix (auth.failed,auth.password).
What changed
escape()now uses/(\\(?!n)|')/g->'\\$1'. Every quote is escaped, and so is every backslash that is not followed byn. The existing fixtures (simple.php,double-quotes-out.php,double-and-single-quotes.php) are unchanged and still match byte for byte.phpExporternow builds the whole nested object first, with the same walk asjsonNestedExporterandObject.prototype.hasOwnProperty.call. Then a recursiveserialize()writes'k'=>'v',for strings and'k'=>[...],for objects. Nested objects are null-prototype, so a term like__proto__.xstays an ordinary key andhasOwnProperty.yno longer breaks the walk.- A key that is both a value and a parent now throws the same kind of error as
jsonNestedExporter("flat key that have both a value and sub keys"). It does this in both term orders. Before, value-then-child threwCannot create property 'title' on string 'A', and child-then-value silently wrote the key twice.
Tests
New tests in api/src/formatters/php.spec.ts:
| Test | develop 5fcb410 | this branch |
|---|---|---|
exports zzz = \'.system("id")];#; exact output, and phpParser(output) returns exactly that one term | FAIL ('\\'.system(...) in output) | pass |
exports app.desc + app.title; exact output, and phpParser(output) returns both under app (plus an a.b.c/a.b.d/a.e/f round trip) | FAIL ('title' outside app) | pass |
| a key that is both a value and a parent throws, in both orders | FAIL (TypeError / no error) | pass |
php.spec.ts: 3 failed, 6 passed on develop. All 9 pass on this branch, and the 6 existing tests stay green.
Checks run
yarn --cwd api lint(tslint): passesyarn check-fmt(prettier --check "**/*.ts"at the root, as CircleCI runs it): passesyarn --cwd api test: 25/26 suites and 164/165 tests pass. The one failure isproperties.spec.ts"should export properties files". It fails only on Windows: thepropertieslibrary writes\r\nwhenprocess.platform === "win32", and the fixture uses LF. It has nothing to do with this change and passes on Linux CI.
Fleet task T-2512; authored by the lead lane while the fleet is paused by its daily cost ceiling.
🤖 Generated with Claude Code
Summary by cubic
Fixes two defects in the PHP exporter: exported values are now escaped so they can't close their PHP string early, and sibling keys under the same dotted prefix stay nested under their parent.
- A value such as
\'.system("id")];#no longer exports a string-closing\'; every quote and every backslash except the one in\nis now escaped. - The exporter builds the whole nested object before serializing, so
app.descandapp.titleboth export underapp. - A key that is both a value and a parent now throws a clear error in either term order, instead of a TypeError or a silently duplicated key.