github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/remotesrv/sealer.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remotesrv
    16  
    17  import (
    18  	"crypto/aes"
    19  	"crypto/cipher"
    20  	"crypto/rand"
    21  	"encoding/base64"
    22  	"errors"
    23  	"fmt"
    24  	"net/url"
    25  	"strconv"
    26  	"strings"
    27  	"time"
    28  )
    29  
    30  // Interface to seal requests to the HTTP server so that they cannot be forged.
    31  // The gRPC server seals URLs and the HTTP server unseals them.
    32  type Sealer interface {
    33  	Seal(*url.URL) (*url.URL, error)
    34  	Unseal(*url.URL) (*url.URL, error)
    35  }
    36  
    37  var _ Sealer = identitySealer{}
    38  
    39  type identitySealer struct {
    40  }
    41  
    42  func (identitySealer) Seal(u *url.URL) (*url.URL, error) {
    43  	return u, nil
    44  }
    45  
    46  func (identitySealer) Unseal(u *url.URL) (*url.URL, error) {
    47  	return u, nil
    48  }
    49  
    50  // Seals a URL by encrypting its Path and Query components and passing those in
    51  // a base64 encoded query parameter. Adds a not before timestamp (nbf) and an
    52  // expiration timestamp (exp) as query parameters. Encrypts the URL with
    53  // AES-256 GCM and adds the nbf and exp parameters as authenticated data.
    54  type singleSymmetricKeySealer struct {
    55  	privateKeyBytes []byte
    56  }
    57  
    58  func NewSingleSymmetricKeySealer() (Sealer, error) {
    59  	var key [32]byte
    60  	_, err := rand.Read(key[:])
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return singleSymmetricKeySealer{privateKeyBytes: key[:]}, nil
    65  }
    66  
    67  func (s singleSymmetricKeySealer) Seal(u *url.URL) (*url.URL, error) {
    68  	requestURI := (&url.URL{
    69  		Path:     u.EscapedPath(),
    70  		RawQuery: u.RawQuery,
    71  	}).String()
    72  	nbf := time.Now().Add(-10 * time.Second)
    73  	exp := time.Now().Add(15 * time.Minute)
    74  	nbfStr := strconv.FormatInt(nbf.UnixMilli(), 10)
    75  	expStr := strconv.FormatInt(exp.UnixMilli(), 10)
    76  	var nonceBytes [12]byte
    77  	_, err := rand.Read(nonceBytes[:])
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	nonceStr := base64.RawURLEncoding.EncodeToString(nonceBytes[:])
    82  
    83  	block, err := aes.NewCipher(s.privateKeyBytes)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("internal error: error making aes cipher with key: %w", err)
    86  	}
    87  	aesgcm, err := cipher.NewGCM(block)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("internal error: error making gcm mode opener with key: %w", err)
    90  	}
    91  
    92  	reqBytes := aesgcm.Seal(nil, nonceBytes[:], []byte(requestURI), []byte(nbfStr+":"+expStr))
    93  	reqStr := base64.RawURLEncoding.EncodeToString(reqBytes)
    94  
    95  	ret := *u
    96  	ret.Path = "/single_symmetric_key_sealed_request/" + u.EscapedPath()
    97  	ret.RawQuery = url.Values(map[string][]string{
    98  		"req":   []string{reqStr},
    99  		"nbf":   []string{strconv.FormatInt(nbf.UnixMilli(), 10)},
   100  		"exp":   []string{strconv.FormatInt(exp.UnixMilli(), 10)},
   101  		"nonce": []string{nonceStr},
   102  	}).Encode()
   103  	return &ret, nil
   104  }
   105  
   106  func (s singleSymmetricKeySealer) Unseal(u *url.URL) (*url.URL, error) {
   107  	if !strings.HasPrefix(u.Path, "/single_symmetric_key_sealed_request/") {
   108  		return nil, errors.New("bad request: cannot unseal URL whose path does not start with /single_symmetric_key_sealed_request/")
   109  	}
   110  	q := u.Query()
   111  	if !q.Has("nbf") {
   112  		return nil, errors.New("bad request: cannot unseal URL which does not include an nbf")
   113  	}
   114  	if !q.Has("exp") {
   115  		return nil, errors.New("bad request: cannot unseal URL which does not include an exp")
   116  	}
   117  	if !q.Has("nonce") {
   118  		return nil, errors.New("bad request: cannot unseal URL which does not include a nonce")
   119  	}
   120  	if !q.Has("req") {
   121  		return nil, errors.New("bad request: cannot unseal URL which does not include a req")
   122  	}
   123  	nbfStr := q.Get("nbf")
   124  	expStr := q.Get("exp")
   125  	nonceStr := q.Get("nonce")
   126  
   127  	nbf, err := strconv.ParseInt(nbfStr, 10, 64)
   128  	if err != nil {
   129  		return nil, fmt.Errorf("bad request: error parsing nbf as int64: %w", err)
   130  	}
   131  	exp, err := strconv.ParseInt(expStr, 10, 64)
   132  	if err != nil {
   133  		return nil, fmt.Errorf("bad request: error parsing exp as int64: %w", err)
   134  	}
   135  	nonce, err := base64.RawURLEncoding.DecodeString(nonceStr)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("bad request: error parsing nonce as base64 URL encoded: %w", err)
   138  	}
   139  
   140  	if time.Now().Before(time.UnixMilli(nbf)) {
   141  		return nil, fmt.Errorf("bad request: nbf is invalid")
   142  	}
   143  	if time.Now().After(time.UnixMilli(exp)) {
   144  		return nil, fmt.Errorf("bad request: exp is invalid")
   145  	}
   146  
   147  	block, err := aes.NewCipher(s.privateKeyBytes)
   148  	if err != nil {
   149  		return nil, fmt.Errorf("internal error: error making aes cipher with key: %w", err)
   150  	}
   151  	aesgcm, err := cipher.NewGCM(block)
   152  	if err != nil {
   153  		return nil, fmt.Errorf("internal error: error making gcm mode opener with key: %w", err)
   154  	}
   155  
   156  	reqStr := q.Get("req")
   157  	reqBytes, err := base64.RawURLEncoding.DecodeString(reqStr)
   158  	if err != nil {
   159  		return nil, fmt.Errorf("bad request: error parsing req as base64 URL encoded: %w", err)
   160  	}
   161  
   162  	requestURI, err := aesgcm.Open(nil, nonce, reqBytes, []byte(nbfStr+":"+expStr))
   163  	if err != nil {
   164  		return nil, fmt.Errorf("bad request: error opening sealed url: %w", err)
   165  	}
   166  	requestURL, err := url.Parse(string(requestURI))
   167  	if err != nil {
   168  		return nil, fmt.Errorf("bad request: error parsing unsealed request uri: %w", err)
   169  	}
   170  
   171  	if strings.TrimPrefix(u.Path, "/single_symmetric_key_sealed_request/") != requestURL.EscapedPath() {
   172  		return nil, fmt.Errorf("bad request: unsealed request path did not equal request path in sealed request")
   173  	}
   174  
   175  	ret := *u
   176  	ret.Path = requestURL.Path
   177  	ret.RawQuery = requestURL.RawQuery
   178  	return &ret, nil
   179  }