Move the security policy editor from js-yaml to yaml
What does this MR do and why?
This MR moves the security policy editor from js-yaml 3 to yaml 2. It is the first step in moving the frontend to one YAML library.
The editor now parses and writes policy YAML through two new helpers, parsePolicyYaml and stringifyPolicyYaml. They keep the options in one place.
Options, and what I ruled out
Parsing:
uniqueKeys: falseandmerge: true. With{ json: true },js-yamlkeeps the last of duplicate keys and resolves<<merge keys. The backend's Psych does the same. Without these options,yamlthrows on a duplicate key, and the editor would show an error for a policy that the backend accepts.- The YAML 1.2 core schema, which is the
yamldefault. I ruled out the 1.1 schema for parsing. It readson,yes,yandnas booleans, keys included, wherejs-yamlreads strings.
Writing:
version: '1.1'. The backend reads policies with Psych, which implements YAML 1.1. With the 1.2 default,yamlwriteson,yes,no,2026-01-01and10:30without quotes. Psych then reads booleans, aDate(whichsafe_loadrefuses) and a number. With 1.1,yamlquotes them, asjs-yamldid.singleQuote: true. This keeps the quote style ofjs-yaml, so that the YAML users see in the editor stays the same.aliasDuplicateObjects: false. This is theyamlform ofnoRefs: true. Two call sites did not passnoRefs:getInitialPolicyand the scan executiontoYaml. They now also write a shared object out in full.getInitialPolicybuilds a new object from a parsed policy and literals, so it has no shared objects.toYamlis only called from its spec.
Behaviour changes
- Package URLs.
js-yamlwrote'pkg:npm/foo@1.0.0', andyamlwritespkg:npm/foo@1.0.0. Psych reads the same string. - Long strings.
js-yamlfolded them with>-, andyamlwrites a plain multi-line scalar. Psych reads the same string. undefinedvalues.js-yamlthrew, sopolicyToYamlreported the error to Sentry and returned an empty string.yamlleaves the key out. No code in the editor setsundefinedon purpose.- Unquoted timestamps and times that a user types. For
until: 2026-01-01T00:00:00Zorstart_time: 10:30,js-yamlreturned aDateor a number.yamlreturns the string. The backend refuses aDate, and the schema requires strings forsnooze.untilandstart_time, so the string is the value that a valid policy holds. - Unknown tags, such as
name: !foo test.js-yamlthrew, so the editor read the policy as empty, as it does for any YAML it cannot parse.yamlkeeps the value and writes aYAMLWarningto the browser console. The backend'sYAML.safe_loadalso keeps the value, so the editor now agrees with the backend. - Tab indentation.
js-yamlaccepted a tab before a key or a list item.yamlrefuses it, and so does Psych. The editor now treats such a policy as YAML it cannot parse, instead of accepting a policy that the backend refuses. - Input that is not a string.
js-yamlconverted its input withString().yamlthrows, andfromYamlcatches the error and returns{}. All app callers pass the editor text or the GraphQLyamlfield. One spec passed the manifest in an array, and this MR fixes that spec.
Parse errors still reach the same handling. Both libraries throw, and every call site uses a bare catch. No code reads YAMLException or .mark.
scan_execution/lib/to_yaml.js and scan_result/lib/to_yaml.js are only called from their specs. This MR moves them to yaml and does not remove them, to keep the MR to the library change.
How I verified it
-
I ran each YAML string and policy object from the policy spec mocks and the policy constants through both libraries:
- All 122 YAML strings parse to the same result.
- Of 259 dumped objects, 244 give identical text.
- I read both dumps with Psych, with the options of
Gitlab::Config::Loader::Yaml. In all 256 cases where neither library throws, Psych reads the same values. - Of the other 3 objects, 2 hold functions, and both libraries throw. The third holds
undefined, as described above.
-
policy_yaml_spec.jspins the options: duplicate keys, merge keys, the quotes that a YAML 1.1 reader needs, no aliases, andundefinedvalues. -
The unit specs in
ee/spec/frontend/security_orchestrationand the integration specs inee/spec/frontend/integration/security_policiespass locally. -
I ran a temporary feature spec in GDK, once at the merge-base and once at the MR head. The spec was not committed. It does these steps:
- Opens Secure > Policies > New policy > Merge request approval policy.
- Types
yesas the name and a description longer than one line. - Selects a security scan rule.
- Clicks Configure with a merge request.
- Reads
.gitlab/security-policies/policy.ymlfrom the new MR's branch withGitlab::Config::Loader::Yaml.
In both runs, the backend read the name as the string
"yes"and the description unchanged. The committed file was identical in both runs, because the backend writes its own dump of the policy.
Screenshots
The YAML preview at the top right is the only change. The name yes stays quoted. The long description is a plain multi-line string instead of a >- folded one.
Before (806747c7) |
After (f6997878) |
|---|---|
![]() |
![]() |
References
- Part of #638642 (closed)
- The migration came out of the review of !267423 (merged)

