Skip to content

Commit bff0b95

Browse files
DavidS-ovmactions-user
authored andcommitted
stdlib: remove query args and fragments from redirect URLs (#2598)
This will reduce the number of random redirect-to-oauth items, which provide no value to the blast-radius. The trade-off is that we now might be missing some valid redirect chains that would require the args to pass through. GitOrigin-RevId: c897427fe3cdbef7dfcac3647c0e39c8c52eb6f0
1 parent c166049 commit bff0b95

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

stdlib-source/adapters/http.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"fmt"
88
"net"
99
"net/http"
10+
"net/url"
1011
"runtime"
1112
"strings"
1213
"sync"
1314
"time"
1415

1516
"github.com/overmindtech/cli/sdp-go"
1617
"github.com/overmindtech/cli/sdpcache"
18+
"google.golang.org/protobuf/types/known/structpb"
1719
)
1820

1921
const USER_AGENT_VERSION = "0.1"
@@ -271,19 +273,38 @@ func (s *HTTPAdapter) Get(ctx context.Context, scope string, query string, ignor
271273
// Detect redirect and add a linked item for the redirect target
272274
if res.StatusCode >= 300 && res.StatusCode < 400 {
273275
if loc := res.Header.Get("Location"); loc != "" {
274-
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
275-
Query: &sdp.Query{
276-
Type: "http",
277-
Method: sdp.QueryMethod_GET,
278-
Query: loc,
279-
Scope: scope,
280-
},
281-
BlastPropagation: &sdp.BlastPropagation{
282-
// Redirects are tightly coupled
283-
In: true,
284-
Out: true,
276+
item.Attributes.AttrStruct.Fields["location"] = &structpb.Value{
277+
Kind: &structpb.Value_StringValue{
278+
StringValue: loc,
285279
},
286-
})
280+
}
281+
locU, err := url.Parse(loc)
282+
if err != nil {
283+
item.Attributes.AttrStruct.Fields["location-error"] = &structpb.Value{
284+
Kind: &structpb.Value_StringValue{
285+
StringValue: err.Error(),
286+
},
287+
}
288+
} else {
289+
// Don't include query string and fragment in the linked item.
290+
// This leads to too many items, like auth redirect errors, that
291+
// do not provide value.
292+
locU.Fragment = ""
293+
locU.RawQuery = ""
294+
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
295+
Query: &sdp.Query{
296+
Type: "http",
297+
Method: sdp.QueryMethod_GET,
298+
Query: locU.String(),
299+
Scope: scope,
300+
},
301+
BlastPropagation: &sdp.BlastPropagation{
302+
// Redirects are tightly coupled
303+
In: true,
304+
Out: true,
305+
},
306+
})
307+
}
287308
}
288309
}
289310

stdlib-source/adapters/http_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewTestServer() (*TestHTTPServer, error) {
4747
}))
4848

4949
sm.Handle("/301", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50-
w.Header().Add("Location", "https://www.google.com")
50+
w.Header().Add("Location", "https://www.google.com?foo=bar#baz")
5151
w.WriteHeader(http.StatusMovedPermanently)
5252
}))
5353

@@ -232,8 +232,18 @@ func TestHTTPGet(t *testing.T) {
232232
t.Errorf("expected status to be 301, got: %v", status)
233233
}
234234

235-
if len(item.GetLinkedItemQueries()) == 0 {
236-
t.Error("expected a linked item to redirected location, got none")
235+
liqs := item.GetLinkedItemQueries()
236+
if len(liqs) != 3 {
237+
t.Errorf("expected linked items for redirected location, ip, and dns, got %v: %v", len(liqs), liqs)
238+
}
239+
for l := range liqs {
240+
// Look for the linked item with the http query to the redirect
241+
// location, check that the query and fragment have been stripped.
242+
if liqs[l].GetQuery().GetType() == "http" {
243+
if liqs[l].GetQuery().GetQuery() != "https://www.google.com" {
244+
t.Errorf("expected linked item query to be https://www.google.com, got %v", liqs[l].GetQuery().GetQuery())
245+
}
246+
}
237247
}
238248

239249
discovery.TestValidateItem(t, item)

0 commit comments

Comments
 (0)