blob: ef7a6d684a54167dd5d6ae2f4e3051acf306889a [file] [log] [blame]
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
)
const (
// This salt is not meant to be kept secret (it’s checked in after all). It’s
// a tiny bit of paranoia to avoid whatever problems a collision may cause.
salt = "Go playground salt\n"
maxSnippetSize = 64 * 1024
)
type snippet struct {
Body []byte `datastore:",noindex"` // golang.org/issues/23253
}
func (s *snippet) ID() string {
h := sha256.New()
io.WriteString(h, salt)
h.Write(s.Body)
sum := h.Sum(nil)
b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
base64.URLEncoding.Encode(b, sum)
// Web sites don’t always linkify a trailing underscore, making it seem like
// the link is broken. If there is an underscore at the end of the substring,
// extend it until there is not.
hashLen := 11
for hashLen <= len(b) && b[hashLen-1] == '_' {
hashLen++
}
return string(b)[:hashLen]
}
func (s *server) handleShare(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
// This is likely a pre-flight CORS request.
return
}
if r.Method != "POST" {
http.Error(w, "Requires POST", http.StatusMethodNotAllowed)
return
}
if !allowShare(r) {
http.Error(w, "Either this isn't available in your country due to legal reasons, or our IP geolocation is wrong.",
http.StatusUnavailableForLegalReasons)
return
}
var body bytes.Buffer
_, err := io.Copy(&body, io.LimitReader(r.Body, maxSnippetSize+1))
r.Body.Close()
if err != nil {
s.log.Errorf("reading Body: %v", err)
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
if body.Len() > maxSnippetSize {
http.Error(w, "Snippet is too large", http.StatusRequestEntityTooLarge)
return
}
snip := &snippet{Body: body.Bytes()}
id := snip.ID()
if err := s.db.PutSnippet(r.Context(), id, snip); err != nil {
s.log.Errorf("putting Snippet: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprint(w, id)
}
// golang/go#65081: the IP prefixes below have been observed in proxied traffic
// from go.dev to play.golang.org, and as of 2024-01-16 are incorrectly
// identified as CN by X-AppEngine-Country. Using geoIP tooling, these were all
// verified as having temporarily been categorized as CN in early January, but
// are currently US.
//
// While this is being investigated, hard-code a temporary allow list for these
// IPs to get the playground working again.
//
// Per https://www.gstatic.com/ipranges/goog.json, these are Google IPs.
var temporaryAllowListIPPrefixes = []string{
"2600:1900:2001:2",
"2600:1900:2001:3",
"2600:1900:2000:1b",
"2600:1900:2000:1d",
"2600:1900:2000:38",
"2600:1900:2000:37",
"2600:1900:2000:9",
}
func allowShare(r *http.Request) bool {
if r.Header.Get("X-AppEngine-Country") != "CN" {
return true
}
for _, forward := range strings.Split(r.Header.Get("X-Forwarded-For"), ",") {
for _, prefix := range temporaryAllowListIPPrefixes {
if strings.HasPrefix(strings.TrimSpace(forward), prefix) {
return true
}
}
}
return false
}