forked from mlcommons/dynabench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_insecure_cors_configuration.py
48 lines (37 loc) · 1.42 KB
/
detect_insecure_cors_configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import argparse
from typing import List, NamedTuple, Optional, Sequence
class BadFile(NamedTuple):
filename: str
bad_line_number: int
def check_for_endwsith(
filenames: Sequence[str],
) -> List[BadFile]:
"""Check if files contain bad CORS configurations."""
bad_files = []
for filename in filenames:
if "cors" in filename and (filename != "detect_insecure_cors_configuration.py"):
with open(filename) as content:
text_lines = content.readlines()
for i, txt_line in enumerate(text_lines):
if "endswith" in txt_line:
bad_files.append(BadFile(filename, i + 1))
return bad_files
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="+", help="Filenames to run")
args = parser.parse_args(argv)
bad_filenames = check_for_endwsith(args.filenames)
if bad_filenames:
for bad_file in bad_filenames:
print(
f"`endswith` found in {bad_file.filename} on line number: "
"{bad_file.bad_line_number}"
)
return 1
else:
return 0
if __name__ == "__main__":
raise SystemExit(main())