Skip to content

Commit fd6a9a5

Browse files
Merge pull request #135 from leanix/feature/CID-4118/Validate-webhook-signature-before-removing-it
CID-4118: Validate webhook signature before processing
2 parents 08d3313 + e9ded34 commit fd6a9a5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/main/kotlin/net/leanix/githubagent/services/GitHubWebhookService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class GitHubWebhookService(
5656
throw WebhookSecretNotSetException()
5757
}
5858
if (gitHubEnterpriseProperties.webhookSecret.isNotBlank() && signature256 != null) {
59+
if (!signature256.startsWith("sha256=")) {
60+
logger.error("Invalid signature format, expected 'sha256=' prefix")
61+
throw InvalidEventSignatureException()
62+
}
5963
val hashedSecret = hmacSHA256(gitHubEnterpriseProperties.webhookSecret, payload)
6064
val isEqual = timingSafeEqual(signature256.removePrefix("sha256="), hashedSecret)
6165
if (!isEqual) throw InvalidEventSignatureException()

src/test/kotlin/net/leanix/githubagent/services/GitHubWebhookServiceTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ class GitHubWebhookServiceTest {
8989

9090
verify(exactly = 0) { webhookEventService.consumeWebhookEvent(any(), any()) }
9191
}
92+
93+
@Test
94+
fun `should throw InvalidEventSignatureException when signature does not start with sha256=`() {
95+
every { gitHubEnterpriseProperties.baseUrl } returns "known.host"
96+
every { gitHubEnterpriseProperties.webhookSecret } returns "secret"
97+
98+
assertThrows<InvalidEventSignatureException> {
99+
gitHubWebhookService.handleWebhookEvent("PUSH", "known.host", "invalid_signature", "{}")
100+
}
101+
}
92102
}

0 commit comments

Comments
 (0)