updated storyblok detector (#3981)

* updated storyblok detector

* resolved abdul basit comments
This commit is contained in:
Kashif Khan
2025-03-19 12:51:26 +05:00
committed by GitHub
parent ded5f45b92
commit 475af30650
8 changed files with 1191 additions and 847 deletions
+48 -25
View File
@@ -2,9 +2,11 @@ package storyblok
import (
"context"
regexp "github.com/wasilibs/go-re2"
"fmt"
"io"
"net/http"
"strings"
regexp "github.com/wasilibs/go-re2"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
@@ -20,7 +22,7 @@ var (
client = common.SaneHttpClient()
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"storyblok"}) + `\b([0-9A-Za-z]{22}t{2})\b`)
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"storyblok"}) + `\b([0-9A-Za-z]{22}tt)\b`)
)
// Keywords are used for efficiently pre-filtering chunks.
@@ -29,33 +31,35 @@ func (s Scanner) Keywords() []string {
return []string{"storyblok"}
}
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_StoryblokAccessToken
}
func (s Scanner) Description() string {
return "Storyblok is a headless CMS that allows developers to build flexible and powerful content management solutions. Storyblok tokens can be used to access and modify content within a Storyblok space."
}
// FromData will find and optionally verify Storyblok 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)
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
var uniqueAccessTokens = make(map[string]struct{})
for _, match := range matches {
resMatch := strings.TrimSpace(match[1])
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
uniqueAccessTokens[match[1]] = struct{}{}
}
for accessToken := range uniqueAccessTokens {
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_Storyblok,
Raw: []byte(resMatch),
DetectorType: detectorspb.DetectorType_StoryblokAccessToken,
Raw: []byte(accessToken),
}
if verify {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.storyblok.com/v1/cdn/spaces/me/?token="+resMatch, nil)
if err != nil {
continue
}
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
}
isVerified, verificationErr := verifyStoryBlokAccessToken(ctx, client, accessToken)
s1.Verified = isVerified
s1.SetVerificationError(verificationErr)
}
results = append(results, s1)
@@ -64,10 +68,29 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return results, nil
}
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Storyblok
}
// docs: https://www.storyblok.com/docs/api/content-delivery/v2/getting-started/authentication
func verifyStoryBlokAccessToken(ctx context.Context, client *http.Client, token string) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.storyblok.com/v1/cdn/spaces/me/?token="+token, nil)
if err != nil {
return false, err
}
func (s Scanner) Description() string {
return "Storyblok is a headless CMS that allows developers to build flexible and powerful content management solutions. Storyblok tokens can be used to access and modify content within a Storyblok space."
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:
return false, nil
default:
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}
@@ -48,7 +48,7 @@ func TestStoryblok_FromChunk(t *testing.T) {
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Storyblok,
DetectorType: detectorspb.DetectorType_StoryblokAccessToken,
Verified: true,
},
},
@@ -64,7 +64,7 @@ func TestStoryblok_FromChunk(t *testing.T) {
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Storyblok,
DetectorType: detectorspb.DetectorType_StoryblokAccessToken,
Verified: false,
},
},
@@ -0,0 +1,100 @@
package storyblokpersonalaccesstoken
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/detectorspb"
)
type Scanner struct{}
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)
var (
client = common.SaneHttpClient()
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"storyblok"}) + `\b([0-9A-Za-z]{22}tt-[0-9]{6}-[A-Za-z0-9_-]{20})\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{"storyblok"}
}
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_StoryblokPersonalAccessToken
}
func (s Scanner) Description() string {
return `Storyblok is a headless CMS that allows developers to build flexible and powerful content management solutions.
Storyblok personal access tokens can be used with management APIs.
The Storyblok Management API allows you to create, edit, update, and delete content using a common interface`
}
// FromData will find and optionally verify Storyblok 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)
var uniquePATs = make(map[string]struct{})
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
uniquePATs[match[1]] = struct{}{}
}
for pat := range uniquePATs {
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_StoryblokPersonalAccessToken,
Raw: []byte(pat),
}
if verify {
isVerified, verificationErr := verifyStoryBlokPersonalAccessToken(ctx, client, pat)
s1.Verified = isVerified
s1.SetVerificationError(verificationErr)
}
results = append(results, s1)
}
return results, nil
}
// docs: http://storyblok.com/docs/api/management/core-resources/spaces/retrieve-multiple-spaces
func verifyStoryBlokPersonalAccessToken(ctx context.Context, client *http.Client, token string) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://mapi.storyblok.com/v1/spaces/", nil)
if err != nil {
return false, err
}
// docs: https://www.storyblok.com/docs/api/management/getting-started/authentication
req.Header.Set("Authorization", token)
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:
return false, nil
default:
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}
@@ -0,0 +1,121 @@
//go:build detectors
// +build detectors
package storyblokpersonalaccesstoken
import (
"context"
"fmt"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
func TestStoryblokPersonalAccessToken_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
secret := testSecrets.MustGetField("STORYBLOK_PAT")
inactiveSecret := testSecrets.MustGetField("STORYBLOK_PAT_INACTIVE")
type args struct {
ctx context.Context
data []byte
verify bool
}
tests := []struct {
name string
s Scanner
args args
want []detectors.Result
wantErr bool
}{
{
name: "found, verified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a storyblok secret %s within", secret)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_StoryblokPersonalAccessToken,
Verified: true,
},
},
wantErr: false,
},
{
name: "found, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a storyblok secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_StoryblokPersonalAccessToken,
Verified: false,
},
},
wantErr: false,
},
{
name: "not found",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte("You cannot find the secret within"),
verify: true,
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Scanner{}
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Storyblok.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i := range got {
if len(got[i].Raw) == 0 {
t.Fatalf("no raw secret present: \n %+v", got[i])
}
got[i].Raw = nil
}
if diff := pretty.Compare(got, tt.want); diff != "" {
t.Errorf("Storyblok.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})
}
}
func BenchmarkFromData(benchmark *testing.B) {
ctx := context.Background()
s := Scanner{}
for name, data := range detectors.MustGetBenchmarkData() {
benchmark.Run(name, func(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := s.FromData(ctx, false, data)
if err != nil {
b.Fatal(err)
}
}
})
}
}
@@ -0,0 +1,91 @@
package storyblokpersonalaccesstoken
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
var (
validPattern = "5r7EgNfakeXi6ZoEls1twAtt-001100-Q5E2fKfakeRqsUjwmsJn"
invalidPattern = "5r7EgNfakeXi6ZoEls1twAt-001100-Q5E2fKfakeRqsUjwmsJn"
keyword = "storyblok"
)
func TestStoryblokPersonalAccessToken_Pattern(t *testing.T) {
d := Scanner{}
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
tests := []struct {
name string
input string
want []string
}{
{
name: "valid pattern - with keyword storyblok",
input: fmt.Sprintf("%s token = '%s'", keyword, validPattern),
want: []string{validPattern},
},
{
name: "valid pattern - ignore duplicate",
input: fmt.Sprintf("%s token = '%s' | '%s'", keyword, validPattern, validPattern),
want: []string{validPattern},
},
{
name: "valid pattern - key out of prefix range",
input: fmt.Sprintf("%s keyword is not close to the real key in the data\n = '%s'", keyword, validPattern),
want: []string{},
},
{
name: "invalid pattern",
input: fmt.Sprintf("%s = '%s'", keyword, invalidPattern),
want: []string{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
if len(matchedDetectors) == 0 {
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
return
}
results, err := d.FromData(context.Background(), false, []byte(test.input))
if err != nil {
t.Errorf("error = %v", err)
return
}
if len(results) != len(test.want) {
if len(results) == 0 {
t.Errorf("did not receive result")
} else {
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
}
return
}
actual := make(map[string]struct{}, len(results))
for _, r := range results {
if len(r.RawV2) > 0 {
actual[string(r.RawV2)] = struct{}{}
} else {
actual[string(r.Raw)] = struct{}{}
}
}
expected := make(map[string]struct{}, len(test.want))
for _, v := range test.want {
expected[v] = struct{}{}
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
}
})
}
}
+2
View File
@@ -686,6 +686,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stormboard"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stormglass"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/storyblok"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/storyblokpersonalaccesstoken"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/storychief"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/strava"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/streak"
@@ -1536,6 +1537,7 @@ func buildDetectorList() []detectors.Detector {
&stormboard.Scanner{},
&stormglass.Scanner{},
&storyblok.Scanner{},
&storyblokpersonalaccesstoken.Scanner{},
&storychief.Scanner{},
&strava.Scanner{},
&streak.Scanner{},
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -232,7 +232,7 @@ enum DetectorType {
Duda = 219;
Yext = 220;
ContentStack = 221;
Storyblok = 222;
StoryblokAccessToken = 222;
GraphCMS = 223;
Checkmarket = 224;
Convertkit = 225;
@@ -1025,6 +1025,7 @@ enum DetectorType {
AzureRefreshToken = 1013;
AirtableOAuth = 1014;
AirtablePersonalAccessToken = 1015;
StoryblokPersonalAccessToken = 1016;
}
message Result {