Skip to content

Commit

Permalink
fix: recognition of which endpoint was called to determine the filena…
Browse files Browse the repository at this point in the history
…me of a download
  • Loading branch information
fengelniederhammer committed Feb 2, 2024
1 parent b80ede9 commit 6c1674c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ private class ProtectedDataAuthorizationFilter(
}

override fun isAuthorizedForEndpoint(request: CachedBodyHttpServletRequest): AuthorizationResult {
val isOperatedBehindAProxy = !request.contextPath.isNullOrBlank()
val path = when {
isOperatedBehindAProxy -> request.servletPath
else -> request.requestURI
}
val path = request.getProxyAwarePath()

if (path == "/" || WHITELISTED_PATH_PREFIXES.any { path.startsWith(it) }) {
return AuthorizationResult.success()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class DownloadAsFileFilter(private val objectMapper: ObjectMapper) : OncePerRequ

val downloadAsFile = reReadableRequest.getRequestFields()[DOWNLOAD_AS_FILE_PROPERTY]?.asBoolean()
if (downloadAsFile == true) {
val filename = getFilename(request)
val filename = getFilename(reReadableRequest)
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=$filename")
}
filterChain.doFilter(reReadableRequest, response)
}

private fun getFilename(request: HttpServletRequest): String {
val matchingRoute = SampleRoute.entries.find { request.requestURI.startsWith("/sample${it.pathSegment}") }
private fun getFilename(request: CachedBodyHttpServletRequest): String {
val matchingRoute =
SampleRoute.entries.find { request.getProxyAwarePath().startsWith("/sample${it.pathSegment}") }
val dataName = matchingRoute?.pathSegment?.trim('/') ?: "data"

val fileEnding = when (request.getHeader("Accept")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class CachedBodyHttpServletRequest(request: HttpServletRequest, val objectMapper
}
}

fun getProxyAwarePath(): String {
val isOperatedBehindAProxy = !contextPath.isNullOrBlank()
return when {
isOperatedBehindAProxy -> servletPath
else -> requestURI
}
}

fun getRequestFields(): Map<String, JsonNode> {
if (parameterNames.hasMoreElements()) {
return parameterMap.mapValues { (_, value) -> TextNode(value.joinToString()) }
Expand Down

0 comments on commit 6c1674c

Please sign in to comment.