fix(api): stop PHP export values closing their string and keep sibling keys nested [T-2512]

Merged
EV
7d ago
+95-25

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.

  1. A translation could close its PHP string early (php.ts:45-48 on develop 5fcb410). escape() used /((\\(?![n\']))|\')/g, which skips a backslash when a quote follows it. If term zzz has the value \'.system("id")];#, the export is

    return ['zzz'=>'\\'.system("id")];#',];
    

    That is valid PHP. \\ is one backslash, the next ' closes the string, and system("id") runs when the file is included. The repo's own import parser shows the same thing: on this output it fails with Unexpected PHP value: "bin", a . concat of '\\' and a system call. Other values break the app that loads the file with a syntax error.

  2. 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. Terms app.desc + app.title came out as return ['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 by n. The existing fixtures (simple.php, double-quotes-out.php, double-and-single-quotes.php) are unchanged and still match byte for byte.
  • phpExporter now builds the whole nested object first, with the same walk as jsonNestedExporter and Object.prototype.hasOwnProperty.call. Then a recursive serialize() writes 'k'=>'v', for strings and 'k'=>[...], for objects. Nested objects are null-prototype, so a term like __proto__.x stays an ordinary key and hasOwnProperty.y no 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 threw Cannot 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:

Testdevelop 5fcb410this branch
exports zzz = \'.system("id")];#; exact output, and phpParser(output) returns exactly that one termFAIL ('\\'.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 ordersFAIL (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): passes
  • yarn check-fmt (prettier --check "**/*.ts" at the root, as CircleCI runs it): passes
  • yarn --cwd api test: 25/26 suites and 164/165 tests pass. The one failure is properties.spec.ts "should export properties files". It fails only on Windows: the properties library writes \r\n when process.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 \n is now escaped.
  • The exporter builds the whole nested object before serializing, so app.desc and app.title both export under app.
  • 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.