Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / tools / lint / trojan-source / __init__.py
bloba20c10203d16c538f38539ae86c2614d9bd369de
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 import sys
5 import unicodedata
7 from mozlint import result
8 from mozlint.pathutils import expand_exclusions
10 # Code inspired by Red Hat
11 # https://github.com/siddhesh/find-unicode-control/
12 # published under the 'BSD 3-Clause' license
13 # https://access.redhat.com/security/vulnerabilities/RHSB-2021-007
15 results = []
17 disallowed = set(
18 chr(c) for c in range(sys.maxunicode) if unicodedata.category(chr(c)) == "Cf"
22 def getfiletext(config, filename):
23 # Make a text string from a file, attempting to decode from latin1 if necessary.
24 # Other non-utf-8 locales are not supported at the moment.
25 with open(filename, "rb") as infile:
26 try:
27 return infile.read().decode("utf-8")
28 except Exception as e:
29 res = {
30 "path": filename,
31 "message": "Could not open file as utf-8 - maybe an encoding error: %s"
32 % e,
33 "level": "error",
35 results.append(result.from_config(config, **res))
36 return None
38 return None
41 def analyze_text(filename, text, disallowed):
42 line = 0
43 for t in text.splitlines():
44 line = line + 1
45 subset = [c for c in t if chr(ord(c)) in disallowed]
46 if subset:
47 return (subset, line)
49 return ("", 0)
52 def lint(paths, config, **lintargs):
53 files = list(expand_exclusions(paths, config, lintargs["root"]))
54 for f in files:
55 text = getfiletext(config, f)
56 if text:
57 (subset, line) = analyze_text(f, text, disallowed)
58 if subset:
59 res = {
60 "path": f,
61 "lineno": line,
62 "message": "disallowed characters: %s" % subset,
63 "level": "error",
65 results.append(result.from_config(config, **res))
67 return {"results": results, "fixed": 0}