@@ -521,6 +521,22 @@ func TestHideConfigSecrets(t *testing.T) {
521521 }
522522}
523523
524+ func TestShowMarshalSecretValues (t * testing.T ) {
525+ MarshalSecretValue = true
526+ defer func () { MarshalSecretValue = false }()
527+
528+ c , err := LoadFile ("testdata/conf.good.yml" )
529+ if err != nil {
530+ t .Fatalf ("Error parsing %s: %s" , "testdata/conf.good.yml" , err )
531+ }
532+
533+ // String method must reveal authentication credentials.
534+ s := c .String ()
535+ if strings .Count (s , "<secret>" ) > 0 || ! strings .Contains (s , "mysecret" ) {
536+ t .Fatal ("config's String method must reveal authentication credentials when MarshalSecretValue = true." )
537+ }
538+ }
539+
524540func TestJSONMarshal (t * testing.T ) {
525541 c , err := LoadFile ("testdata/conf.good.yml" )
526542 if err != nil {
@@ -533,7 +549,7 @@ func TestJSONMarshal(t *testing.T) {
533549 }
534550}
535551
536- func TestJSONMarshalSecret (t * testing.T ) {
552+ func TestJSONMarshalHideSecret (t * testing.T ) {
537553 test := struct {
538554 S Secret
539555 }{
@@ -550,7 +566,24 @@ func TestJSONMarshalSecret(t *testing.T) {
550566 require .Equal (t , "{\" S\" :\" \\ u003csecret\\ u003e\" }" , string (c ), "Secret not properly elided." )
551567}
552568
553- func TestMarshalSecretURL (t * testing.T ) {
569+ func TestJSONMarshalShowSecret (t * testing.T ) {
570+ MarshalSecretValue = true
571+ defer func () { MarshalSecretValue = false }()
572+
573+ test := struct {
574+ S Secret
575+ }{
576+ S : Secret ("test" ),
577+ }
578+
579+ c , err := json .Marshal (test )
580+ if err != nil {
581+ t .Fatal (err )
582+ }
583+ require .Equal (t , "{\" S\" :\" test\" }" , string (c ), "config's String method must reveal authentication credentials when MarshalSecretValue = true." )
584+ }
585+
586+ func TestJSONMarshalHideSecretURL (t * testing.T ) {
554587 urlp , err := url .Parse ("http://example.com/" )
555588 if err != nil {
556589 t .Fatal (err )
@@ -584,6 +617,23 @@ func TestMarshalSecretURL(t *testing.T) {
584617 }
585618}
586619
620+ func TestJSONMarshalShowSecretURL (t * testing.T ) {
621+ MarshalSecretValue = true
622+ defer func () { MarshalSecretValue = false }()
623+
624+ urlp , err := url .Parse ("http://example.com/" )
625+ if err != nil {
626+ t .Fatal (err )
627+ }
628+ u := & SecretURL {urlp }
629+
630+ c , err := json .Marshal (u )
631+ if err != nil {
632+ t .Fatal (err )
633+ }
634+ require .Equal (t , "\" http://example.com/\" " , string (c ), "config's String method must reveal authentication credentials when MarshalSecretValue = true." )
635+ }
636+
587637func TestUnmarshalSecretURL (t * testing.T ) {
588638 b := []byte (`"http://example.com/se cret"` )
589639 var u SecretURL
@@ -611,6 +661,18 @@ func TestHideSecretURL(t *testing.T) {
611661 require .NotContains (t , err .Error (), "wrongurl" )
612662}
613663
664+ func TestShowMarshalSecretURL (t * testing.T ) {
665+ MarshalSecretValue = true
666+ defer func () { MarshalSecretValue = false }()
667+
668+ b := []byte (`"://wrongurl/"` )
669+ var u SecretURL
670+
671+ err := json .Unmarshal (b , & u )
672+ require .Error (t , err )
673+ require .Contains (t , err .Error (), "wrongurl" )
674+ }
675+
614676func TestMarshalURL (t * testing.T ) {
615677 for name , tc := range map [string ]struct {
616678 input * URL
0 commit comments