|
| 1 | +// Copyright 2026 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 uspassportnumber implements logic for detecting US passport numbers |
| 16 | +package uspassportnumber |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "regexp" |
| 21 | + |
| 22 | + "github.com/google/osv-scalibr/veles" |
| 23 | + "github.com/google/osv-scalibr/veles/sensitiveinformation" |
| 24 | + "github.com/google/osv-scalibr/veles/sensitiveinformation/common/simpleregex" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + maxPassportNumberLen = 9 |
| 29 | + maxKeywordLen = 20 |
| 30 | + contextWindowSize = 32 |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + keywordsRe = simpleregex.KeywordsRe([]string{ |
| 35 | + `pass`, |
| 36 | + `passport`, |
| 37 | + `travel`, |
| 38 | + `doc`, |
| 39 | + `document`, |
| 40 | + }) |
| 41 | + passportNumberRe = regexp.MustCompile(`\b[A-Za-z][0-9]{8}\b`) |
| 42 | +) |
| 43 | + |
| 44 | +// NewDetector returns a Detector, that finds US Passport Numbers |
| 45 | +func NewDetector() veles.Detector { |
| 46 | + return simpleregex.Detector{ |
| 47 | + MaxLen: max(maxKeywordLen, maxPassportNumberLen), |
| 48 | + Re: passportNumberRe, |
| 49 | + KeywordsRe: keywordsRe, |
| 50 | + ContextWindowBefore: contextWindowSize, |
| 51 | + ContextWindowAfter: contextWindowSize, |
| 52 | + FromMatch: func(blob []byte, keywordMatch bool) (sensitiveinformation.SensitiveInformation, bool) { |
| 53 | + likelihood := sensitiveinformation.LikelihoodUnlikely |
| 54 | + if keywordMatch { |
| 55 | + likelihood = sensitiveinformation.LikelihoodLikely |
| 56 | + } |
| 57 | + |
| 58 | + return sensitiveinformation.SensitiveInformation{ |
| 59 | + InfoType: sensitiveinformation.InfoType{ |
| 60 | + Name: "US_PASSPORT_NUMBER", |
| 61 | + Sensitivity: sensitiveinformation.SensitivityLevelHigh, |
| 62 | + }, |
| 63 | + Likelihood: likelihood, |
| 64 | + Raw: bytes.Clone(blob), |
| 65 | + }, true |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
0 commit comments