github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/resource_transformers/integrity/integrity.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     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  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package integrity
    15  
    16  import (
    17  	"crypto/md5"
    18  	"crypto/sha256"
    19  	"crypto/sha512"
    20  	"encoding/base64"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"hash"
    24  	"io"
    25  
    26  	"github.com/gohugoio/hugo/resources/internal"
    27  
    28  	"github.com/gohugoio/hugo/resources"
    29  	"github.com/gohugoio/hugo/resources/resource"
    30  )
    31  
    32  const defaultHashAlgo = "sha256"
    33  
    34  // Client contains methods to fingerprint (cachebusting) and other integrity-related
    35  // methods.
    36  type Client struct {
    37  	rs *resources.Spec
    38  }
    39  
    40  // New creates a new Client with the given specification.
    41  func New(rs *resources.Spec) *Client {
    42  	return &Client{rs: rs}
    43  }
    44  
    45  type fingerprintTransformation struct {
    46  	algo string
    47  }
    48  
    49  func (t *fingerprintTransformation) Key() internal.ResourceTransformationKey {
    50  	return internal.NewResourceTransformationKey("fingerprint", t.algo)
    51  }
    52  
    53  // Transform creates a MD5 hash of the Resource content and inserts that hash before
    54  // the extension in the filename.
    55  func (t *fingerprintTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
    56  	h, err := newHash(t.algo)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	var w io.Writer
    62  	if rc, ok := ctx.From.(io.ReadSeeker); ok {
    63  		// This transformation does not change the content, so try to
    64  		// avoid writing to To if we can.
    65  		defer rc.Seek(0, 0)
    66  		w = h
    67  	} else {
    68  		w = io.MultiWriter(h, ctx.To)
    69  	}
    70  
    71  	io.Copy(w, ctx.From)
    72  	d, err := digest(h)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	ctx.Data["Integrity"] = integrity(t.algo, d)
    78  	ctx.AddOutPathIdentifier("." + hex.EncodeToString(d[:]))
    79  	return nil
    80  }
    81  
    82  func newHash(algo string) (hash.Hash, error) {
    83  	switch algo {
    84  	case "md5":
    85  		return md5.New(), nil
    86  	case "sha256":
    87  		return sha256.New(), nil
    88  	case "sha384":
    89  		return sha512.New384(), nil
    90  	case "sha512":
    91  		return sha512.New(), nil
    92  	default:
    93  		return nil, fmt.Errorf("unsupported hash algorithm: %q, use either md5, sha256, sha384 or sha512", algo)
    94  	}
    95  }
    96  
    97  // Fingerprint applies fingerprinting of the given resource and hash algorithm.
    98  // It defaults to sha256 if none given, and the options are md5, sha256 or sha512.
    99  // The same algo is used for both the fingerprinting part (aka cache busting) and
   100  // the base64-encoded Subresource Integrity hash, so you will have to stay away from
   101  // md5 if you plan to use both.
   102  // See https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
   103  func (c *Client) Fingerprint(res resources.ResourceTransformer, algo string) (resource.Resource, error) {
   104  	if algo == "" {
   105  		algo = defaultHashAlgo
   106  	}
   107  
   108  	return res.Transform(&fingerprintTransformation{algo: algo})
   109  }
   110  
   111  func integrity(algo string, sum []byte) string {
   112  	encoded := base64.StdEncoding.EncodeToString(sum)
   113  	return algo + "-" + encoded
   114  }
   115  
   116  func digest(h hash.Hash) ([]byte, error) {
   117  	sum := h.Sum(nil)
   118  	return sum, nil
   119  }