-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcspgen.py
47 lines (43 loc) · 1.41 KB
/
cspgen.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
from cspgen import parser
from cspgen import gen
from cspgen import crawler
import sys
import logging
import argparse
def main(args):
# print(args.conf)
if args.url:
profile = {}
url = args.url
page = crawler.get_page(url)
profile["js_sources"], profile["inline"] = crawler.get_js_sources(page)
pol = gen.policy_from_crawl(profile)
if args.output:
gen.write_toml(args.output, pol)
else:
print(pol)
if args.conf:
# print(parser.use_parser(args.conf))
try:
conf = parser.use_parser(args.conf)
except Exception as e:
logging.error("Unable to open/read file %s: %s", fname, str(e))
return
for k in list(conf.keys()):
cat = parser.read_policy(conf, k)
if type(cat) is list:
policy = cat
else:
policy = gen.gen_resource_policy(cat)
gen.add_policy(k, policy)
gen.print_policy()
return
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description="Content Security Policy generator")
argparser.add_argument(
"-c", "--conf", nargs="?", type=str, help="TOML configuration file"
)
argparser.add_argument("-u", "--url", help="URL to crawl")
argparser.add_argument("-o", "--output", help="File to write CSP")
args = argparser.parse_args()
main(args)