-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from pace/add-redaction-system
add redaction system package #238
- Loading branch information
Showing
14 changed files
with
401 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/12/18 by Vincent Landgraf | ||
|
||
package redact | ||
|
||
import "context" | ||
|
||
type patternRedactorKey struct{} | ||
|
||
// WithContext allows storing the PatternRedactor inside a context for passing it on | ||
func (r *PatternRedactor) WithContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, patternRedactorKey{}, r) | ||
} | ||
|
||
// Ctx returns the PatternRedactor stored within the context. If no redactor | ||
// has been defined, an empty redactor is returned that does nothing | ||
func Ctx(ctx context.Context) *PatternRedactor { | ||
if rd, ok := ctx.Value(patternRedactorKey{}).(*PatternRedactor); ok { | ||
return rd.Clone() | ||
} | ||
return NewPatternRedactor(RedactionSchemeDoNothing()) | ||
} | ||
|
||
// ContextTransfer copies a request representation from one context to another. | ||
func ContextTransfer(ctx, targetCtx context.Context) context.Context { | ||
if redactor := Ctx(ctx); redactor != nil { | ||
return context.WithValue(targetCtx, patternRedactorKey{}, redactor) | ||
} | ||
return targetCtx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright © 2021 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2021/01/04 by Vincent Landgraf | ||
|
||
package redact | ||
|
||
// redactionSafe last 4 digits are usually concidered safe (e.g. credit cards, iban, ...) | ||
const redactionSafe = 4 | ||
|
||
var Default *PatternRedactor | ||
|
||
func init() { | ||
scheme := RedactionSchemeKeepLastJWTNoSignature(redactionSafe) | ||
Default = NewPatternRedactor(scheme) | ||
Default.AddPatterns(AllPatterns...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/12/18 by Vincent Landgraf | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pace/bricks/pkg/redact" | ||
) | ||
|
||
// Redact provides a pattern redactor middleware to the request context | ||
func Redact(next http.Handler) http.Handler { | ||
return RedactWithScheme(next, redact.Default) | ||
} | ||
|
||
// RedactWithScheme provides a pattern redactor middleware to the request context | ||
// using the provided scheme | ||
func RedactWithScheme(next http.Handler, redactor *redact.PatternRedactor) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := redactor.WithContext(r.Context()) | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/12/18 by Vincent Landgraf | ||
|
||
package redact | ||
|
||
import "regexp" | ||
|
||
// Sources: | ||
// CreditCard: https://www.regular-expressions.info/creditcard.html | ||
|
||
// AllPatterns is a list of all default redaction patterns | ||
var AllPatterns = []*regexp.Regexp{ | ||
PatternIBAN, | ||
PatternJWT, | ||
PatternCCVisa, | ||
PatternCCMasterCard, | ||
PatternCCAmericanExpress, | ||
PatternCCDinersClub, | ||
PatternCCDiscover, | ||
PatternCCJCB, | ||
} | ||
|
||
var ( | ||
PatternIBAN = regexp.MustCompile( | ||
`[a-zA-Z]{2}` + // DE, NL, ... | ||
`[0-9]{2}` + // 80 | ||
`(?:[ ]?[0-9a-zA-Z]{4})` + // 5001, INGB | ||
`(?:[ ]?[0-9]{4}){2,3}` + // 0517 2589 4683, 8731 3269 | ||
`(?:[ ]?[0-9]{1,2})?`, // 43, 66 | ||
) | ||
|
||
// All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. | ||
PatternCCVisa = regexp.MustCompile(`4[0-9]{12}(?:[0-9]{3})?`) | ||
|
||
// MasterCard numbers either start with the numbers 51 through 55 or with the numbers 2221 through 2720. All have 16 digits. | ||
PatternCCMasterCard = regexp.MustCompile(`(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}`) | ||
|
||
// American Express card numbers start with 34 or 37 and have 15 digits. | ||
PatternCCAmericanExpress = regexp.MustCompile(`3[47][0-9]{13}`) | ||
|
||
// Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard. | ||
PatternCCDinersClub = regexp.MustCompile(`3(?:0[0-5]|[68][0-9])[0-9]{11}`) | ||
|
||
// Discover card numbers begin with 6011 or 65. All have 16 digits. | ||
PatternCCDiscover = regexp.MustCompile(`6(?:011|5[0-9]{2})[0-9]{12}`) | ||
|
||
// JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits. | ||
PatternCCJCB = regexp.MustCompile(`(?:2131|1800|35\d{3})\d{11}`) | ||
|
||
// PatternJWT JsonWebToken | ||
PatternJWT = regexp.MustCompile(`(?:ey[a-zA-Z0-9=_-]+.){2}.[a-zA-Z0-9=_-]+`) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/12/18 by Vincent Landgraf | ||
|
||
package redact | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func validatePattern(t *testing.T, p *regexp.Regexp, s string, expected bool) { | ||
assert.Equal(t, expected, p.MatchString(s), fmt.Sprintf("expected %q to match %v, but didn't", s, p)) | ||
} | ||
|
||
func TestIBANPattern(t *testing.T) { | ||
validatePattern(t, PatternIBAN, "NL29INGB8731326943", true) | ||
validatePattern(t, PatternIBAN, "DE80500105172589468366", true) | ||
validatePattern(t, PatternIBAN, "NL29 INGB 8731 3269 43", true) | ||
validatePattern(t, PatternIBAN, "DE80 5001 0517 2589 4683 66", true) | ||
validatePattern(t, PatternIBAN, "CM7168156782527355483576522", true) | ||
validatePattern(t, PatternIBAN, "TL045597565817778146141", true) | ||
validatePattern(t, PatternIBAN, "AL85214511261456316638277339", true) | ||
validatePattern(t, PatternIBAN, "fL20-ING-B0-00-12-34-567", false) | ||
validatePattern(t, PatternIBAN, "fX22YYY1234567890123", true) | ||
validatePattern(t, PatternIBAN, "[email protected]", false) | ||
} | ||
|
||
func TestCreditCardPattern(t *testing.T) { | ||
// Testnumbers from https://www.paypalobjects.com/en_AU/vhelp/paypalmanager_help/credit_card_numbers.htm | ||
validatePattern(t, PatternCCAmericanExpress, "378282246310005", true) | ||
validatePattern(t, PatternCCAmericanExpress, "371449635398431", true) | ||
validatePattern(t, PatternCCAmericanExpress, "378734493671000", true) | ||
|
||
validatePattern(t, PatternCCDinersClub, "30569309025904", true) | ||
validatePattern(t, PatternCCDinersClub, "38520000023237", true) | ||
|
||
validatePattern(t, PatternCCDiscover, "6011111111111117", true) | ||
validatePattern(t, PatternCCDiscover, "6011000990139424", true) | ||
|
||
validatePattern(t, PatternCCJCB, "3530111333300000", true) | ||
validatePattern(t, PatternCCJCB, "3566002020360505", true) | ||
|
||
validatePattern(t, PatternCCMasterCard, "5555555555554444", true) | ||
validatePattern(t, PatternCCMasterCard, "5105105105105100", true) | ||
|
||
validatePattern(t, PatternCCVisa, "4111111111111111", true) | ||
validatePattern(t, PatternCCVisa, "4012888888881881", true) | ||
validatePattern(t, PatternCCVisa, "4222222222222", true) | ||
} | ||
|
||
func TestPatternJWT(t *testing.T) { | ||
validatePattern(t, PatternJWT, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", true) | ||
validatePattern(t, PatternJWT, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI", true) | ||
} |
Oops, something went wrong.