Add reserved keys

This commit is contained in:
Daan Sprenkels 2019-09-01 12:04:43 +02:00
parent 0d7f7c7808
commit 73814e0f5b

View File

@ -5,6 +5,7 @@ package main
//go:generate go-bindata -pkg $GOPACKAGE assets/ //go:generate go-bindata -pkg $GOPACKAGE assets/
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"crypto/subtle" "crypto/subtle"
"encoding/base64" "encoding/base64"
@ -47,6 +48,9 @@ const (
const CookieOwnerToken = "owner_token" const CookieOwnerToken = "owner_token"
// These keys are designated reserved, and will not be randomly chosen
var ReservedPasteKeys [][]byte = [][]byte{[]byte("xd42"), []byte("example")}
// Base64 encoding and decoding // Base64 encoding and decoding
var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
var base64Encoder = base64.NewEncoding(base64Alphabet).WithPadding(base64.NoPadding) var base64Encoder = base64.NewEncoding(base64Alphabet).WithPadding(base64.NoPadding)
@ -286,10 +290,23 @@ func shortenURL(tx *bolt.Tx, userURL *url.URL, ownerKey [16]byte) (*StoredPaste,
if err != nil { if err != nil {
return nil, errors.Wrap(err, "url-key generation failed") return nil, errors.Wrap(err, "url-key generation failed")
} }
found := shortenBucket.Get(urlKey) found := shortenBucket.Get(urlKey)
if found == nil { if found == nil {
break break
} }
isReserved := false
for _, reservedKey := range ReservedPasteKeys {
if bytes.HasPrefix(urlKey, reservedKey) {
isReserved = true
break
}
}
if !isReserved {
break
}
epoch++ epoch++
} }