Update the Trojan Source scanner for Unicode confusables/homoglyphs.
Review Request #11908 — Created Jan. 6, 2022 and submitted
The Trojan Source scanner now looks for certain Unicode characters that
appear as standard latin1 characters, like A-Z, a-z, 0-9, etc. These can
be used by a malicious developer to try to sneak in logic that appears
to define or make use of a function, class, variable, etc. with one
name, while actually using a completely different name.This is CVE-2021-42694.
This sort of scanning must be done carefully. There are a lot of
perfectly valid Unicode characters out there, and we don't want to check
them all, assume they're all nefarious.What we instead do is check only confusables that meet the following
criteria:
- Has a codepoint >= 128 (avoiding issues with, say, "1" vs" "l").
- Can be confused with a COMMON or LATIN Unicode character (ones most
likely to be legitimately used in function names or other code)- Is not itself a COMMON or LATIN Unicode character.
To generate the mapping, we have a new
./contrib/internal/build-confusables.py
file, which will pull down the
latest datasets from unicode.org and generate a resulting
reviewboard/codesafety/_unicode_confusables.py
file.This is not perfect. People may find that some comments or strings
trigger a warning. Ideally, we'd be able to selectively apply these
tests depending on where it appears, but we're not in a position to do
that yet. Still, most of these should probably not be hit often in
practice.Possible areas of future expansion would be to allow these if beside
other characters from the same script that are not themselves
confusables. This could be attempted if we get feedback later stating
that too many false-positives are being generated.There is one major caveat to this implementation: it largely requires
wide Unicode character support, so that surrogate pairs appear as one
character/codepoint and not multiple.This is always the case on Python 3. For Python 2, it depends on how
CPython was compiled. If wide support is not enabled, certain characters
cannot be found.
Unit tests pass on Python 2 (without wide support) and Python 3.
Tested with all the test code sets provided on
https://github.com/nickboucher/trojan-source/
Summary | ID |
---|---|
84faa8cbdfda9a72b60281245b7c9cf0c53c4bb0 |
- Change Summary:
-
Updated for Review Board 5:
- Removed Python 2-specific code, include the
WIDE_UNICODE
constant. - Removed
six
usage. - Removed
__future__
imports. - Changed
unicode
tostr
in docstrings. - Changed
SafeText
toSafeString
. - Updated versions in docstrings.
- Updated
build-confusables.py
to do a better job building the output path and printing a result.
- Removed Python 2-specific code, include the
- Commits:
-
Summary ID 21bc54a697a84aa01c2817a5631766e3a9436f2a 702c66b09851e56ab4ac3f78ad9950de53556243 - Branch:
-
release-4.0.xrelease-5.0.x
- Diff:
-
Revision 2 (+4142 -20)
Checks run (2 succeeded)
- Change Summary:
-
Removed
__future__
imports. - Commits:
-
Summary ID 702c66b09851e56ab4ac3f78ad9950de53556243 84faa8cbdfda9a72b60281245b7c9cf0c53c4bb0 - Diff:
-
Revision 3 (+4126 -20)