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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

# make codeguard-ci: CI intentionally runs the stable Marketplace action instead of the in-repo binary.
- uses: devr-tools/codeguard@v1.2.0
- uses: devr-tools/codeguard@a1f8eb3aed6b6b645d42be2a8279b3f578328c40
with:
config: .codeguard/codeguard.yaml

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ jobs:
# the SLSA generator verifies its own provenance against the tagged ref, and
# a SHA pin breaks that check. This is the one intentional exception to the
# SHA-pinning policy.
uses: slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@v2.1.0
uses: slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@f7dd8c54c2067bafc12ca7a55595d5ee9b75204a
with:
base64-subjects: ${{ needs.build-release.outputs.hashes }}
upload-assets: true
Expand Down
33 changes: 30 additions & 3 deletions internal/codeguard/checks/quality/quality_ai_resolution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package quality

import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
Expand All @@ -11,6 +13,10 @@ import (
"time"
)

const maxGitHeadMessageBytes = 1 << 20

var errGitHeadMessageTooLarge = errors.New("git HEAD message exceeds size limit")

type packageManifest struct {
Name string `json:"name"`
Dependencies map[string]string `json:"dependencies"`
Expand Down Expand Up @@ -59,11 +65,32 @@ func readGitHeadMessage(dir string) string {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "-C", dir, "log", "-1", "--format=%B") //nolint:gosec // fixed git subcommand; dir is a config-supplied scan target path
out, err := cmd.Output()
if err != nil {
out := limitedBuffer{limit: maxGitHeadMessageBytes}
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return ""
}
return string(out)
return out.String()
}

// limitedBuffer prevents os/exec from buffering attacker-controlled command
// output without bound. Returning an error also stops the stdout copy once the
// limit is reached.
type limitedBuffer struct {
bytes.Buffer
limit int
}

func (b *limitedBuffer) Write(p []byte) (int, error) {
remaining := b.limit - b.Len()
if remaining <= 0 {
return 0, errGitHeadMessageTooLarge
}
if len(p) > remaining {
n, _ := b.Buffer.Write(p[:remaining])
return n, errGitHeadMessageTooLarge
}
return b.Buffer.Write(p)
}

func envFlagEnabled(keys []string) bool {
Expand Down
42 changes: 42 additions & 0 deletions internal/codeguard/checks/quality/quality_ai_resolution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package quality

import (
"errors"
"testing"
)

func TestLimitedBufferRejectsOutputPastLimit(t *testing.T) {
t.Parallel()

var output limitedBuffer
output.limit = 4

n, err := output.Write([]byte("abcdef"))
if n != output.limit {
t.Fatalf("Write() wrote %d bytes, want %d", n, output.limit)
}
if !errors.Is(err, errGitHeadMessageTooLarge) {
t.Fatalf("Write() error = %v, want %v", err, errGitHeadMessageTooLarge)
}
if got := output.String(); got != "abcd" {
t.Fatalf("buffer contents = %q, want %q", got, "abcd")
}

n, err = output.Write([]byte("more"))
if n != 0 || !errors.Is(err, errGitHeadMessageTooLarge) {
t.Fatalf("second Write() = (%d, %v), want (0, %v)", n, err, errGitHeadMessageTooLarge)
}
}

func TestLimitedBufferAcceptsOutputWithinLimit(t *testing.T) {
t.Parallel()

output := limitedBuffer{limit: 4}
n, err := output.Write([]byte("abcd"))
if err != nil || n != 4 {
t.Fatalf("Write() = (%d, %v), want (4, nil)", n, err)
}
if got := output.String(); got != "abcd" {
t.Fatalf("buffer contents = %q, want %q", got, "abcd")
}
}
Loading