Skip to content

[BUG] SerializerExtension.load_yaml raises AttributeError instead of TemplateRuntimeError on malformed YAML with the libyaml loader #69533

Description

@ggiesen

Description

salt.utils.jinja.SerializerExtension.load_yaml crashes with AttributeError: 'NoneType' object has no attribute 'splitlines' instead of raising the intended TemplateRuntimeError when YAML parsing fails and PyYAML's libyaml (C) loader is in use.

The C loader populates the exception's problem_mark but leaves problem_mark.buffer as None. load_yaml only guards against problem_mark being absent (except AttributeError), so with the C loader it takes the else branch and passes buffer=None into salt.utils.stringutils.get_context(), which then calls None.splitlines().

This affects the load_yaml filter and the {% load_yaml %} / {% import_yaml %} tags whenever the YAML is invalid and libyaml is installed (the default in most packages).

Setup

  • onedir or classic packaging; requires PyYAML built with libyaml (CSafeLoader), which is the default.

Steps to Reproduce the behavior

Render a jinja template that loads malformed YAML:

{% load_yaml as data %}{foo, bar: baz}{% endload %}{{ data }}

Minimal reproduction:

from jinja2 import Environment
from salt.utils.jinja import SerializerExtension
env = Environment(extensions=[SerializerExtension])
env.from_string("{% load_yaml as d %}{a, b: c}{% endload %}{{ d }}").render()

With the libyaml loader active this raises AttributeError: 'NoneType' object has no attribute 'splitlines' from salt/utils/stringutils.py instead of a TemplateRuntimeError.

Expected behavior

load_yaml raises jinja2.exceptions.TemplateRuntimeError ("Encountered error loading yaml: ...") regardless of which YAML loader is active.

Versions Report

Reproduced on 3006.26; the same error-handling code is present on current master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions