4
4
"context"
5
5
"fmt"
6
6
"net/http"
7
- "strings"
8
7
9
8
regexp "github.com/wasilibs/go-re2"
10
9
@@ -18,6 +17,7 @@ type Scanner struct {
18
17
}
19
18
20
19
const accuweatherURL = "https://dataservice.accuweather.com"
20
+ const requiredShannonEntropy = 4
21
21
22
22
var (
23
23
// Ensure the Scanner satisfies the interface at compile time.
@@ -46,21 +46,26 @@ func (s Scanner) getClient() *http.Client {
46
46
func (s Scanner ) FromData (ctx context.Context , verify bool , data []byte ) (results []detectors.Result , err error ) {
47
47
dataStr := string (data )
48
48
49
- matches := keyPat .FindAllStringSubmatch (dataStr , - 1 )
50
-
51
- for _ , match := range matches {
52
- resMatch := strings .TrimSpace (match [1 ])
49
+ matches := make (map [string ]struct {})
50
+ for _ , match := range keyPat .FindAllStringSubmatch (dataStr , - 1 ) {
51
+ k := match [1 ]
52
+ if detectors .StringShannonEntropy (k ) < requiredShannonEntropy {
53
+ continue
54
+ }
55
+ matches [k ] = struct {}{}
56
+ }
53
57
58
+ for key := range matches {
54
59
s1 := detectors.Result {
55
60
DetectorType : detectorspb .DetectorType_Accuweather ,
56
- Raw : []byte (resMatch ),
61
+ Raw : []byte (key ),
57
62
}
58
63
59
64
if verify {
60
65
client := s .getClient ()
61
- isVerified , verificationErr := verifyAccuweather (ctx , client , resMatch )
66
+ isVerified , verificationErr := verifyAccuweather (ctx , client , key )
62
67
s1 .Verified = isVerified
63
- s1 .SetVerificationError (verificationErr , resMatch )
68
+ s1 .SetVerificationError (verificationErr , key )
64
69
}
65
70
66
71
results = append (results , s1 )
@@ -69,8 +74,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
69
74
return results , nil
70
75
}
71
76
72
- func verifyAccuweather (ctx context.Context , client * http.Client , resMatch string ) (bool , error ) {
73
- req , err := http .NewRequestWithContext (ctx , http .MethodGet , accuweatherURL + "/locations/v1/cities/autocomplete?apikey=" + resMatch + "&q=----&language=en-us" , nil )
77
+ func verifyAccuweather (ctx context.Context , client * http.Client , key string ) (bool , error ) {
78
+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , accuweatherURL + "/locations/v1/cities/autocomplete?apikey=" + key + "&q=----&language=en-us" , nil )
74
79
if err != nil {
75
80
return false , err
76
81
}
@@ -97,5 +102,5 @@ func (s Scanner) Type() detectorspb.DetectorType {
97
102
}
98
103
99
104
func (s Scanner ) Description () string {
100
- return "Accuweather is a weather forecasting service. Accuweather API keys can be used to access weather data and forecasts."
105
+ return "AccuWeather is a weather forecasting service. AccuWeather API keys can be used to access weather data and forecasts."
101
106
}
0 commit comments