Skip to content

Commit 694921d

Browse files
dmitshurgopherbot
authored andcommitted
cmd/adminapp: add protection against CSRF
Use CrossOriginProtection, which was added to net/http in Go 1.25, to reject non-safe cross-origin browser requests and help protect against Cross-Site Request Forgery (CSRF). Also modernize slightly while here by using strings.CutPrefix and removing a no-op return. Change-Id: I698d26e1fe70be3b606c6e67a7d204e9e0f245d9 Reviewed-on: https://go-review.googlesource.com/c/website/+/711900 Auto-Submit: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent d3762fc commit 694921d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cmd/adminapp/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Require go1.25+ for http.CrossOriginProtection.
6+
// (This can be deleted when go.mod is at 1.25.0+.)
7+
//go:build go1.25
8+
59
// The admingolangorg command serves an administrative interface for owners of
610
// the golang-org Google Cloud project.
711
package main
@@ -32,7 +36,6 @@ func main() {
3236
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3337
w.Header().Set("Content-Type", "text/html")
3438
w.Write([]byte(index))
35-
return
3639
}))
3740
mux.Handle("/shortlink", short.AdminHandler(dsClient, mcClient))
3841
mux.Handle("/snippet", &snippetHandler{dsClient})
@@ -41,9 +44,11 @@ func main() {
4144
port = "8080"
4245
log.Printf("Defaulting to port %s", port)
4346
}
47+
handler := iapAuth(audience, mux)
48+
handler = http.NewCrossOriginProtection().Handler(handler)
4449

4550
log.Printf("Listening on port %s", port)
46-
log.Fatal(http.ListenAndServe(":"+port, iapAuth(audience, mux)))
51+
log.Fatal(http.ListenAndServe(":"+port, handler))
4752
}
4853

4954
type snippetHandler struct {
@@ -69,8 +74,8 @@ func (h *snippetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6974
}
7075
var snippetID string
7176
for _, p := range prefixes {
72-
if strings.HasPrefix(snippetLink, p) {
73-
snippetID = strings.TrimPrefix(snippetLink, p)
77+
if id, ok := strings.CutPrefix(snippetLink, p); ok {
78+
snippetID = id
7479
break
7580
}
7681
}

0 commit comments

Comments
 (0)