Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/plugins/resources/yaml/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ func (y *Yaml) Condition(_ context.Context, source string, scm scm.ScmHandler) (
if derr == io.EOF {
break
}
// The decoder does not advance past a syntax error, so it would
// return the same error on every subsequent call. Stop reading
// this file instead of looping on it.
errorMessages = append(errorMessages, fmt.Errorf(
"%q - parsing yaml file: %w", originalFilePath, err))
continue
"%q - parsing yaml file: %w", originalFilePath, derr))
break
}
docs = append(docs, &doc)
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/plugins/resources/yaml/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -807,3 +808,53 @@ name: github
})
}
}

// Test_Condition_yamlpathUndecodableFile guards against a decode loop that never
// terminates. yaml.v3 does not advance past a syntax error, so a file whose first
// document parses and whose remainder does not (a Markdown file dropped in a
// workflows directory, say) makes the decoder return the same non-EOF error
// forever. Retrying instead of stopping grew errorMessages until the process was
// OOM-killed.
func Test_Condition_yamlpathUndecodableFile(t *testing.T) {
spec := Spec{
File: "README.md",
Key: "$.name",
Engine: "yamlpath",
}

y, err := New(spec)
require.NoError(t, err)

y.contentRetriever = &text.MockTextRetriever{
Contents: map[string]string{
"README.md": `# OpenAPI Breaking Changes Detection

## Overview

This workflow detects breaking changes in the OpenAPI specification.

## How It Works

1. **Automatic Detection**: On every PR, the workflow:
- compares the spec against the base branch
`,
},
}
y.files = map[string]file{
"README.md": {originalFilePath: "README.md", filePath: "README.md"},
}

done := make(chan error, 1)
go func() {
_, _, condErr := y.Condition(context.Background(), "", nil)
done <- condErr
}()

select {
case condErr := <-done:
require.Error(t, condErr)
assert.Contains(t, condErr.Error(), "parsing yaml file")
case <-time.After(10 * time.Second):
t.Fatal("Condition did not return: the decoder loop is spinning on a non-EOF error")
}
}
Loading