@@ -411,59 +411,64 @@ func TestBadDir(t *testing.T) {
411
411
}
412
412
413
413
func TestRedactTokens (t * testing.T ) {
414
+ // https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#githubs-token-formats
415
+ tokensToRedacted := map [string ]string {
416
+ "ghp_1234567890" : "ghp_**********" ,
417
+ "github_pat_1234567890" : "github_pat_**********" ,
418
+ "gho_1234567890" : "gho_**********" ,
419
+ "ghu_1234567890" : "ghu_**********" ,
420
+ "ghs_1234567890" : "ghs_**********" ,
421
+ "ghr_1234567890" : "ghr_**********" ,
422
+ }
423
+
414
424
tests := []struct {
415
425
name string
416
426
input string
417
427
want string
418
428
}{
419
429
{
420
- name : "no token" ,
421
- input : "no token" ,
422
- want : "no token" ,
423
- },
424
- {
425
- name : "oauth token at start" ,
426
- input : "gho_1234567890" ,
427
- want : "gho_**********" ,
428
- },
429
- {
430
- name : "oauth token at end" ,
431
- input : "this is a gho_1234567890" ,
432
- want : "this is a gho_**********" ,
430
+ name : "token at start" ,
431
+ input : "%s" ,
432
+ want : "%s" ,
433
433
},
434
434
{
435
- name : "oauth token in middle " ,
436
- input : "this is a gho_1234567890 and this is redacted " ,
437
- want : "this is a gho_********** and this is redacted " ,
435
+ name : "token at end " ,
436
+ input : "this is a %s " ,
437
+ want : "this is a %s " ,
438
438
},
439
439
{
440
- name : "oauth token at start with newline " ,
441
- input : "gho_1234567890 \n " ,
442
- want : "gho_********** \n " ,
440
+ name : "token in middle " ,
441
+ input : "this is a %s and this is redacted " ,
442
+ want : "this is a %s and this is redacted " ,
443
443
},
444
444
{
445
- name : "oauth token with no preceding word boundary " ,
446
- input : "foogho_1234567890 " ,
447
- want : "foogho_********** " ,
445
+ name : "token at start with newline " ,
446
+ input : "%s \n " ,
447
+ want : "%s \n " ,
448
448
},
449
449
{
450
- name : "oauth token no word boundary (extends redaction into next word) " ,
451
- input : "gho_1234567890x " ,
452
- want : "gho_*********** " ,
450
+ name : "token with no preceding word boundary" ,
451
+ input : "foo%s " ,
452
+ want : "foo%s " ,
453
453
},
454
454
{
455
- name : "pat token" ,
456
- input : "ghp_1234567890 " ,
457
- want : "ghp_********* *" ,
455
+ name : "token no word boundary (extends redaction into next word) " ,
456
+ input : "%sx " ,
457
+ want : "%s *" ,
458
458
},
459
459
}
460
460
461
- for _ , test := range tests {
462
- t .Run (test .name , func (t * testing.T ) {
463
- if got := redactTokens (test .input ); got != test .want {
464
- t .Fatalf ("redactTokens(%q) == %q, want %q" , test .input , got , test .want )
465
- }
466
- })
461
+ for token , redacted := range tokensToRedacted {
462
+ for _ , test := range tests {
463
+ t .Run (fmt .Sprintf ("%s: %s" , token , test .name ), func (t * testing.T ) {
464
+ unredacted := fmt .Sprintf (test .input , token )
465
+ expected := fmt .Sprintf (test .want , redacted )
466
+
467
+ if got := redactTokens (unredacted ); got != expected {
468
+ t .Fatalf ("redactTokens(%q) == %q, want %q" , unredacted , got , expected )
469
+ }
470
+ })
471
+ }
467
472
}
468
473
}
469
474
0 commit comments