|
| 1 | +package masker |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMask(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + want string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "AWS Access Key", |
| 18 | + input: "aws_access_key_id = AKIAIOSFODNN7EXAMPLE", |
| 19 | + want: "aws_access_key_id = " + strings.Repeat("*", 20), |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "GitHub Personal Access Token", |
| 23 | + input: "token: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 24 | + want: "token: " + strings.Repeat("*", 40), |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "GitHub OAuth Token", |
| 28 | + input: "GITHUB_TOKEN=gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 29 | + want: "GITHUB_TOKEN=" + strings.Repeat("*", 40), |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "GitLab Personal Access Token", |
| 33 | + input: "GITLAB_TOKEN=glpat-xxxxxxxxxxxxxxxxxxxx", |
| 34 | + want: "GITLAB_TOKEN=" + strings.Repeat("*", 26), |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Slack Bot Token", |
| 38 | + input: "SLACK_TOKEN=xoxb-123456789-abcdefgh", |
| 39 | + want: "SLACK_TOKEN=" + strings.Repeat("*", 23), |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "JWT Token", |
| 43 | + input: "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U", |
| 44 | + want: "Authorization: Bearer " + strings.Repeat("*", 108), |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "RSA Private Key Header", |
| 48 | + input: "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA...", |
| 49 | + want: strings.Repeat("*", 31) + "\nMIIEpAIBAAKCAQEA...", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "No sensitive data", |
| 53 | + input: "const message = 'Hello World'", |
| 54 | + want: "const message = 'Hello World'", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Multiple secrets", |
| 58 | + input: "GITHUB=ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\nAWS=AKIAIOSFODNN7EXAMPLE", |
| 59 | + want: "GITHUB=" + strings.Repeat("*", 40) + "\nAWS=" + strings.Repeat("*", 20), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Empty string", |
| 63 | + input: "", |
| 64 | + want: "", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Preserves length", |
| 68 | + input: "KEY=AKIAIOSFODNN7EXAMPLE", |
| 69 | + want: "KEY=" + strings.Repeat("*", 20), |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + got := Mask(tt.input) |
| 76 | + assert.Equal(t, tt.want, got) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments