|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 OneLiteFeather |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.reposilite.console.infrastructure |
| 17 | + |
| 18 | +import com.reposilite.ReposiliteJournalist |
| 19 | +import com.reposilite.auth.specification.AuthenticationSpecification |
| 20 | +import com.reposilite.console.CommandExecutor |
| 21 | +import com.reposilite.console.ConsoleFacade |
| 22 | +import com.reposilite.journalist.backend.InMemoryLogger |
| 23 | +import com.reposilite.shared.ErrorResponse |
| 24 | +import com.reposilite.status.FailureFacade |
| 25 | +import com.reposilite.token.AccessTokenPermission.MANAGER |
| 26 | +import com.reposilite.token.AccessTokenType.TEMPORARY |
| 27 | +import com.reposilite.token.api.AccessTokenDto |
| 28 | +import com.reposilite.token.api.CreateAccessTokenRequest |
| 29 | +import net.onelitefeather.ingot.identity.login.LoginFacade |
| 30 | +import net.onelitefeather.ingot.identity.session.SessionCookies |
| 31 | +import org.assertj.core.api.Assertions.assertThat |
| 32 | +import org.junit.jupiter.api.AfterEach |
| 33 | +import org.junit.jupiter.api.BeforeEach |
| 34 | +import org.junit.jupiter.api.Test |
| 35 | +import panda.std.Result |
| 36 | +import panda.std.asSuccess |
| 37 | +import panda.std.reactive.Reference |
| 38 | +import java.io.ByteArrayInputStream |
| 39 | +import java.time.Clock |
| 40 | +import java.time.Instant |
| 41 | +import java.time.ZoneOffset |
| 42 | + |
| 43 | +/** |
| 44 | + * The hello of a browser (task 4.6): without a token it authenticates with the session cookie of the upgrade request, and the |
| 45 | + * `Origin` of that request has to name the host it reached. Driven through the connection seam, with no server and no wait. |
| 46 | + */ |
| 47 | +internal class ConsoleWebSocketCookieHelloTest : AuthenticationSpecification() { |
| 48 | + |
| 49 | + private val journalist = ReposiliteJournalist(InMemoryLogger(), 100, testEnv = true, noColor = true) |
| 50 | + private val sessionsSeen = mutableListOf<String>() |
| 51 | + private lateinit var handler: ConsoleWebSocketHandler |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + fun createHandler() { |
| 55 | + accessTokenFacade.createAccessToken( |
| 56 | + CreateAccessTokenRequest(TEMPORARY, "browser-admin", secret = "s3cret", permissions = setOf(MANAGER)) |
| 57 | + ) |
| 58 | + val executor = CommandExecutor( |
| 59 | + journalist = journalist, |
| 60 | + failureFacade = FailureFacade(InMemoryLogger(), Clock.fixed(Instant.EPOCH, ZoneOffset.UTC)), |
| 61 | + source = ByteArrayInputStream(ByteArray(0)), |
| 62 | + ) |
| 63 | + // A real session needs the login store, so a stand-in resolves the one cookie value the tests use. |
| 64 | + val authenticator = object : ConsoleAuthenticator( |
| 65 | + accessTokenFacade, |
| 66 | + authenticationFacade, |
| 67 | + LoginFacade(service = null, sessions = null, ownTokens = null), |
| 68 | + ) { |
| 69 | + override fun authenticateSession(sessionJwt: String, ip: String): Result<AccessTokenDto, ErrorResponse> { |
| 70 | + sessionsSeen += sessionJwt |
| 71 | + return accessTokenFacade.getAccessToken("browser-admin")!!.asSuccess() |
| 72 | + } |
| 73 | + } |
| 74 | + handler = ConsoleWebSocketHandler( |
| 75 | + journalist = journalist, |
| 76 | + consoleAuthenticator = authenticator, |
| 77 | + consoleFacade = ConsoleFacade(journalist, executor), |
| 78 | + forwardedIp = Reference.reference("X-Forwarded-For"), |
| 79 | + audit = {}, |
| 80 | + scheduler = FakeConsoleScheduler(), |
| 81 | + nodeId = "node-a", |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + @AfterEach |
| 86 | + fun shutDownJournalist() { |
| 87 | + journalist.shutdown() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `accepts a hello without a token when the cookie session and the origin name the host`() { |
| 92 | + // given: a browser that sends its session cookie and an Origin of the host it reached |
| 93 | + val connection = browser(origin = "https://repo.example.org", host = "repo.example.org") |
| 94 | + |
| 95 | + // when: it says hello without a token |
| 96 | + handler.handleMessage(connection, HELLO) |
| 97 | + |
| 98 | + // then: the session of the cookie was checked and the welcome follows |
| 99 | + assertThat(sessionsSeen).`as`("sessions checked").containsExactly("jwt-of-browser") |
| 100 | + assertThat(connection.json.first()).`as`("first frame").isInstanceOf(WelcomeFrame::class.java) |
| 101 | + assertThat(connection.closeCode).`as`("close code").isNull() |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `accepts an origin with a port that equals the host with its port`() { |
| 106 | + val connection = browser(origin = "http://localhost:8080", host = "localhost:8080") |
| 107 | + |
| 108 | + handler.handleMessage(connection, HELLO) |
| 109 | + |
| 110 | + assertThat(connection.json.first()).`as`("first frame").isInstanceOf(WelcomeFrame::class.java) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `closes a cookie hello whose origin names another site with 1008 and unauthorized`() { |
| 115 | + val connection = browser(origin = "https://evil.example.net", host = "repo.example.org") |
| 116 | + |
| 117 | + handler.handleMessage(connection, HELLO) |
| 118 | + |
| 119 | + assertThat((connection.json.single() as ErrorFrame).code).`as`("error code").isEqualTo("unauthorized") |
| 120 | + assertThat(connection.closeCode).`as`("close code").isEqualTo(1008) |
| 121 | + assertThat(sessionsSeen).`as`("sessions checked before the origin was refused").isEmpty() |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `closes a cookie hello without an origin with 1008`() { |
| 126 | + val connection = browser(origin = null, host = "repo.example.org") |
| 127 | + |
| 128 | + handler.handleMessage(connection, HELLO) |
| 129 | + |
| 130 | + assertThat(connection.closeCode).`as`("close code").isEqualTo(1008) |
| 131 | + assertThat(sessionsSeen).`as`("sessions checked").isEmpty() |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `closes a cookie hello whose origin is not a URL with 1008`() { |
| 136 | + val connection = browser(origin = "null", host = "repo.example.org") |
| 137 | + |
| 138 | + handler.handleMessage(connection, HELLO) |
| 139 | + |
| 140 | + assertThat(connection.closeCode).`as`("close code").isEqualTo(1008) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun `lets a token hello pass whatever its origin is`() { |
| 145 | + // given: a client with a token in the hello that happens to send a foreign Origin |
| 146 | + val connection = FakeConsoleConnection(origin = "https://evil.example.net", requestHost = "repo.example.org") |
| 147 | + |
| 148 | + handler.handleMessage(connection, "{\"type\":\"hello\",\"protocols\":[1],\"token\":\"browser-admin:s3cret\"}") |
| 149 | + |
| 150 | + // then: a page of another site cannot read the token, so the origin is not asked |
| 151 | + assertThat(connection.json.first()).`as`("first frame").isInstanceOf(WelcomeFrame::class.java) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `closes a hello without a token and without a cookie with 1008`() { |
| 156 | + val connection = FakeConsoleConnection(origin = "https://repo.example.org", requestHost = "repo.example.org") |
| 157 | + |
| 158 | + handler.handleMessage(connection, HELLO) |
| 159 | + |
| 160 | + assertThat((connection.json.single() as ErrorFrame).code).`as`("error code").isEqualTo("unauthorized") |
| 161 | + assertThat(connection.closeCode).`as`("close code").isEqualTo(1008) |
| 162 | + } |
| 163 | + |
| 164 | + private fun browser(origin: String?, host: String) = |
| 165 | + FakeConsoleConnection(origin = origin, requestHost = host, cookies = mapOf(SessionCookies.SESSION to "jwt-of-browser")) |
| 166 | + |
| 167 | + private companion object { |
| 168 | + const val HELLO = "{\"type\":\"hello\",\"protocols\":[1]}" |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments