Skip to content

Commit

Permalink
Verifiser orgnr, siden de er user-controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbb committed Oct 12, 2023
1 parent be2296c commit ec09ff7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class KontaktinfoController(
private val eregService: EregService,
private val kontaktinfoClient: KontaktinfoClient,
) {

@PostMapping("/api/kontaktinfo/v1")
fun getKontaktinfo(@RequestBody requestBody: KontaktinfoRequest): KontaktinfoResponse {
val orgnrUnderenhet = requestBody.virksomhetsnummer
Expand Down Expand Up @@ -48,7 +47,15 @@ class KontaktinfoController(

class KontaktinfoRequest(
val virksomhetsnummer: String,
)
) {
init {
require(virksomhetsnummer.matches(orgnrRegex))
}

companion object {
private val orgnrRegex = Regex("^[0-9]{9}$")
}
}

@Suppress("unused") // DTO
class Kontaktinfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class KontaktinfoControllerSerdeTest {
fun superflousJsonFields() {
/* spring's objectmapper godtar ekstra felter. */
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": "12341234", "garbage": 2 }"""
content = """{ "virksomhetsnummer": "123412341", "garbage": 2 }"""
).andExpect {
status { isOk() }
}
Expand All @@ -88,7 +88,7 @@ class KontaktinfoControllerSerdeTest {
@Test
fun disallowAcceptXML() {
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": "12341234" }""",
content = """{ "virksomhetsnummer": "123412341" }""",
accept = MediaType.APPLICATION_XML
).andExpect {
status { is4xxClientError() }
Expand Down

0 comments on commit ec09ff7

Please sign in to comment.