Defending AI Agents
עברית Join waitlist
Menu

On-ramp · Lesson 03

YAML is JSON with indentation and four traps

A team lists its allowed countries in a config file: - GB, - DE, - NO. The service starts, and Norwegian users are denied. The YAML loader read NO as the boolean false. The same file, loaded by the same code, is a different policy from the one its author read.

The reading skill

YAML is a superset of JSON (since version 1.2) written with indentation instead of braces (YAML, 2021). You will meet it in every configuration file of the course: the lab, policies, CI. Three forms cover nearly everything:

name: acme-gateway          # a mapping: key, colon, space, value
allow:                      # a list is dashes at one indentation
  - crm.acme.example
  - cal.acme.example
limits: {rate: 10, burst: 20}   # JSON-style on one line is legal

Indentation is structure and uses spaces, never tabs. A # starts a comment. Two spaces of indentation fewer is a different file.

Trap 1: bare words become types

Run it, as the course's tools do, with the pure-data loader:

import yaml
print(yaml.safe_load("country: no\nversion: 1.10\nports: [80, 0o17, 010]\nmode: 0755\non: yes"))
# {'country': False, 'version': 1.1, 'ports': [80, '0o17', 8], 'mode': 493, True: True}

no is False, 1.10 is the float 1.1 (a version number that has lost its zero), 010 is octal 8, 0755 is the permission bits 493, and the key on is the boolean True. These are YAML 1.1's implicit types (the YAML 1.1 boolean type lists y, yes, on and their negatives; Boolean Language-Independent Type for YAML version 1.1, 2005). Version 1.2 trims the list, but PyYAML, which most Python tooling uses, still resolves the 1.1 words (though not the single letters), as the run above shows. Quote strings that must stay strings: "NO", "1.10".

Trap 2: duplicate keys, again

yaml.safe_load("a: 1\na: 2")      # {'a': 2}

The later one silently wins, and a copy-pasted block can shadow the key above it. The same differential as lesson 2, in a file a person edits.

Trap 3: load can run code

Some loaders build arbitrary objects from tags such as !!python/object/apply:os.system. PyYAML's documentation says yaml.load "is as powerful as pickle.load and so may call any Python function" (PyYAML documentation); safe_load refuses such a tag with a ConstructorError. Use the safe loader for every file you did not write, and for those you did, because someone else may be able to edit them.

Trap 4: anchors and merge keys

&base names a block and *base or <<: *base reuses it. A policy file that merges a base block into twenty rules reads shorter and is audited wrongly: a reader sees a rule, not its inherited fields. Expand before reviewing (load it and print the result).

Expert: why a control reads data, not text

A gateway config that is reloaded on change is a control plane. Loading it with a strict, typed schema (a field country is a string, anything else fails to load) turns trap 1 into a startup error, not a silent policy change. Fail the load, not the request.

Check yourself

  1. Which of these load as a string: id: 0x1F, id: "0x1F", id: 1_000, id: ~? Run it.
  2. A reviewer reads a rule saying allow: [crm]. Which two YAML features can make the effective rule wider than what they read?
  3. Why is "we only load files from our own repo" not an answer to trap 3? And which of the four traps does switching to safe_load leave open?
Answers
  1. Only id: "0x1F". PyYAML's safe_load reads 0x1F as the integer 31, 1_000 as 1000 (YAML 1.1 allows underscores in integers) and ~ as None.
  2. Duplicate keys (trap 2): a second allow: further down the same mapping silently replaces the one they read. Anchors and merge keys (trap 4): a <<: *base in the rule brings in fields they never see on the page. Loading the file and printing the result shows the rule that actually applies.
  3. Because "our own repo" is a set of writers, not one: any contributor, compromised account, CI step or tool with write access can plant a !!python/object/apply tag, and load will run it. safe_load closes trap 3 only; implicit types, duplicate keys and merge keys all behave the same under it, as the lesson's own runs show.