Skip to content

Commit ae38bfe

Browse files
authored
fix(server): return 404 if the significant version is invalid (#2307)
1 parent 1b5d615 commit ae38bfe

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

jit-binding-server/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
implementation(projects.sharedInternal)
3131

3232
testImplementation("io.ktor:ktor-server-test-host")
33+
testImplementation("io.ktor:ktor-client-mock:3.4.3")
3334
testImplementation("io.mockk:mockk:1.14.9")
3435
}
3536

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/RequestParsing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import io.ktor.http.Parameters
1313
fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest? {
1414
val owner = this["owner"]!!
1515
val nameAndPathAndSignificantVersionParts = this["name"]!!.split("___", limit = 2)
16-
val nameAndPath = nameAndPathAndSignificantVersionParts.first()
1716
val significantVersion =
1817
nameAndPathAndSignificantVersionParts
1918
.drop(1)
@@ -24,6 +23,7 @@ fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest? {
2423
.entries
2524
.find { "$it" == significantVersionString }
2625
} ?: FULL
26+
val nameAndPath = if (significantVersion == FULL) this["name"]!! else nameAndPathAndSignificantVersionParts.first()
2727
val nameAndPathParts = nameAndPath.split("__")
2828
val name = nameAndPathParts.first()
2929
val path =

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingAc
44
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
55
import io.github.typesafegithub.workflows.mavenbinding.TextArtifact
66
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
7+
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
78
import io.kotest.core.spec.style.FunSpec
89
import io.kotest.matchers.shouldBe
910
import io.ktor.client.HttpClient
11+
import io.ktor.client.engine.mock.MockEngine
12+
import io.ktor.client.engine.mock.respond
1013
import io.ktor.client.request.get
1114
import io.ktor.client.statement.bodyAsText
1215
import io.ktor.http.HttpStatusCode
@@ -146,5 +149,47 @@ class ArtifactRoutesTest :
146149
verify(exactly = 2) { mockBuildVersionArtifacts(any(), any()) }
147150
}
148151
}
152+
153+
test("when version significance is invalid") {
154+
testApplication {
155+
// Given
156+
application {
157+
appModule(
158+
buildVersionArtifacts = { bindingsServerRequest, _ ->
159+
buildVersionArtifacts(
160+
bindingsServerRequest,
161+
HttpClient(
162+
MockEngine { request ->
163+
when (request.url.toString()) {
164+
"https://raw.githubusercontent.com" +
165+
"/some-owner/some-action-act/v4/_weird/action.yml",
166+
"https://raw.githubusercontent.com" +
167+
"/some-owner/some-action-act/v4/_weird/action.yaml",
168+
-> {
169+
respond("Not found", status = HttpStatusCode.NotFound)
170+
}
171+
172+
else -> {
173+
error("An internal error occurred!")
174+
}
175+
}
176+
},
177+
),
178+
)
179+
},
180+
// Irrelevant for these tests.
181+
buildPackageArtifacts = { _, _, _, _ -> emptyMap() },
182+
getGithubAuthToken = { "" },
183+
)
184+
}
185+
186+
// When
187+
val response =
188+
client.get("some-owner/some-action-act___weird/v4/some-action-act___weird-v4.pom")
189+
190+
// Then
191+
response.status shouldBe HttpStatusCode.NotFound
192+
}
193+
}
149194
}
150195
})

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/RequestParsingTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class RequestParsingTest :
8383
name = "act",
8484
version = "irrelevant",
8585
significantVersion = FULL,
86-
path = null,
86+
path = "_weird",
8787
),
8888
)
8989
}

0 commit comments

Comments
 (0)