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: false and merge: true. With { json: true }, js-yaml keeps the last of duplicate keys and resolves << merge keys. The backend's Psych does the same. Without these options, yaml throws 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 yaml default. I ruled out the 1.1 schema for parsing. It reads on, yes, y and n as booleans, keys included, where js-yaml reads strings.

Writing:

  • version: '1.1'. The backend reads policies with Psych, which implements YAML 1.1. With the 1.2 default, yaml writes on, yes, no, 2026-01-01 and 10:30 without quotes. Psych then reads booleans, a Date (which safe_load refuses) and a number. With 1.1, yaml quotes them, as js-yaml did.
  • singleQuote: true. This keeps the quote style of js-yaml, so that the YAML users see in the editor stays the same.
  • aliasDuplicateObjects: false. This is the yaml form of noRefs: true. Two call sites did not pass noRefs: getInitialPolicy and the scan execution toYaml. They now also write a shared object out in full. getInitialPolicy builds a new object from a parsed policy and literals, so it has no shared objects. toYaml is only called from its spec.

Behaviour changes

  • Package URLs. js-yaml wrote 'pkg:npm/foo@1.0.0', and yaml writes pkg:npm/foo@1.0.0. Psych reads the same string.
  • Long strings. js-yaml folded them with >-, and yaml writes a plain multi-line scalar. Psych reads the same string.
  • undefined values. js-yaml threw, so policyToYaml reported the error to Sentry and returned an empty string. yaml leaves the key out. No code in the editor sets undefined on purpose.
  • Unquoted timestamps and times that a user types. For until: 2026-01-01T00:00:00Z or start_time: 10:30, js-yaml returned a Date or a number. yaml returns the string. The backend refuses a Date, and the schema requires strings for snooze.until and start_time, so the string is the value that a valid policy holds.
  • Unknown tags, such as name: !foo test. js-yaml threw, so the editor read the policy as empty, as it does for any YAML it cannot parse. yaml keeps the value and writes a YAMLWarning to the browser console. The backend's YAML.safe_load also keeps the value, so the editor now agrees with the backend.
  • Tab indentation. js-yaml accepted a tab before a key or a list item. yaml refuses 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-yaml converted its input with String(). yaml throws, and fromYaml catches the error and returns {}. All app callers pass the editor text or the GraphQL yaml field. 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.js pins the options: duplicate keys, merge keys, the quotes that a YAML 1.1 reader needs, no aliases, and undefined values.

  • The unit specs in ee/spec/frontend/security_orchestration and the integration specs in ee/spec/frontend/integration/security_policies pass 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:

    1. Opens Secure > Policies > New policy > Merge request approval policy.
    2. Types yes as the name and a description longer than one line.
    3. Selects a security scan rule.
    4. Clicks Configure with a merge request.
    5. Reads .gitlab/security-policies/policy.yml from the new MR's branch with Gitlab::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)
before_light after_light

References

Edited by Miguel Rincon

Merge request reports

Loading
Loading