Skip to content

Commit ab78d7d

Browse files
ondrejmirtesclaude
andcommitted
Refuse to trust types when opcache.file_cache is configured
The trusted-types pass rewrites op_arrays inside opcache's optimizer, before the script is persisted. With opcache.file_cache set the stripped op_arrays are written to disk, and a later run that never armed the pass - a --debug run, or one without the extension loaded - executes them without their argument and return type checks. Refuse to arm in pt_trusted_types_set_prefix() when a file cache is configured. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 4022659 commit ab78d7d

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎turbo-ext/src/TrustedTypes.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* The switch lives in the optimizer: opcache lets extensions register
1515
* passes (zend_optimizer_register_pass) that run at the end of
1616
* zend_optimize_script(), before the script is persisted, so what shared
17-
* memory (and a file cache) hold is the stripped code. For every function
17+
* memory holds is the stripped code — which is why arming is refused when
18+
* opcache.file_cache is set: the stripped code would outlive the run and
19+
* reach later unarmed ones. For every function
1820
* of a script whose filename starts with the trusted prefix — the running
1921
* phar, handed over by TurboExtensionEnabler::trustOwnTypesIfSuitable():
2022
*
@@ -210,10 +212,21 @@ static bool pt_tt_register_pass()
210212
return true;
211213
}
212214

215+
/* opcache.file_cache persists what the optimizer produced: the stripped
216+
* op_arrays would outlive the armed run, and a later run — a --debug one,
217+
* or one without the extension at all — would execute them unchecked */
218+
static bool pt_tt_file_cache_configured()
219+
{
220+
bool exists = false;
221+
const char *fileCache = zend_ini_string_ex("opcache.file_cache", sizeof("opcache.file_cache") - 1, 0, &exists);
222+
return exists && fileCache != NULL && fileCache[0] != '\0';
223+
}
224+
213225
bool pt_trusted_types_set_prefix(zend_string *prefix)
214226
{
215227
PT_G(trusted_types_prefix_len) = 0;
216228
if (ZSTR_LEN(prefix) == 0 || ZSTR_LEN(prefix) >= sizeof(PT_G(trusted_types_prefix))) return false;
229+
if (pt_tt_file_cache_configured()) return false;
217230
if (!pt_tt_register_pass()) return false;
218231
memcpy(PT_G(trusted_types_prefix), ZSTR_VAL(prefix), ZSTR_LEN(prefix));
219232
PT_G(trusted_types_prefix_len) = ZSTR_LEN(prefix);

‎turbo-ext/tests/trusted-types.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ function check(string $label, array $expected, array $actual): bool
128128
require $trustedPrefix . 'functions.php';
129129
$ok = check('trusted', TRUSTED_EXPECTED, runCases('TrustedTypesFixture\Trusted')) && $ok;
130130

131+
// opcache.file_cache persists the optimized scripts: stripped op_arrays would
132+
// outlive this run and be executed by later unarmed runs (--debug, or no
133+
// extension at all), so arming refuses when a file cache is configured. The
134+
// setting is INI_SYSTEM, hence a child process.
135+
$fileCacheDir = sys_get_temp_dir() . '/phpstan-trusted-types-file-cache-' . getmypid();
136+
@mkdir($fileCacheDir);
137+
$extension = getenv('TURBO_DLL') ?: dirname(__DIR__) . '/phpstan_turbo.so';
138+
$armedWithFileCache = exec(sprintf(
139+
'%s -d extension=%s -d opcache.enable_cli=1 -d opcache.file_cache=%s -r %s',
140+
escapeshellarg(PHP_BINARY),
141+
escapeshellarg($extension),
142+
escapeshellarg($fileCacheDir),
143+
escapeshellarg('var_export(PHPStanTurbo\\Runtime::trustTypesUnder(' . var_export($trustedPrefix, true) . '));'),
144+
));
145+
if ($armedWithFileCache !== 'false') {
146+
fwrite(STDERR, sprintf("FAIL: trustTypesUnder() must refuse with opcache.file_cache set, got %s\n", var_export($armedWithFileCache, true)));
147+
$ok = false;
148+
}
149+
$removeTree = static function (string $path) use (&$removeTree): void {
150+
if (is_dir($path) && !is_link($path)) {
151+
foreach (scandir($path) as $entry) {
152+
if ($entry !== '.' && $entry !== '..') {
153+
$removeTree($path . '/' . $entry);
154+
}
155+
}
156+
@rmdir($path);
157+
return;
158+
}
159+
@unlink($path);
160+
};
161+
$removeTree($fileCacheDir);
162+
131163
if (!$ok) {
132164
exit(1);
133165
}

0 commit comments

Comments
 (0)