|
| 1 | +// Copyright 2026 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package jobparser |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + |
| 10 | + "gitea.dev/actionslib/pkg/model" |
| 11 | + |
| 12 | + "go.yaml.in/yaml/v4" |
| 13 | +) |
| 14 | + |
| 15 | +// maxExpandedNodes bounds how many nodes alias expansion may create. go-yaml's own alias guard does |
| 16 | +// not cover us: it only counts while decoding into values, and a workflow is kept as raw yaml.Nodes. |
| 17 | +const maxExpandedNodes = 50000 |
| 18 | + |
| 19 | +var errTooManyYamlNodes = errors.New("maximum YAML nodes exceeded") |
| 20 | + |
| 21 | +// ReadWorkflow decodes a workflow file with its aliases expanded. Callers inspect the workflow's |
| 22 | +// raw nodes by kind, and an alias is a kind none of them expect. |
| 23 | +func ReadWorkflow(content []byte) (*model.Workflow, error) { |
| 24 | + doc, err := resolveYamlAliases(content) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + return readWorkflowDoc(doc) |
| 29 | +} |
| 30 | + |
| 31 | +func readWorkflowDoc(doc *yaml.Node) (*model.Workflow, error) { |
| 32 | + if doc.Kind == 0 { |
| 33 | + return nil, io.EOF // what a yaml decoder reports for an empty file |
| 34 | + } |
| 35 | + w := new(model.Workflow) |
| 36 | + return w, doc.Decode(w) |
| 37 | +} |
| 38 | + |
| 39 | +// decodeResolved is yaml.Unmarshal with aliases expanded first. |
| 40 | +func decodeResolved(content []byte, out any) error { |
| 41 | + doc, err := resolveYamlAliases(content) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + return decodeYamlDoc(doc, out) |
| 46 | +} |
| 47 | + |
| 48 | +func decodeYamlDoc(doc *yaml.Node, out any) error { |
| 49 | + if doc.Kind == 0 { |
| 50 | + return nil // an empty document, as yaml.Unmarshal treats it |
| 51 | + } |
| 52 | + return doc.Decode(out) |
| 53 | +} |
| 54 | + |
| 55 | +// resolveYamlAliases parses content and replaces every alias with a copy of the node its anchor names. |
| 56 | +func resolveYamlAliases(content []byte) (*yaml.Node, error) { |
| 57 | + doc := &yaml.Node{} |
| 58 | + if err := yaml.Unmarshal(content, doc); err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + budget := maxExpandedNodes |
| 62 | + return doc, expandAliases(doc, &budget) |
| 63 | +} |
| 64 | + |
| 65 | +// expandAliases replaces node's alias descendants in place. |
| 66 | +func expandAliases(node *yaml.Node, budget *int) error { |
| 67 | + node.Anchor = "" // a name for a node, not part of the workflow: keep it out of the payloads |
| 68 | + if err := rejectMergeKeys(node); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + for i, child := range node.Content { |
| 72 | + if child.Kind != yaml.AliasNode { |
| 73 | + if err := expandAliases(child, budget); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + continue |
| 77 | + } |
| 78 | + copied, err := copyExpanded(child.Alias, budget) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + node.Content[i] = copied |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// copyExpanded deep copies a node expandAliases already expanded and validated, since an anchor is |
| 88 | +// declared before the alias naming it. An anchor aliased from inside itself is the exception, and |
| 89 | +// recurses here until it exhausts budget. |
| 90 | +func copyExpanded(node *yaml.Node, budget *int) (*yaml.Node, error) { |
| 91 | + if *budget--; *budget < 0 { |
| 92 | + return nil, errTooManyYamlNodes |
| 93 | + } |
| 94 | + if node.Kind == yaml.AliasNode { |
| 95 | + return copyExpanded(node.Alias, budget) |
| 96 | + } |
| 97 | + |
| 98 | + copied := *node |
| 99 | + copied.Content = make([]*yaml.Node, len(node.Content)) |
| 100 | + for i, child := range node.Content { |
| 101 | + child, err := copyExpanded(child, budget) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + copied.Content[i] = child |
| 106 | + } |
| 107 | + return &copied, nil |
| 108 | +} |
| 109 | + |
| 110 | +// rejectMergeKeys refuses `<<: *anchor`, same as GitHub does |
| 111 | +func rejectMergeKeys(node *yaml.Node) error { |
| 112 | + if node.Kind != yaml.MappingNode { |
| 113 | + return nil |
| 114 | + } |
| 115 | + for i := 0; i < len(node.Content)-1; i += 2 { |
| 116 | + if node.Content[i].Tag == "!!merge" { |
| 117 | + return errors.New("merge keys (`<<`) are not supported, alias the whole value instead") |
| 118 | + } |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
0 commit comments