Skip to content

Commit 28d172f

Browse files
Fixed PubnubsubsciptionKey detector verification (#4107)
* fixed verification issue * resolved comment
1 parent b06f6d7 commit 28d172f

1 file changed

Lines changed: 42 additions & 11 deletions

File tree

pkg/detectors/pubnubsubscriptionkey/pubnubsubscriptionkey.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package pubnubsubscriptionkey
22

33
import (
44
"context"
5+
"fmt"
6+
"io"
57
"net/http"
68
"strings"
79

@@ -45,17 +47,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
4547
}
4648

4749
if verify {
48-
req, err := http.NewRequestWithContext(ctx, "GET", "https://ps.pndsn.com/v2/objects/"+resMatch+"/uuids", nil)
49-
if err != nil {
50-
continue
51-
}
52-
res, err := client.Do(req)
53-
if err == nil {
54-
defer res.Body.Close()
55-
if res.StatusCode >= 200 && res.StatusCode < 300 {
56-
s1.Verified = true
57-
}
58-
}
50+
isVerified, verificationErr := verifyKey(ctx, client, resMatch)
51+
s1.Verified = isVerified
52+
s1.SetVerificationError(verificationErr)
5953
}
6054

6155
results = append(results, s1)
@@ -71,3 +65,40 @@ func (s Scanner) Type() detectorspb.DetectorType {
7165
func (s Scanner) Description() string {
7266
return "PubNub is a real-time communication platform. A PubNub Subscription Key allows access to the PubNub API for subscribing to channels and receiving messages."
7367
}
68+
69+
func verifyKey(ctx context.Context, client *http.Client, key string) (bool, error) {
70+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://ps.pndsn.com/v2/objects/"+key+"/uuids", nil)
71+
if err != nil {
72+
return false, err
73+
}
74+
75+
resp, err := client.Do(req)
76+
if err != nil {
77+
return false, err
78+
}
79+
80+
defer func() {
81+
_, _ = io.Copy(io.Discard, resp.Body)
82+
_ = resp.Body.Close()
83+
}()
84+
85+
switch resp.StatusCode {
86+
case http.StatusOK:
87+
return true, nil
88+
case http.StatusForbidden:
89+
bodyBytes, err := io.ReadAll(resp.Body)
90+
if err != nil {
91+
return false, err
92+
}
93+
94+
if strings.Contains(string(bodyBytes), "Objects not enabled for this subscriber key.") {
95+
return true, nil
96+
}
97+
98+
return false, nil
99+
case http.StatusUnauthorized:
100+
return false, nil
101+
default:
102+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
103+
}
104+
}

0 commit comments

Comments
 (0)