···
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
211
-
os.Setenv("PDS_JWT_SECRET", secret)
212
-
os.Setenv("HS256_ISSUERS", issuer)
214
-
os.Unsetenv("PDS_JWT_SECRET")
215
-
os.Unsetenv("HS256_ISSUERS")
216
-
ResetJWTConfigForTesting()
210
+
t.Setenv("PDS_JWT_SECRET", secret)
211
+
t.Setenv("HS256_ISSUERS", issuer)
212
+
t.Cleanup(ResetJWTConfigForTesting)
tokenString := createHS256Token(t, "did:plc:test123", issuer, secret, 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
240
-
os.Setenv("PDS_JWT_SECRET", "correct-secret")
241
-
os.Setenv("HS256_ISSUERS", issuer)
243
-
os.Unsetenv("PDS_JWT_SECRET")
244
-
os.Unsetenv("HS256_ISSUERS")
245
-
ResetJWTConfigForTesting()
235
+
t.Setenv("PDS_JWT_SECRET", "correct-secret")
236
+
t.Setenv("HS256_ISSUERS", issuer)
237
+
t.Cleanup(ResetJWTConfigForTesting)
// Create token with wrong secret
tokenString := createHS256Token(t, "did:plc:test123", issuer, "wrong-secret", 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
263
-
os.Unsetenv("PDS_JWT_SECRET") // Ensure secret is not set
264
-
os.Setenv("HS256_ISSUERS", issuer)
266
-
os.Unsetenv("HS256_ISSUERS")
267
-
ResetJWTConfigForTesting()
254
+
t.Setenv("PDS_JWT_SECRET", "") // Ensure secret is not set (empty = not configured)
255
+
t.Setenv("HS256_ISSUERS", issuer)
256
+
t.Cleanup(ResetJWTConfigForTesting)
tokenString := createHS256Token(t, "did:plc:test123", issuer, "any-secret", 1*time.Hour)
···
// An attacker tries to use HS256 with an issuer that should use RS256/ES256
ResetJWTConfigForTesting()
289
-
os.Setenv("PDS_JWT_SECRET", "some-secret")
290
-
os.Setenv("HS256_ISSUERS", "https://trusted.example.com") // Different from token issuer
292
-
os.Unsetenv("PDS_JWT_SECRET")
293
-
os.Unsetenv("HS256_ISSUERS")
294
-
ResetJWTConfigForTesting()
277
+
t.Setenv("PDS_JWT_SECRET", "some-secret")
278
+
t.Setenv("HS256_ISSUERS", "https://trusted.example.com") // Different from token issuer
279
+
t.Cleanup(ResetJWTConfigForTesting)
// Create HS256 token with non-whitelisted issuer (simulating attack)
tokenString := createHS256Token(t, "did:plc:attacker", "https://victim-pds.example.com", "some-secret", 1*time.Hour)
···
// SECURITY TEST: When no issuers are whitelisted for HS256, all HS256 tokens should be rejected
ResetJWTConfigForTesting()
314
-
os.Setenv("PDS_JWT_SECRET", "some-secret")
315
-
os.Unsetenv("HS256_ISSUERS") // Empty whitelist
317
-
os.Unsetenv("PDS_JWT_SECRET")
318
-
ResetJWTConfigForTesting()
298
+
t.Setenv("PDS_JWT_SECRET", "some-secret")
299
+
t.Setenv("HS256_ISSUERS", "") // Empty whitelist
300
+
t.Cleanup(ResetJWTConfigForTesting)
tokenString := createHS256Token(t, "did:plc:test123", "https://any-pds.example.com", "some-secret", 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
335
-
os.Setenv("PDS_JWT_SECRET", "test-secret")
336
-
os.Setenv("HS256_ISSUERS", issuer)
338
-
os.Unsetenv("PDS_JWT_SECRET")
339
-
os.Unsetenv("HS256_ISSUERS")
340
-
ResetJWTConfigForTesting()
316
+
t.Setenv("PDS_JWT_SECRET", "test-secret")
317
+
t.Setenv("HS256_ISSUERS", issuer)
318
+
t.Cleanup(ResetJWTConfigForTesting)
// Create RS256-signed token (can't actually sign without RSA key, but we can test the header check)
···
func TestIsHS256IssuerWhitelisted_Whitelisted(t *testing.T) {
ResetJWTConfigForTesting()
417
-
os.Setenv("HS256_ISSUERS", "https://pds1.example.com,https://pds2.example.com")
419
-
os.Unsetenv("HS256_ISSUERS")
420
-
ResetJWTConfigForTesting()
394
+
t.Setenv("HS256_ISSUERS", "https://pds1.example.com,https://pds2.example.com")
395
+
t.Cleanup(ResetJWTConfigForTesting)
if !isHS256IssuerWhitelisted("https://pds1.example.com") {
t.Error("Expected pds1 to be whitelisted")
···
func TestIsHS256IssuerWhitelisted_NotWhitelisted(t *testing.T) {
ResetJWTConfigForTesting()
433
-
os.Setenv("HS256_ISSUERS", "https://pds1.example.com")
435
-
os.Unsetenv("HS256_ISSUERS")
436
-
ResetJWTConfigForTesting()
407
+
t.Setenv("HS256_ISSUERS", "https://pds1.example.com")
408
+
t.Cleanup(ResetJWTConfigForTesting)
if isHS256IssuerWhitelisted("https://attacker.example.com") {
t.Error("Expected non-whitelisted issuer to return false")
···
func TestIsHS256IssuerWhitelisted_EmptyWhitelist(t *testing.T) {
ResetJWTConfigForTesting()
446
-
os.Unsetenv("HS256_ISSUERS")
447
-
defer ResetJWTConfigForTesting()
417
+
t.Setenv("HS256_ISSUERS", "") // Empty whitelist
418
+
t.Cleanup(ResetJWTConfigForTesting)
if isHS256IssuerWhitelisted("https://any.example.com") {
t.Error("Expected false when whitelist is empty (safe default)")
···
func TestIsHS256IssuerWhitelisted_WhitespaceHandling(t *testing.T) {
ResetJWTConfigForTesting()
456
-
os.Setenv("HS256_ISSUERS", " https://pds1.example.com , https://pds2.example.com ")
458
-
os.Unsetenv("HS256_ISSUERS")
459
-
ResetJWTConfigForTesting()
427
+
t.Setenv("HS256_ISSUERS", " https://pds1.example.com , https://pds2.example.com ")
428
+
t.Cleanup(ResetJWTConfigForTesting)
if !isHS256IssuerWhitelisted("https://pds1.example.com") {
t.Error("Expected whitespace-trimmed issuer to be whitelisted")
···
func TestShouldUseHS256_WithKid_AlwaysFalse(t *testing.T) {
// Tokens with kid should NEVER use HS256, regardless of issuer whitelist
ResetJWTConfigForTesting()
472
-
os.Setenv("HS256_ISSUERS", "https://whitelisted.example.com")
474
-
os.Unsetenv("HS256_ISSUERS")
475
-
ResetJWTConfigForTesting()
440
+
t.Setenv("HS256_ISSUERS", "https://whitelisted.example.com")
441
+
t.Cleanup(ResetJWTConfigForTesting)
···
func TestShouldUseHS256_WithoutKid_WhitelistedIssuer(t *testing.T) {
ResetJWTConfigForTesting()
491
-
os.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
493
-
os.Unsetenv("HS256_ISSUERS")
494
-
ResetJWTConfigForTesting()
456
+
t.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
457
+
t.Cleanup(ResetJWTConfigForTesting)
···
func TestShouldUseHS256_WithoutKid_NotWhitelisted(t *testing.T) {
ResetJWTConfigForTesting()
509
-
os.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
511
-
os.Unsetenv("HS256_ISSUERS")
512
-
ResetJWTConfigForTesting()
471
+
t.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
472
+
t.Cleanup(ResetJWTConfigForTesting)