Repository navigation
fix: keep obfuscated copy row-aligned with the source - #9617
Merged
Merged
Conversation
…ned copy The obfuscator computed its edits and the certificate ranges from tokenize rows, then applied them to rows from str.splitlines. splitlines also breaks on a form feed and seven other characters that tokenize treats as ordinary text. One form feed before a private block moved every edit up a row: the private class was printed in the clear and a public comment was rewritten (finding A3, form feed). A new astutil.source_rows() splits on \n only, like tokenize. The obfuscator's edit and hide steps and the marker parser now use it. run() now compares the row counts of the obfuscated copy and the source. On a mismatch it writes no copy and issues no certificate. It reports a new row-count-mismatch violation (raised as PolicyViolation under strict). This also refuses the docstring-shift form of A3, where a multi-line string in an obfuscate block collapses to one row, until the obfuscator keeps those rows. Tests: a form feed inside a public comment before an obfuscate block, and before an obfuscate-then-hide block, keeps the row count. Both fail on the old code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The obfuscator replaced a string token with one "■", however many rows it spanned. A five-line docstring in an obfuscate block collapsed to one row. The hide step then used source row numbers on the shorter text: it blanked a public line and printed the hidden lines in the clear (finding A3, docstring shift). Since the previous commit, run() refused such a file. A string that spans rows is now replaced by a triple-quoted "■" with the same number of newlines. Multi-line f-string text keeps its newlines too. The copy keeps the source's row count, so run() accepts the file again and the hide step blanks the right rows. Tests: a three-line docstring in an obfuscate block, with and without a hide block after it, keeps the row count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A different row count in the display copy is an error in the obfuscator. It is not a problem in the private code. Therefore run() now raises ObfuscationError for it, also with strict=False. The row-count-mismatch violation code no longer exists. The check is in _require_same_row_count(). A new test makes the copy lose a row and expects the error. The row-count tests of the obfuscator also check that hidden rows show the hide marker and that public rows do not change.
pjwerneck
approved these changes
Nov 13, 2026
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 finding A3: the obfuscated copy could end up with a different number of rows than the source. The hide step uses row numbers from the source, so the wrong rows got hidden. Hidden code was printed in the clear and public lines were blanked or rewritten.
Changes
astutil.source_rows(), which splits on\nonly, the same waytokenizecounts rows.str.splitlinesalso splits on form feeds and other separators. The obfuscator and the marker parser now usesource_rows().■with the same number of newlines). Multi-line f-string text also keeps its newlines.run()now checks that the obfuscated copy has the same number of rows as the source. If not, it writes no copy, issues no certificate, and reports a newrow-count-mismatchviolation (raised asPolicyViolationin strict mode).row-count-mismatchtodocs/blacklist.mdand to theViolationCodelist.Testing
just test-unit-restrict: 237 passed.Asana task