Skip to content

Commit ddba9f5

Browse files
Web Risk Teamrvilgalys
Web Risk Team
authored andcommitted
Internal change
PiperOrigin-RevId: 504834508
1 parent 4677e1e commit ddba9f5

23 files changed

+452
-397
lines changed

Dockerfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ RUN go mod download && go mod verify
88

99
COPY . .
1010

11-
# Uncomment the lines below to vet and test this container before building.
12-
# RUN go vet -v
13-
# RUN go test -v
11+
# Analyze our Go code and run all tests. To speed up building, such as during
12+
# development, consider commenting out these lines.
13+
RUN go vet -v
14+
RUN go test -v
1415

1516
RUN CGO_ENABLED=0 go build -o /go/bin/wrserver cmd/wrserver/main.go
1617

@@ -19,5 +20,5 @@ FROM gcr.io/distroless/static-debian11 as wrserver
1920
COPY --from=build /go/bin/wrserver /
2021

2122
# The APIKEY Environmental Variable should be passed in at runtime. Example:
22-
# docker run -e APIKEY=XXXXXXXXXXXXXXXXXXXXX -p 8080:8080
23-
ENTRYPOINT [ "/wrserver"]
23+
# docker run -e APIKEY=XXXXXXXXXXXXXXXXXXXXX -p 8080:8080 <container label>
24+
ENTRYPOINT [ "/wrserver"]

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,4 @@
199199
distributed under the License is distributed on an "AS IS" BASIS,
200200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201201
See the License for the specific language governing permissions and
202-
limitations under the License.
202+
limitations under the License.

api.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2023 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -22,24 +22,24 @@ import (
2222
"net/url"
2323
"strings"
2424

25+
"google.golang.org/protobuf/encoding/protojson"
26+
"google.golang.org/protobuf/proto"
2527
pb "github.com/google/webrisk/internal/webrisk_proto"
26-
"github.com/golang/protobuf/proto"
27-
"github.com/golang/protobuf/jsonpb"
2828
)
2929

3030
const (
31-
findHashPath = "v1/hashes:search"
32-
fetchUpdatePath = "v1/threatLists:computeDiff"
33-
threatTypeString = "threat_type"
34-
versionTokenString = "version_token"
31+
findHashPath = "v1/hashes:search"
32+
fetchUpdatePath = "v1/threatLists:computeDiff"
33+
threatTypeString = "threat_type"
34+
versionTokenString = "version_token"
3535
supportedCompressionsString = "constraints.supported_compressions"
36-
hashPrefixString = "hash_prefix"
37-
threatTypesString = "threat_types"
36+
hashPrefixString = "hash_prefix"
37+
threatTypesString = "threat_types"
3838
)
3939

