From 4df6ae9fadf1b67873018036b2707fceb651da04 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Wed, 2 Sep 2015 08:40:41 +1000 Subject: [PATCH] playground: block share functionality from specific countries This will permit us to serve *.golang.org to Chinese users. Change-Id: If184760d7f4c9e49a3df3785c15af770958413de Reviewed-on: https://go-review.googlesource.com/14190 Reviewed-by: Jason Buberel Reviewed-by: Brad Fitzpatrick --- playground/appengine.go | 4 ++++ playground/common.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/playground/appengine.go b/playground/appengine.go index 073b419d..94c5d523 100644 --- a/playground/appengine.go +++ b/playground/appengine.go @@ -13,6 +13,10 @@ import ( "appengine/urlfetch" ) +func init() { + onAppengine = !appengine.IsDevAppServer() +} + func client(r *http.Request) *http.Client { return urlfetch.Client(appengine.NewContext(r)) } diff --git a/playground/common.go b/playground/common.go index 3ffce883..9f428f51 100644 --- a/playground/common.go +++ b/playground/common.go @@ -9,6 +9,7 @@ package playground // import "golang.org/x/tools/playground" import ( "bytes" + "errors" "fmt" "io" "net/http" @@ -32,6 +33,9 @@ func bounce(w http.ResponseWriter, r *http.Request) { } func passThru(w io.Writer, req *http.Request) error { + if req.URL.Path == "/share" && !allowShare(req) { + return errors.New("Forbidden") + } defer req.Body.Close() url := baseURL + req.URL.Path r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body) @@ -44,3 +48,16 @@ func passThru(w io.Writer, req *http.Request) error { } return nil } + +var onAppengine = false // will be overriden by appengine.go + +func allowShare(r *http.Request) bool { + if !onAppengine { + return true + } + switch r.Header.Get("X-AppEngine-Country") { + case "", "ZZ", "HK", "CN", "RC": + return false + } + return true +}