Skip to content

Commit 69bed78

Browse files
committed
refactor(test): factor synthetic GitHub PAT helper into pkg/internal/portcullistest
Both pkg/hooks/builtins/redact_secrets_test.go and pkg/sandbox/kit/ kit_test.go grew an identical CRC32-suffix computation to produce a synthetic, portcullis-detectable GitHub PAT without hard-coding the full 40-char ghp_ literal (which would trip GitHub secret-scanning push protection). Move that computation to a single helper — portcullistest.FakeGitHubPAT — parameterised by a 30-char body, and have both tests call it.
1 parent 65d8f29 commit 69bed78

4 files changed

Lines changed: 67 additions & 29 deletions

File tree

pkg/hooks/builtins/redact_secrets_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package builtins
22

33
import (
4-
"hash/crc32"
54
"testing"
65

76
"github.com/docker/portcullis"
@@ -10,23 +9,12 @@ import (
109

1110
"github.com/docker/docker-agent/pkg/chat"
1211
"github.com/docker/docker-agent/pkg/hooks"
12+
"github.com/docker/docker-agent/pkg/internal/portcullistest"
1313
"github.com/docker/docker-agent/pkg/tools"
1414
)
1515

16-
// fakeGitHubPAT returns a synthetic GitHub PAT with a body that
17-
// passes portcullis' CRC32 checksum validation. The full token is
18-
// never written as a source literal so GitHub's secret-scanning push
19-
// protection doesn't flag this file.
2016
func fakeGitHubPAT() string {
21-
const body = "cxLeRrvbJfmYdUtr70xnNE3Q7Gvli4"
22-
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
23-
var suffix [6]byte
24-
checksum := uint64(crc32.ChecksumIEEE([]byte(body)))
25-
for i := len(suffix) - 1; i >= 0; i-- {
26-
suffix[i] = alphabet[checksum%62]
27-
checksum /= 62
28-
}
29-
return "ghp_" + body + string(suffix[:])
17+
return portcullistest.FakeGitHubPAT("cxLeRrvbJfmYdUtr70xnNE3Q7Gvli4")
3018
}
3119

3220
// TestRedactSecretsScrubsTopLevelStringValue: a recognised secret in
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Package portcullistest provides test helpers for code that
2+
// exercises portcullis' GitHub-token detection rules.
3+
package portcullistest
4+
5+
import "hash/crc32"
6+
7+
// FakeGitHubPAT returns a synthetic GitHub PAT whose trailing 6-char
8+
// base62 CRC32 suffix passes portcullis' validGitHubChecksum, so the
9+
// returned token is detected (and redacted) by the secret scanner.
10+
//
11+
// The token is assembled at runtime: the full 40-char `ghp_…` string
12+
// never appears as a source literal, so GitHub's secret-scanning
13+
// push protection won't flag files that call this helper.
14+
//
15+
// body must be exactly 30 base62 characters; callers vary it to get
16+
// distinct fake tokens. The result has the GitHub-PAT shape
17+
// `ghp_` + 36 base62 chars and is never a real credential.
18+
func FakeGitHubPAT(body string) string {
19+
const (
20+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
21+
suffixLen = 6
22+
)
23+
if len(body) != 30 {
24+
panic("portcullistest: body must be 30 chars")
25+
}
26+
var suffix [suffixLen]byte
27+
checksum := uint64(crc32.ChecksumIEEE([]byte(body)))
28+
for i := suffixLen - 1; i >= 0; i-- {
29+
suffix[i] = alphabet[checksum%62]
30+
checksum /= 62
31+
}
32+
return "ghp_" + body + string(suffix[:])
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package portcullistest_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/docker/portcullis"
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/docker/docker-agent/pkg/internal/portcullistest"
11+
)
12+
13+
func TestFakeGitHubPAT_DetectedByPortcullis(t *testing.T) {
14+
t.Parallel()
15+
16+
tok := portcullistest.FakeGitHubPAT("cxLeRrvbJfmYdUtr70xnNE3Q7Gvli4")
17+
18+
assert.True(t, strings.HasPrefix(tok, "ghp_"))
19+
assert.Len(t, tok, 40)
20+
assert.True(t, portcullis.Contains(tok), "synthetic PAT must trigger portcullis detection")
21+
assert.Equal(t, portcullis.Marker, portcullis.Redact(tok))
22+
}
23+
24+
func TestFakeGitHubPAT_RejectsWrongLength(t *testing.T) {
25+
t.Parallel()
26+
27+
assert.Panics(t, func() { portcullistest.FakeGitHubPAT("too-short") })
28+
}

pkg/sandbox/kit/kit_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kit
22

33
import (
44
"encoding/json"
5-
"hash/crc32"
65
"os"
76
"path/filepath"
87
"sort"
@@ -15,25 +14,15 @@ import (
1514
"github.com/stretchr/testify/require"
1615

1716
latestcfg "github.com/docker/docker-agent/pkg/config/latest"
17+
"github.com/docker/docker-agent/pkg/internal/portcullistest"
1818
"github.com/docker/docker-agent/pkg/promptfiles"
1919
"github.com/docker/docker-agent/pkg/skills"
2020
)
2121

22-
// fakeGitHubToken returns a synthetic GitHub PAT that triggers
23-
// portcullis. The trailing 6 chars are computed at runtime so the
24-
// full token never appears as a source literal — otherwise GitHub's
25-
// secret-scanning push protection would flag this file. Used only as
26-
// input to the redactor, never as an actual credential.
22+
// fakeGitHubToken is used only as input to the redactor, never as an
23+
// actual credential.
2724
func fakeGitHubToken() string {
28-
const body = "1234567890abcdefghijklmnopqrst"
29-
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
30-
var suffix [6]byte
31-
checksum := uint64(crc32.ChecksumIEEE([]byte(body)))
32-
for i := len(suffix) - 1; i >= 0; i-- {
33-
suffix[i] = alphabet[checksum%62]
34-
checksum /= 62
35-
}
36-
return "ghp_" + body + string(suffix[:])
25+
return portcullistest.FakeGitHubPAT("1234567890abcdefghijklmnopqrst")
3726
}
3827

3928
// isolateEnv prevents a developer-exported DOCKER_AGENT_KIT_DIR from

0 commit comments

Comments
 (0)