-
Notifications
You must be signed in to change notification settings - Fork 5
notification-service: stop cross-user overwrite of notification preferences (IDOR) #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
1
commit into
main
Choose a base branch
from
devin/1789396479-preferences-idor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...cation-service/src/test/kotlin/com/otterworks/notification/routes/PreferencesRouteTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package com.otterworks.notification.routes | ||
|
|
||
| import com.otterworks.notification.model.DeliveryChannel | ||
| import com.otterworks.notification.service.NotificationService | ||
| import com.otterworks.notification.websocket.WebSocketManager | ||
| import io.ktor.client.request.header | ||
| import io.ktor.client.request.put | ||
| import io.ktor.client.request.setBody | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.HttpStatusCode | ||
| import io.ktor.http.contentType | ||
| import io.ktor.serialization.kotlinx.json.json | ||
| import io.ktor.server.application.install | ||
| import io.ktor.server.plugins.contentnegotiation.ContentNegotiation | ||
| import io.ktor.server.testing.ApplicationTestBuilder | ||
| import io.ktor.server.testing.testApplication | ||
| import io.ktor.server.websocket.WebSockets | ||
| import io.micrometer.prometheus.PrometheusConfig | ||
| import io.micrometer.prometheus.PrometheusMeterRegistry | ||
| import io.mockk.coVerify | ||
| import io.mockk.mockk | ||
| import kotlinx.serialization.json.Json | ||
| import org.koin.core.context.stopKoin | ||
| import org.koin.dsl.module | ||
| import org.koin.ktor.plugin.Koin | ||
| import kotlin.test.AfterTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class PreferencesRouteTest { | ||
|
|
||
| private val notificationService = mockk<NotificationService>(relaxed = true) | ||
| private val webSocketManager = mockk<WebSocketManager>(relaxed = true) | ||
|
|
||
| @AfterTest | ||
| fun tearDown() { | ||
| stopKoin() | ||
| } | ||
|
|
||
| private fun ApplicationTestBuilder.setupApp() { | ||
| application { | ||
| install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) } | ||
| install(WebSockets) | ||
| install(Koin) { | ||
| modules( | ||
| module { | ||
| single { notificationService } | ||
| single { webSocketManager } | ||
| } | ||
| ) | ||
| } | ||
| configureRouting(PrometheusMeterRegistry(PrometheusConfig.DEFAULT)) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `PUT preferences uses the authenticated X-User-ID, not the body userId`() = testApplication { | ||
| setupApp() | ||
|
|
||
| val response = client.put("/api/v1/preferences") { | ||
| header("X-User-ID", "alice") | ||
| contentType(ContentType.Application.Json) | ||
| setBody("""{"eventType":"file_shared","channels":["IN_APP"]}""") | ||
| } | ||
|
|
||
| assertEquals(HttpStatusCode.NoContent, response.status) | ||
| coVerify(exactly = 1) { notificationService.updatePreferences("alice", "file_shared", listOf(DeliveryChannel.IN_APP)) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `PUT preferences rejects a body userId that differs from the caller`() = testApplication { | ||
| setupApp() | ||
|
|
||
| val response = client.put("/api/v1/preferences") { | ||
| header("X-User-ID", "attacker") | ||
| contentType(ContentType.Application.Json) | ||
| setBody("""{"userId":"victim","eventType":"file_shared","channels":[]}""") | ||
| } | ||
|
|
||
| assertEquals(HttpStatusCode.Forbidden, response.status) | ||
| coVerify(exactly = 0) { notificationService.updatePreferences(any(), any(), any()) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `PUT preferences accepts a body userId that matches the caller`() = testApplication { | ||
| setupApp() | ||
|
|
||
| val response = client.put("/api/v1/preferences") { | ||
| header("X-User-ID", "alice") | ||
| contentType(ContentType.Application.Json) | ||
| setBody("""{"userId":"alice","eventType":"comment_added","channels":["EMAIL"]}""") | ||
| } | ||
|
|
||
| assertEquals(HttpStatusCode.NoContent, response.status) | ||
| coVerify(exactly = 1) { notificationService.updatePreferences("alice", "comment_added", listOf(DeliveryChannel.EMAIL)) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `PUT preferences without an authenticated identity is rejected`() = testApplication { | ||
| setupApp() | ||
|
|
||
| val response = client.put("/api/v1/preferences") { | ||
| contentType(ContentType.Application.Json) | ||
| setBody("""{"userId":"victim","eventType":"file_shared","channels":[]}""") | ||
| } | ||
|
|
||
| assertEquals(HttpStatusCode.Unauthorized, response.status) | ||
| coVerify(exactly = 0) { notificationService.updatePreferences(any(), any(), any()) } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟥 Direct requests forge preference ownership
A direct client can set
X-User-IDand overwrite that user's preferences. The public service ingress bypasses gateway JWT validation.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged, but this is the platform's existing trust model rather than something this PR introduces: every identity-scoped route in notification-service (GET /notifications, /unread-count, /read-all, GET /preferences) and the other services already trust the gateway-injected
X-User-ID, and the gateway (api-gateway/internal/proxy/router.go) overwrites that header from the validated JWT so a client cannot forge it through the supported entry point. The finding being fixed here (sfind-229db8f8) is specifically that this one write handler ignored that identity and trusted the request body instead, which was exploitable even through the gateway.Hardening the per-service ingress (network policy / JWT validation inside each service) is a separate, cross-service change and out of scope for this fix. Leaving this thread open for the reviewer to decide.