fix: handle custom yaml tag '!unsafe'

This commit is contained in:
Robert Kaussow 2021-01-11 12:08:55 +01:00
parent b6fb8cab47
commit 777415731d
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61
2 changed files with 20 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from .yamlhelper import action_tasks
from .yamlhelper import normalize_task from .yamlhelper import normalize_task
from .yamlhelper import normalized_yaml from .yamlhelper import normalized_yaml
from .yamlhelper import parse_yaml_linenumbers from .yamlhelper import parse_yaml_linenumbers
from .yamlhelper import UnsafeTag
def get_tasks(candidate, settings): def get_tasks(candidate, settings):
@ -160,6 +161,9 @@ def get_raw_yaml(candidate, settings):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
yaml.add_constructor(
UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader
)
content = yaml.safe_load(f) content = yaml.safe_load(f)
except yaml.YAMLError as e: except yaml.YAMLError as e:
errors.append( errors.append(
@ -176,6 +180,9 @@ def run_yamllint(candidate, options="extends: default"):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
yaml.add_constructor(
UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader
)
yaml.safe_load(f) yaml.safe_load(f)
for problem in linter.run(f, YamlLintConfig(options)): for problem in linter.run(f, YamlLintConfig(options)):

View File

@ -576,3 +576,16 @@ def normalized_yaml(file, options):
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
raise LaterError("syntax error", e) raise LaterError("syntax error", e)
return lines return lines
class UnsafeTag:
"""Handle custom yaml unsafe tag."""
yaml_tag = u"!unsafe"
def __init__(self, value):
self.unsafe = value
@staticmethod
def yaml_constructor(loader, node):
return loader.construct_scalar(node)