github.com/google/osv-scalibr@v0.4.1/veles/secrets/gcpoauth2access/detector.go (about)

     1  // Copyright 2025 Google LLC
     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 gcpoauth2access
    16  
    17  import (
    18  	"regexp"
    19  
    20  	"github.com/google/osv-scalibr/veles"
    21  	"github.com/google/osv-scalibr/veles/secrets/common/simpletoken"
    22  )
    23  
    24  // Enforce detector interface.
    25  var _ veles.Detector = NewDetector()
    26  
    27  const (
    28  	// maxTokenLength is the maximum size of a GCP OAuth2 access token.
    29  	maxTokenLength = 500
    30  )
    31  
    32  var (
    33  	// tokenRe is a regular expression that matches GCP OAuth2 access tokens.
    34  	// Pattern: ya29. followed by alphanumeric characters, underscores, and hyphens (case-insensitive)
    35  	// https://cloud.google.com/docs/authentication/token-types#access-tokens
    36  	// There are not documented lower and upper bounds on the length of the token.
    37  	// But based on the pattern and https://datatracker.ietf.org/doc/html/rfc4648#section-5,
    38  	// token length should be at least 10 characters.
    39  	// The upper bound is based on the assumption that a typical access token is around 256 bits long,
    40  	// which is true for real examples and documentation.
    41  	// This is needed to avoid matching larger blobs of text.
    42  	tokenRe = regexp.MustCompile(`\bya29\.[a-zA-Z0-9_-]{10,500}`)
    43  )
    44  
    45  // NewDetector returns a new simpletoken.Detector that matches GCP OAuth2 access tokens.
    46  func NewDetector() veles.Detector {
    47  	return simpletoken.Detector{
    48  		MaxLen: maxTokenLength,
    49  		Re:     tokenRe,
    50  		FromMatch: func(b []byte) (veles.Secret, bool) {
    51  			return Token{Token: string(b)}, true
    52  		},
    53  	}
    54  }