pytest/source_char: check for mixed direction text
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 17 Nov 2021 20:17:53 +0000 (20:17 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 3 Dec 2021 18:53:43 +0000 (18:53 +0000)
commitdab828f63c0a6bf0bb96920fd36383f6cbe43179
treefea5ec65dc1d59a852b37d691c386b039a40966d
parent0f7e58b0e29778711d3385adbba957c175c3bdef
pytest/source_char: check for mixed direction text

As pointed out in https://lwn.net/Articles/875964, forbidding bidi
marker characters is not always going to be enough to avoid
right-to-left vs left-to-right confusion. Consider this:

$ python -c's = "b = x  # 2 * n * m"; print(s); print(s.replace("x", "א").replace("n", "ח"))'

b = x  # 2 * n * m
b = א  # 2 * ח * m

Those two lines are semantically the same, with the Hebrew letters
"א" and "ח" replacing "x" and "n". But they look like they mean
different things.

It is not enough to say we only allow these scripts (or indeed
non-ascii) in strings and comments, as demonstrated in this example:

$ python -c's = "b = \"x#\"  #  n"; print(s); print(s.replace("x", "א").replace("n", "ח"))'

b = "x#"  #  n
b = "א#"  #  ח

where the second line is visually disordered but looks valid. Any series
of neutral characters between teo RTL characters will be reversed (and
possibly mirrored).

In practice this affects one file, which is a text file for testing
unicode normalisation.

I think, for the reasons shown above, we are unlikely to see legitimate
RTL code outside perhaps of documentation files — but if we do, we can
add those files to the allow-list.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Fri Dec  3 18:53:43 UTC 2021 on sn-devel-184
python/samba/tests/source_chars.py
testdata/source-chars-bidi.py [new file with mode: 0644]