mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Finished Iframe Implementation
This commit is contained in:
30
filter_plugins/csp_hashes.py
Normal file
30
filter_plugins/csp_hashes.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from ansible.errors import AnsibleFilterError
|
||||
import copy
|
||||
|
||||
def append_csp_hash(applications, application_id, code_one_liner):
|
||||
"""
|
||||
Ensures that applications[application_id].csp.hashes['script-src-elem']
|
||||
exists and appends the given one-liner (if not already present).
|
||||
"""
|
||||
if not isinstance(applications, dict):
|
||||
raise AnsibleFilterError("`applications` must be a dict")
|
||||
if application_id not in applications:
|
||||
raise AnsibleFilterError(f"Unknown application_id: {application_id}")
|
||||
|
||||
apps = copy.deepcopy(applications)
|
||||
app = apps[application_id]
|
||||
csp = app.setdefault('csp', {})
|
||||
hashes = csp.setdefault('hashes', {})
|
||||
|
||||
existing = hashes.get('script-src-elem', [])
|
||||
if code_one_liner not in existing:
|
||||
existing.append(code_one_liner)
|
||||
hashes['script-src-elem'] = existing
|
||||
|
||||
return apps
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'append_csp_hash': append_csp_hash
|
||||
}
|
44
filter_plugins/text_filters.py
Normal file
44
filter_plugins/text_filters.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# filter_plugins/text_filters.py
|
||||
|
||||
from ansible.errors import AnsibleFilterError
|
||||
import re
|
||||
|
||||
def to_one_liner(s):
|
||||
"""
|
||||
Collapse any multi-line string into a single line,
|
||||
trim extra whitespace, and remove JavaScript comments.
|
||||
Supports removal of both '//' line comments and '/*...*/' block comments,
|
||||
but preserves '//' inside string literals and templating expressions.
|
||||
"""
|
||||
if not isinstance(s, str):
|
||||
raise AnsibleFilterError("to_one_liner() expects a string")
|
||||
|
||||
# 1) Remove block comments /* ... */
|
||||
no_block_comments = re.sub(r'/\*.*?\*/', '', s, flags=re.DOTALL)
|
||||
|
||||
# 2) Extract string literals to protect them from comment removal
|
||||
string_pattern = re.compile(r"'(?:\\.|[^'\\])*'|\"(?:\\.|[^\"\\])*\"")
|
||||
literals = []
|
||||
def _extract(match):
|
||||
idx = len(literals)
|
||||
literals.append(match.group(0))
|
||||
return f"__STR{idx}__"
|
||||
temp = string_pattern.sub(_extract, no_block_comments)
|
||||
|
||||
# 3) Remove line comments // ...
|
||||
temp = re.sub(r'//.*$', '', temp, flags=re.MULTILINE)
|
||||
|
||||
# 4) Restore string literals
|
||||
for idx, lit in enumerate(literals):
|
||||
temp = temp.replace(f"__STR{idx}__", lit)
|
||||
|
||||
# 5) Collapse all whitespace
|
||||
one_liner = re.sub(r'\s+', ' ', temp).strip()
|
||||
|
||||
return one_liner
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'to_one_liner': to_one_liner,
|
||||
}
|
Reference in New Issue
Block a user