* Update Cloudflare detectors for 2026+ prefixed credential formats
## Context/Background
Cloudflare is rolling out new prefixed credential formats in 2026.
The new formats (`cfk_`, `cfut_`, `cfat_`) are self-identifying via
prefix and do not need keyword proximity matching. Per project
convention, new token formats are added via the `Versioner` interface
with a `v1`/`v2` directory split.
Additionally, CA keys (Service Keys) are now deprecated.
## Changes in this commit
- cloudflareglobalapikey: split into `v1`/`v2` with `Versioner`
interface. v1 fixes legacy regex to `[a-f0-9]{37,45}` (lowercase
hex). v2 adds `cfk_` prefixed format detection.
- cloudflareapitoken: split into `v1`/`v2` with `Versioner`
interface. v2 adds `cfut_`/`cfat_` prefixed format detection.
`cfat_` (account tokens) route verification through the
account-scoped `/accounts/:id/tokens/verify` endpoint, extracting
account IDs from surrounding data.
- cloudflarecakey: add deprecation notice with changelog link.
- defaults.go: updated imports and registrations for versioned
scanners.
* Address PR feedback on Cloudflare detectors
## Context/Background
Addressing reviewer feedback from @MuneebUllahKhan222 and Cursor
Bugbot on PR #4830.
## Changes in this commit
- Verification functions now return `(bool, error)` with proper
indeterminate/determinate handling. Callsites use
`SetVerificationError`. Status checks use exact `http.StatusOK`
instead of range. Response bodies properly drained via
`io.Copy(io.Discard, ...)`.
- Extracted shared verification functions into `common.go` at each
detector package root (following the AWS detector pattern). v1 and
v2 both import from the parent package instead of v2 importing v1.
- Added integration tests for both v2 detectors with coverage for
`cfk_`, `cfut_`, and `cfat_` (including account-scoped
verification).
- `cfat_` tokens now pair with account IDs in `RawV2` regardless of
the `verify` flag, consistent with how `cfk_` pairs with emails.
* Resolved Comments
---------
Co-authored-by: Nicholas Comer <[email protected]>
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package cloudflarecakey
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
regexp "github.com/wasilibs/go-re2"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
|
|
)
|
|
|
|
type Scanner struct{}
|
|
|
|
// Ensure the Scanner satisfies the interface at compile time.
|
|
var _ detectors.Detector = (*Scanner)(nil)
|
|
|
|
var (
|
|
client = common.SaneHttpClient()
|
|
|
|
// Origin CA keys (aka "Service Keys") are deprecated as of 2026-03-19:
|
|
// https://developers.cloudflare.com/changelog/post/2026-03-19-service-key-authentication-deprecated/
|
|
//
|
|
// Reference: https://developers.cloudflare.com/fundamentals/api/get-started/ca-keys/
|
|
keyPat = regexp.MustCompile(`\b(v1\.0-[A-Za-z0-9-]{171})\b`)
|
|
)
|
|
|
|
// Keywords are used for efficiently pre-filtering chunks.
|
|
// Use identifiers in the secret preferably, or the provider name.
|
|
func (s Scanner) Keywords() []string {
|
|
return []string{"cloudflare"}
|
|
}
|
|
|
|
// FromData will find and optionally verify CloudflareCaKey secrets in a given set of bytes.
|
|
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
|
|
dataStr := string(data)
|
|
|
|
uniqueMatches := make(map[string]struct{})
|
|
|
|
for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {
|
|
uniqueMatches[matches[1]] = struct{}{}
|
|
}
|
|
|
|
for caKey := range uniqueMatches {
|
|
s1 := detectors.Result{
|
|
DetectorType: detector_typepb.DetectorType_CloudflareCaKey,
|
|
Raw: []byte(caKey),
|
|
SecretParts: map[string]string{"key": caKey},
|
|
}
|
|
|
|
if verify {
|
|
isVerified, verificationErr := verifyCloudFlareCAKey(ctx, client, caKey)
|
|
s1.Verified = isVerified
|
|
s1.SetVerificationError(verificationErr, caKey)
|
|
}
|
|
|
|
results = append(results, s1)
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (s Scanner) Type() detector_typepb.DetectorType {
|
|
return detector_typepb.DetectorType_CloudflareCaKey
|
|
}
|
|
|
|
func (s Scanner) Description() string {
|
|
return "Cloudflare is a web infrastructure and website security company. Cloudflare CA keys can be used to manage SSL/TLS certificates and other security settings."
|
|
}
|
|
|
|
func verifyCloudFlareCAKey(ctx context.Context, client *http.Client, caKey string) (bool, error) {
|
|
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.cloudflare.com/client/v4/certificates?zone_id=a", nil)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("user-agent", "curl/7.68.0") // pretend to be from curl so we do not wait 100+ seconds -> nice try did not work
|
|
|
|
req.Header.Add("X-Auth-User-Service-Key", caKey)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
defer func() {
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
_ = resp.Body.Close()
|
|
}()
|
|
|
|
switch resp.StatusCode {
|
|
case http.StatusOK:
|
|
return true, nil
|
|
case http.StatusUnauthorized, http.StatusForbidden:
|
|
return false, nil
|
|
default:
|
|
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
}
|
|
}
|