···
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
-
os.Setenv("PDS_JWT_SECRET", secret)
-
os.Setenv("HS256_ISSUERS", issuer)
-
os.Unsetenv("PDS_JWT_SECRET")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
tokenString := createHS256Token(t, "did:plc:test123", issuer, secret, 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
-
os.Setenv("PDS_JWT_SECRET", "correct-secret")
-
os.Setenv("HS256_ISSUERS", issuer)
-
os.Unsetenv("PDS_JWT_SECRET")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
// Create token with wrong secret
tokenString := createHS256Token(t, "did:plc:test123", issuer, "wrong-secret", 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
-
os.Unsetenv("PDS_JWT_SECRET") // Ensure secret is not set
-
os.Setenv("HS256_ISSUERS", issuer)
-
os.Unsetenv("HS256_ISSUERS")
-
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()
-
os.Setenv("PDS_JWT_SECRET", "some-secret")
-
os.Setenv("HS256_ISSUERS", "https://trusted.example.com") // Different from token issuer
-
os.Unsetenv("PDS_JWT_SECRET")
-
os.Unsetenv("HS256_ISSUERS")
-
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()
-
os.Setenv("PDS_JWT_SECRET", "some-secret")
-
os.Unsetenv("HS256_ISSUERS") // Empty whitelist
-
os.Unsetenv("PDS_JWT_SECRET")
-
ResetJWTConfigForTesting()
tokenString := createHS256Token(t, "did:plc:test123", "https://any-pds.example.com", "some-secret", 1*time.Hour)
···
issuer := "https://pds.coves.social"
ResetJWTConfigForTesting()
-
os.Setenv("PDS_JWT_SECRET", "test-secret")
-
os.Setenv("HS256_ISSUERS", issuer)
-
os.Unsetenv("PDS_JWT_SECRET")
-
os.Unsetenv("HS256_ISSUERS")
-
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()
-
os.Setenv("HS256_ISSUERS", "https://pds1.example.com,https://pds2.example.com")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
if !isHS256IssuerWhitelisted("https://pds1.example.com") {
t.Error("Expected pds1 to be whitelisted")
···
func TestIsHS256IssuerWhitelisted_NotWhitelisted(t *testing.T) {
ResetJWTConfigForTesting()
-
os.Setenv("HS256_ISSUERS", "https://pds1.example.com")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
if isHS256IssuerWhitelisted("https://attacker.example.com") {
t.Error("Expected non-whitelisted issuer to return false")
···
func TestIsHS256IssuerWhitelisted_EmptyWhitelist(t *testing.T) {
ResetJWTConfigForTesting()
-
os.Unsetenv("HS256_ISSUERS")
-
defer ResetJWTConfigForTesting()
if isHS256IssuerWhitelisted("https://any.example.com") {
t.Error("Expected false when whitelist is empty (safe default)")
···
func TestIsHS256IssuerWhitelisted_WhitespaceHandling(t *testing.T) {
ResetJWTConfigForTesting()
-
os.Setenv("HS256_ISSUERS", " https://pds1.example.com , https://pds2.example.com ")
-
os.Unsetenv("HS256_ISSUERS")
-
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()
-
os.Setenv("HS256_ISSUERS", "https://whitelisted.example.com")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
···
func TestShouldUseHS256_WithoutKid_WhitelistedIssuer(t *testing.T) {
ResetJWTConfigForTesting()
-
os.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()
···
func TestShouldUseHS256_WithoutKid_NotWhitelisted(t *testing.T) {
ResetJWTConfigForTesting()
-
os.Setenv("HS256_ISSUERS", "https://my-pds.example.com")
-
os.Unsetenv("HS256_ISSUERS")
-
ResetJWTConfigForTesting()