Skip to content

Commit d6528fe

Browse files
committed
Fix #34904: validate realmId
1 parent 581f5c8 commit d6528fe

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

  • src/main/kotlin/eu/openanalytics/shinyproxyoperator/impl/source

src/main/kotlin/eu/openanalytics/shinyproxyoperator/impl/source/FileSource.kt

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,18 @@ class FileSource(
9696
if (spec.get("proxy")?.get("realm-id")?.isTextual != true) {
9797
throw IllegalStateException("No or invalid realm-id")
9898
}
99-
val name = spec.get("proxy").get("realm-id").textValue().lowercase()
99+
val name = spec.get("proxy").get("realm-id").textValue()
100+
validateRealmId(name)
100101
val shinyProxy = ShinyProxy(spec, name, "default")
101102
val realmId = shinyProxy.realmId
102103

103104
// check for duplicate realms in files
104105
if (nameToFile.containsKey(realmId)) {
105-
logger.warn { "Found duplicate realmId: '${shinyProxy.realmId}' in file: '${file.name}', already defined in '${nameToFile[realmId]}'" }
106-
hasInputError = true
107-
continue
106+
throw IllegalStateException("Found duplicate realmId: '${shinyProxy.realmId}' in file: '${file.name}', already defined in '${nameToFile[realmId]}'")
108107
}
109108
nameToFile[realmId] = file.name
110109

111-
if (!checkDuplicateUrl(urlToFile, shinyProxy, file.name)) {
112-
hasInputError = true
113-
continue
114-
}
110+
checkDuplicateUrl(urlToFile, shinyProxy, file.name)
115111

116112
val existingShinyProxy = shinyProxies[shinyProxy.realmId]
117113
if (existingShinyProxy == null) {
@@ -143,22 +139,19 @@ class FileSource(
143139
}
144140
}
145141

146-
private fun checkDuplicateUrl(urlToFile: HashMap<Pair<String, String>, String>, shinyProxy: ShinyProxy, fileName: String): Boolean {
142+
private fun checkDuplicateUrl(urlToFile: HashMap<Pair<String, String>, String>, shinyProxy: ShinyProxy, fileName: String) {
147143
val baseUrl = Pair(shinyProxy.fqdn, shinyProxy.subPath)
148144
if (urlToFile.containsKey(baseUrl)) {
149-
logger.warn { "Found multiple ShinyProxy resources with the same URL, fqdn: '${shinyProxy.fqdn}', path: '${shinyProxy.subPath}', realm: '${shinyProxy.realmId}' in file: '${fileName}', already defined in '${urlToFile[baseUrl]}'" }
150-
return false
145+
throw IllegalStateException("Found multiple ShinyProxy resources with the same URL, fqdn: '${shinyProxy.fqdn}', path: '${shinyProxy.subPath}', realm: '${shinyProxy.realmId}' in file: '${fileName}', already defined in '${urlToFile[baseUrl]}'")
151146
}
152147
urlToFile[baseUrl] = fileName
153148
for (additionalFqdn in shinyProxy.additionalFqdns) {
154149
val url = Pair(additionalFqdn, shinyProxy.subPath)
155150
if (urlToFile.containsKey(url)) {
156-
logger.warn { "Found multiple ShinyProxy resources with the same (additional) URL, additional fqdn: '${additionalFqdn}', path: '${shinyProxy.subPath}', realm: '${shinyProxy.realmId}' in file: '${fileName}', already defined in '${urlToFile[url]}'" }
157-
return false
151+
throw IllegalStateException("Found multiple ShinyProxy resources with the same (additional) URL, additional fqdn: '${additionalFqdn}', path: '${shinyProxy.subPath}', realm: '${shinyProxy.realmId}' in file: '${fileName}', already defined in '${urlToFile[url]}'")
158152
}
159-
urlToFile[url] =fileName
153+
urlToFile[url] = fileName
160154
}
161-
return true
162155
}
163156

164157
private suspend fun checkForDeleted(discoveredShinyProxies: Collection<String>) {
@@ -174,6 +167,25 @@ class FileSource(
174167
}
175168
}
176169

170+
private fun validateRealmId(realmId: String) {
171+
if (realmId.isBlank()) {
172+
throw IllegalArgumentException("RealmId: '$realmId' is invalid: should not be blank")
173+
}
174+
if (realmId.length > 63) {
175+
throw IllegalArgumentException("RealmId: '$realmId' is invalid: should contain at most 63 characters")
176+
}
177+
val regex = Regex("^[0-9a-z-]*$")
178+
if (!realmId.matches(regex)) {
179+
throw IllegalArgumentException("RealmId: '$realmId' is invalid: should contain only lowercase alphanumeric characters or '-'")
180+
}
181+
if (realmId.startsWith("-")) {
182+
throw IllegalArgumentException("RealmId: '$realmId' is invalid: should not start with '-'")
183+
}
184+
if (realmId.endsWith("-")) {
185+
throw IllegalArgumentException("RealmId: '$realmId' is invalid: should not end with '-'")
186+
}
187+
}
188+
177189
fun stop() {
178190
timer?.cancel()
179191
}

0 commit comments

Comments
 (0)