4040
// The api interface specifies wrappers around the Web Risk API.
4141
type api interface {
42-
ListUpdate(ctx context.Context, threat_type pb.ThreatType, version_token []byte,
42+
ListUpdate(ctx context.Context, threatType pb.ThreatType, versionToken []byte,
4343
compressionTypes []pb.CompressionType) (*pb.ComputeThreatListDiffResponse, error)
4444
HashLookup(ctx context.Context, hashPrefix []byte,
4545
threatTypes []pb.ThreatType) (*pb.SearchHashesResponse, error)
@@ -67,11 +67,11 @@ func newNetAPI(root string, key string, proxy string) (*netAPI, error) {
6767
httpClient := &http.Client{}
6868

6969
if proxy != "" {
70-
proxyUrl, err := url.Parse(proxy)
70+
proxyURL, err := url.Parse(proxy)
7171
if err != nil {
7272
return nil, err
7373
}
74-
httpClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
74+
httpClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
7575
}
7676

7777
q := u.Query()
@@ -84,6 +84,9 @@ func newNetAPI(root string, key string, proxy string) (*netAPI, error) {
8484
// response body payload as resp.
8585
func (a *netAPI) doRequest(ctx context.Context, urlString string, resp proto.Message) error {
8686
httpReq, err := http.NewRequest("GET", urlString, nil)
87+
if err != nil {
88+
return err
89+
}
8790
httpReq.Header.Add("Content-Type", "application/json")
8891
httpReq.Header.Add("User-Agent", "Webrisk-Client/0.1.3")
8992
httpReq = httpReq.WithContext(ctx)
@@ -99,7 +102,7 @@ func (a *netAPI) doRequest(ctx context.Context, urlString string, resp proto.Mes
99102
if err != nil {
100103
return err
101104
}
102-
return jsonpb.UnmarshalString(string(body), resp)
105+
return protojson.Unmarshal(body, resp)
103106
}
104107

105108
// ListUpdate issues a ComputeThreatListDiff API call and returns the response.

api_test.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2023 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,9 +21,9 @@ import (
2121
"reflect"
2222
"testing"
2323

24+
"google.golang.org/protobuf/encoding/protojson"
25+
"google.golang.org/protobuf/proto"
2426
pb "github.com/google/webrisk/internal/webrisk_proto"
25-
"github.com/golang/protobuf/jsonpb"
26-
"github.com/golang/protobuf/proto"
2727
)
2828

2929
type mockAPI struct {
@@ -51,47 +51,46 @@ func TestNetAPI(t *testing.T) {
5151
var gotResp, wantResp proto.Message
5252
responseMisformatter := ""
5353
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54-
var p string
54+
var p []byte
5555
var err error
56-
marshaler := jsonpb.Marshaler{}
5756
for key, value := range r.URL.Query() {
58-
if (key == "threat_type") {
59-
if (len(value) == 0) {
57+
if key == "threat_type" {
58+
if len(value) == 0 {
6059
t.Fatalf("missing value for key: %v", key)
6160
}
6261
gotReqThreatType = pb.ThreatType(pb.ThreatType_value[value[0]])
63-
} else if (key == "constraints.supported_compressions") {
64-
if (len(value) == 0) {
62+
} else if key == "constraints.supported_compressions" {
63+
if len(value) == 0 {
6564
t.Fatalf("missing value for key: %v", key)
6665
}
6766
for _, comp := range value {
6867
gotReqCompressionTypes = append(gotReqCompressionTypes,
6968
pb.CompressionType(pb.CompressionType_value[comp]))
7069
}
71-
} else if (key == "hash_prefix") {
72-
if (len(value) == 0) {
70+
} else if key == "hash_prefix" {
71+
if len(value) == 0 {
7372
t.Fatalf("missing value for key: %v", key)
7473
}
7574
gotReqHashPrefix, err = base64.StdEncoding.DecodeString(value[0])
76-
if (err != nil) {
75+
if err != nil {
7776
t.Fatalf("unexpected hash prefix decoding error for: %v", value[0])
7877
}
79-
} else if (key == "threat_types") {
80-
if (len(value) == 0) {
78+
} else if key == "threat_types" {
79+
if len(value) == 0 {
8180
t.Fatalf("missing value for key: %v", key)
8281
}
8382
for _, threat := range value {
8483
gotReqThreatTypes = append(gotReqThreatTypes,
8584
pb.ThreatType(pb.ThreatType_value[threat]))
8685
}
87-
} else if (key != "key") {
86+
} else if key != "key" {
8887
t.Fatalf("unexpected request param error for key: %v", key)
8988
}
9089
}
91-
if p, err = marshaler.MarshalToString(wantResp); err != nil {
92-
t.Fatalf("unexpected jsonpb MarshalToString error: %v", err)
90+
if p, err = protojson.Marshal(wantResp); err != nil {
91+
t.Fatalf("unexpected json MarshalToString error: %v", err)
9392
}
94-
if _, err := w.Write([]byte(responseMisformatter + p)); err != nil {
93+
if _, err := w.Write([]byte(responseMisformatter + string(p))); err != nil {
9594
t.Fatalf("unexpected ResponseWriter.Write error: %v", err)
9695
}
9796
}))
@@ -113,8 +112,8 @@ func TestNetAPI(t *testing.T) {
113112
RawIndices: &pb.RawIndices{Indices: []int32{1, 2, 3}},
114113
},
115114
}
116-
resp1, err := api.ListUpdate(context.Background(), wantReqThreatType, []byte {},
117-
wantReqCompressionTypes)
115+
resp1, err := api.ListUpdate(context.Background(), wantReqThreatType, []byte{},
116+
wantReqCompressionTypes)
118117
gotResp = resp1
119118
if err != nil {
120119
t.Errorf("unexpected ListUpdate error: %v", err)
@@ -127,7 +126,7 @@ func TestNetAPI(t *testing.T) {
127126
t.Errorf("mismatching ListUpdate requests for compression types:\ngot %+v\nwant %+v",
128127
gotReqCompressionTypes, wantReqCompressionTypes)
129128
}
130-
if !reflect.DeepEqual(gotResp, wantResp) {
129+
if !proto.Equal(gotResp, wantResp) {
131130
t.Errorf("mismatching ListUpdate responses:\ngot %+v\nwant %+v", gotResp, wantResp)
132131
}
133132

@@ -151,7 +150,7 @@ func TestNetAPI(t *testing.T) {
151150
t.Errorf("mismatching HashLookup requests for threat types:\ngot %+v\nwant %+v",
152151
gotReqThreatTypes, wantReqThreatTypes)
153152
}
154-
if !reflect.DeepEqual(gotResp, wantResp) {
153+
if !proto.Equal(gotResp, wantResp) {
155154
t.Errorf("mismatching HashLookup responses:\ngot %+v\nwant %+v", gotResp, wantResp)
156155
}
157156

cache.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2023 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,12 +14,10 @@
1414
package webrisk
1515

1616
import (
17-
"fmt"
1817
"sync"
1918
"time"
2019

2120
pb "github.com/google/webrisk/internal/webrisk_proto"
22-
pt "github.com/golang/protobuf/ptypes"
2321
)
2422

2523
type cacheResult int
@@ -85,17 +83,13 @@ func (c *cache) Update(req *pb.SearchHashesRequest, resp *pb.SearchHashesRespons
8583
c.pttls[fullHash] = make(map[ThreatType]time.Time)
8684
}
8785
for _, tt := range threat.ThreatTypes {
88-
var err error
89-
c.pttls[fullHash][ThreatType(tt)], err = pt.Timestamp(threat.ExpireTime)
90-
if err != nil {
91-
return fmt.Errorf("pt.Timestamp: %v", err)
92-
}
86+
c.pttls[fullHash][ThreatType(tt)] = threat.ExpireTime.AsTime()
9387
}
9488
}
9589

9690
// Insert negative TTLs for partial hashes.
9791
if resp.GetNegativeExpireTime() != nil {
98-
nttl, _ := pt.Timestamp(resp.GetNegativeExpireTime())
92+
nttl := resp.GetNegativeExpireTime().AsTime()
9993
partialHash := hashPrefix(req.HashPrefix)
10094
c.nttls[partialHash] = nttl
10195
}

cache_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2023 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,8 +18,9 @@ import (
1818
"testing"
1919
"time"
2020

21+
timepb "google.golang.org/protobuf/types/known/timestamppb"
22+
2123
pb "github.com/google/webrisk/internal/webrisk_proto"
22-
pt "github.com/golang/protobuf/ptypes"
2324
)
2425

2526
func TestCacheLookup(t *testing.T) {
@@ -164,8 +165,9 @@ func TestCacheLookup(t *testing.T) {
164165
func TestCacheUpdate(t *testing.T) {
165166
now := time.Unix(1451436338, 951473000)
166167
mockNow := func() time.Time { return now }
167-
ts, _ := pt.TimestampProto(now.Add(1000 * time.Second))
168-
tft, _ := pt.Timestamp(ts)
168+
169+
ts := timepb.New(now.Add(1000 * time.Second))
170+
tft := ts.AsTime()
169171

170172
vectors := []struct {
171173
req *pb.SearchHashesRequest

cmd/wrlookup/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2023 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,17 +11,18 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
1514
// Command wrlookup is a tool for looking up URLs via the command-line.
1615
//
1716
// The tool reads one URL per line from STDIN and checks every URL against
1817
// the Web Risk API. The "Safe" or "Unsafe" verdict is printed to STDOUT.
1918
// If an error occurred, debug information may be printed to STDERR.
2019
//
2120
// To build the tool:
21+
//
2222
// $ go get github.com/google/webrisk/cmd/wrlookup
2323
//
2424
// Example usage:
25+
//
2526
// $ wrlookup -apikey $APIKEY
2627
// https://google.com
2728
// Safe URL: https://google.com
@@ -33,8 +34,9 @@ import (
3334
"bufio"
3435
"flag"
3536
"fmt"
36-
"github.com/google/webrisk"
3737
"os"
38+
39+
"github.com/google/webrisk"
3840
)
3941

4042
var (
@@ -77,7 +79,7 @@ func main() {
7779
fmt.Fprintln(os.Stderr, "No -apikey specified")
7880
os.Exit(codeInvalid)
7981
}
80-
sb, err := webrisk.NewWebriskClient(webrisk.Config{
82+
sb, err := webrisk.NewUpdateClient(webrisk.Config{
8183
APIKey: *apiKeyFlag,
8284
DBPath: *databaseFlag,
8385
Logger: os.Stderr,

0 commit comments

Comments
 (0)