Skip to content

Commit

Permalink
fix: consider only servlet URL when checking auth behind a proxy
Browse files Browse the repository at this point in the history
Don't consider the full "requestURI" as this might contain a prefix that a proxy sets.
The MockMVC unit tests don't set the servlet path properly though. If not behind a proxy, one can fall back to requestURI
  • Loading branch information
fengelniederhammer committed Dec 14, 2023
1 parent 0006ac2 commit 6b1767a
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ private class ProtectedDataAuthorizationFilter(
}

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

if (path == "/" || WHITELISTED_PATH_PREFIXES.any { path.startsWith(it) }) {
return AuthorizationResult.success()
Expand All @@ -116,7 +120,7 @@ private class ProtectedDataAuthorizationFilter(
val requestFields = request.getRequestFields()

val accessKey = requestFields[ACCESS_KEY_PROPERTY]?.textValue()
?: return AuthorizationResult.failure("An access key is required to access ${path}.")
?: return AuthorizationResult.failure("An access key is required to access $path.")

if (accessKeys.fullAccessKey == accessKey) {
return AuthorizationResult.success()
Expand All @@ -129,6 +133,6 @@ private class ProtectedDataAuthorizationFilter(
return AuthorizationResult.success()
}

return AuthorizationResult.failure("You are not authorized to access ${path}.")
return AuthorizationResult.failure("You are not authorized to access $path.")
}
}

0 comments on commit 6b1767a

Please sign in to comment.