YAML Parser Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓YAML Parser runs in O(n) time and O(n) space, in Java as in every other language.
- ✓Nothing in the JDK. SnakeYAML is the standard library in practice, and Jackson offers a YAML data format module that reuses the ObjectMapper machinery.
- ✓Construct the parser with a SafeConstructor, or use SnakeYAML 2.0 or later where that is the default, and bind to a declared type rather than loading into a generic map.
- ✓The Java trap: SnakeYAML is the clearest case of the whole tag problem. Its bare constructor allowed a document to name any class on the classpath and have it instantiated, which is CVE-2022-1471 and was exploitable for remote code execution wherever a service parsed user-supplied YAML.
- ✓SnakeYAML builds an event stream and then a node graph before constructing objects, so it is heavier than a direct binding.
What YAML Parser actually is
YAML Parser in Java: what you actually reach for
Java implementation
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;
public final class Config {
public static class AppConfig {
public int port;
public String host;
}
/** Untrusted input: SafeConstructor builds ONLY plain types. */
public static Object loadUntrusted(String yaml) {
var options = new LoaderOptions();
// Also bound the expansion so anchors cannot be used as a bomb.
options.setMaxAliasesForCollections(50);
return new Yaml(new SafeConstructor(options)).load(yaml);
}
/** Trusted input, bound to ONE declared type, not "any class". */
public static AppConfig loadTyped(String yaml) {
var options = new LoaderOptions();
return new Yaml(new Constructor(AppConfig.class, options)).load(yaml);
}
// THE VULNERABILITY, for context: the bare "new Yaml().load(untrusted)"
// let the DOCUMENT name the class to instantiate, which is CVE-2022-1471
// and was exploitable for remote code execution. SnakeYAML 2.0 made the
// safe constructor the default, but the vulnerable versions still
// compile and still run, so the dependency version IS the control.
// Check the version in your build file, not the shape of the call.
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for YAML Parser?
Nothing in the JDK. SnakeYAML is the standard library in practice, and Jackson offers a YAML data format module that reuses the ObjectMapper machinery.
What is the most common Java mistake when implementing YAML Parser?
SnakeYAML is the clearest case of the whole tag problem. Its bare constructor allowed a document to name any class on the classpath and have it instantiated, which is CVE-2022-1471 and was exploitable for remote code execution wherever a service parsed user-supplied YAML. SnakeYAML 2.0 changed the default to a safe constructor, which is the fix, but it is a breaking change that some applications pinned away from precisely because it broke their intentional use of typed tags. So the version you are on determines whether you are safe, and the vulnerable version still compiles and still runs. Check the dependency, not the code.
How does YAML Parser actually perform in Java?
SnakeYAML builds an event stream and then a node graph before constructing objects, so it is heavier than a direct binding. For large documents Jackson streaming over the YAML factory avoids the intermediate graph. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Student discounts on developer tools are collected on Noizz, tool by tool.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.