GitHub Actions accepts invalid YAML
GitHub Actions accepts invalid YAML
All TILs Homepage Blog 2026-11-22 github github-actions TL;DR: GitHub Actions accepts several invalid YAML productions, making it difficult (read: impossible) to parse some real-world action and workflow definitions with a properly conforming YAML parser. This is related to zizmor#1116, which has been bugging me for a year and a half.
TL;DR:GitHub Actions 接受几种无效的 YAML 结构,这使得使用符合规范的 YAML 解析器来解析某些实际的 Action 和工作流定义变得困难(甚至是不可能的)。这与 zizmor#1116 有关,这个问题已经困扰我一年半了。
Last April, a user reported that zizmor was rejecting a workflow containing a step that looks like this: - name: Create NPM Remote Repository JSON uses: jsdaniell/create-json@v1.2.3 with: name: “npm-remote-template.json” json: ’{ “key”: “npm-remote”, “rclass”: “remote”, “packageType”: “npm”, “url”: “https://registry.npmjs.org” }’ Apart from the use of a multi-line flow-style string for the json: key, nothing particularly stands out about this step. Except: it isn’t valid YAML!
去年四月,一位用户报告称 zizmor 拒绝了一个包含如下步骤的工作流:- name: Create NPM Remote Repository JSON uses: jsdaniell/create-json@v1.2.3 with: name: “npm-remote-template.json” json: ’{ “key”: “npm-remote”, “rclass”: “remote”, “packageType”: “npm”, “url”: “https://registry.npmjs.org” }’ 除了 json: 键使用了多行流式字符串外,这个步骤看起来没什么特别之处。但问题是:它不是有效的 YAML!
YAML says this about flow-style nodes that are nested inside of a block-style parent collection (read: map or array): Flow nodes must be indented by at least one more space than the parent block collection. Note that flow nodes may begin on a following line. Source: YAML 1.2.2 § 8.2.3 That means the correct syntax is instead: - name: Create NPM Remote Repository JSON uses: jsdaniell/create-json@v1.2.3 with: name: “npm-remote-template.json” json: ’{ “key”: “npm-remote”, “rclass”: “remote”, “packageType”: “npm”, “url”: “https://registry.npmjs.org” }’ The difference is barely visible: - }’ + }’
YAML 关于嵌套在块样式父集合(即映射或数组)中的流样式节点有如下规定:流节点必须比父块集合至少多缩进一个空格。注意,流节点可以从下一行开始。来源:YAML 1.2.2 § 8.2.3。这意味着正确的语法应该是:- name: Create NPM Remote Repository JSON uses: jsdaniell/create-json@v1.2.3 with: name: “npm-remote-template.json” json: ’{ “key”: “npm-remote”, “rclass”: “remote”, “packageType”: “npm”, “url”: “https://registry.npmjs.org” }’ 两者的区别微乎其微:- }’ + }’
YAML is a big and complicated spec, so it didn’t even occur to me that this was invalid. Instead, I opened tree-sitter-yaml#36 to “fix” it. I’ve now opened tree-sitter-yaml#51 to revert that misbegotten fix.
YAML 是一个庞大且复杂的规范,所以我根本没想过这是无效的。相反,我提交了 tree-sitter-yaml#36 来“修复”它。现在,我已经提交了 tree-sitter-yaml#51 来撤销那个错误的修复。
On GitHub’s side, I’m not sure if they have a clean path towards spec conformance here. Much of the code behind Actions is proprietary, but it seems likely that the underlying parser on their end is YamlDotNet. Actions appears to use YamlDotNet 5.3.0, which was released in 2018. However, even upgrading to the newest version (18.1.0 as of writing) wouldn’t fix this particular bug, since it’s still present on that version (note the !isSingleQuoted guard).
在 GitHub 方面,我不确定他们是否有明确的路径来实现规范一致性。Actions 背后的许多代码都是专有的,但他们底层的解析器很可能是 YamlDotNet。Actions 似乎使用的是 2018 年发布的 YamlDotNet 5.3.0。然而,即使升级到最新版本(撰写本文时为 18.1.0)也无法修复这个特定的错误,因为它在该版本中仍然存在(注意 !isSingleQuoted 防御代码)。
My best guess is that a 2020 patch (YamlDotNet#467) only fixed the double-quoted form because YAML’s official test suite only tests rule 8.2.3 with a double-quoted string, despite it applying to both forms.
我的猜测是,2020 年的一个补丁(YamlDotNet#467)仅修复了双引号形式,因为 YAML 的官方测试套件仅使用双引号字符串来测试规则 8.2.3,尽管该规则同时适用于两种形式。