Fix token scrambling to preserve type hints and magic constants#4
Merged
archisgore merged 4 commits intomainfrom Jan 25, 2026
Merged
Fix token scrambling to preserve type hints and magic constants#4archisgore merged 4 commits intomainfrom
archisgore merged 4 commits intomainfrom
Conversation
This commit fixes critical issues in the PHP token scrambler that were causing syntax errors when executing scrambled WordPress code. ## Changes Made ### 1. Token-Aware Transformer (`token-aware-transformer.php`) - Added protection for PHP language keyword tokens (T_FUNCTION, T_WHILE, T_IF, etc.) - Added protection for PHP magic constant tokens (T_DIR, T_FILE, T_LINE, T_CLASS_C, etc.) - Removed non-existent token constants (T_DIE, T_EXIT, T_AND_EQUAL) that don't exist in PHP 8.5 - Changed fallback behavior to NOT transform unknown token types ### 2. Dictionary Handler (`dictionaryHandler.go`) - Added `MagicConstants` map to blacklist PHP magic constants from scrambling - Modified `AddToEEWords()` to skip magic constants (__DIR__, __FILE__, __LINE__, etc.) - These constants must never be scrambled as they are compile-time values ### 3. Scrambler (`scrambler.go`) - Modified `substituteWordsInString()` to skip magic constants during token processing - Prevents magic constants from being added to the scrambling dictionary ## Problem Statement The previous implementation scrambled ALL tokens found in PHP source files, including: 1. PHP language keywords (function, while, if, for, etc.) - caused parse errors 2. PHP magic constants (__DIR__, __FILE__, etc.) - caused runtime errors ## Solution The fix operates at two levels: **Level 1: Dictionary Generation (Go scrambler)** - Magic constants are excluded from the dictionary during PHP recompilation - This ensures the PHP interpreter itself understands these constants **Level 2: Application Code Transformation (PHP transformer)** - Keywords and magic constants are preserved when transforming WordPress files - Only user-defined tokens (functions, variables, classes) are scrambled ## Testing Tested with WordPress 6.9 scrambling: - ✅ WordPress files transform without parse errors - ✅ PHP keywords preserved (function, while, if, etc.) - ✅ Magic constants preserved (__DIR__, __FILE__, etc.) - ✅ PHP recompiles successfully with scrambled dictionary Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit fixes critical issues in the token scrambling system: 1. **Token-aware transformer: Fix parameter list detection** - Previously failed to detect function parameter lists correctly - Was checking previous token expecting 'function', but found function name instead - Now walks back up to 5 tokens to find 'function' or 'fn' keyword - Correctly preserves type hints (string, int, bool, etc.) in parameters 2. **Dictionary handler: Exclude magic constants from scrambling** - Added MagicConstants map to blacklist PHP magic constants - Prevents __DIR__, __FILE__, __LINE__, etc. from being scrambled - Modified AddToEEWords() to skip magic constants - Ensures PHP interpreter can still understand these compile-time constants 3. **Scrambler: Skip magic constants in source transformation** - Added check in substituteWordsInString() to skip magic constants - Prevents scrambling when processing PHP lexer/parser files - Checks both lowercase and uppercase versions These fixes ensure WordPress and other PHP applications can be properly scrambled while maintaining type safety and PHP's built-in functionality. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…rm-php.py - Update Dockerfile.test-symbols to copy files from encrypted-execution/src/ paths - Install PHP CLI instead of Python - Update test-symbol-scrambling-docker.sh to use token-aware-transformer.php Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Use cat instead of python3 -m json.tool to display JSON dictionary, since Python is no longer installed in the test image. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes critical bugs in the token scrambling system that were causing PHP parse errors in WordPress and other applications.
Problems Fixed
1. Type Hints Being Scrambled
Problem: Function parameter type hints like
string,int,boolwere being scrambled, causing parse errors.Root Cause: The transformer checked the previous token when it saw
(, expecting to findfunction, but the previous token was the function NAME, not the keyword.Example error:
Fix: Walk backwards up to 5 tokens to find the
functionorfnkeyword, correctly detecting parameter lists.2. Magic Constants Being Scrambled
Problem: PHP magic constants (
__DIR__,__FILE__,__LINE__, etc.) were being added to the dictionary and scrambled in the PHP interpreter itself.Root Cause: The scrambler treated magic constants as regular keywords and scrambled them in both application code AND the PHP interpreter source.
Fix:
MagicConstantsmap to blacklist these constantsAddToEEWords()to skip magic constantssubstituteWordsInString()to skip magic constants when processing lexer/parser filesChanges
encrypted-execution/src/transformer/token-aware-transformer.phpencrypted-execution/src/scrambler/dictionaryHandler.goMagicConstantsmap with all PHP magic constantsAddToEEWords()to exclude magic constants from dictionaryencrypted-execution/src/scrambler/scrambler.gosubstituteWordsInString()encrypted-execution/src/scrambler/go.modTesting
Tested with WordPress 6.9:
function→ scrambled version)string $textstays asstring)__DIR__not scrambled)Impact
This fixes the fundamental token scrambling mechanism, enabling:
🤖 Generated with Claude Code