move path check to app access manager
This commit is contained in:
parent
dc0a59fcf2
commit
ea14212337
@ -2,6 +2,7 @@ package com.restapi
|
|||||||
|
|
||||||
import io.javalin.http.Context
|
import io.javalin.http.Context
|
||||||
import io.javalin.http.Handler
|
import io.javalin.http.Handler
|
||||||
|
import io.javalin.http.HttpStatus
|
||||||
import io.javalin.security.AccessManager
|
import io.javalin.security.AccessManager
|
||||||
import io.javalin.security.RouteRole
|
import io.javalin.security.RouteRole
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
@ -10,6 +11,12 @@ class AppAccessManager : AccessManager {
|
|||||||
private val logger = LoggerFactory.getLogger("Access")
|
private val logger = LoggerFactory.getLogger("Access")
|
||||||
override fun manage(handler: Handler, ctx: Context, routeRoles: Set<RouteRole>) {
|
override fun manage(handler: Handler, ctx: Context, routeRoles: Set<RouteRole>) {
|
||||||
logger.warn("access {}, {}", ctx.pathParamMap(), routeRoles)
|
logger.warn("access {}, {}", ctx.pathParamMap(), routeRoles)
|
||||||
|
val regex = Regex("^[a-zA-Z0-9\\-_\\.]+$")
|
||||||
|
|
||||||
|
if(ctx.pathParamMap().values.count { !regex.matches(it) } > 0){
|
||||||
|
ctx.status(HttpStatus.FORBIDDEN).result("invalid request")
|
||||||
|
} else {
|
||||||
handler.handle(ctx)
|
handler.handle(ctx)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -103,17 +103,6 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
NaiveRateLimit.requestPerTimeUnit(ctx, appConfig.rateLimit().getOrDefault(30), TimeUnit.MINUTES) // throws if rate limit is exceeded
|
NaiveRateLimit.requestPerTimeUnit(ctx, appConfig.rateLimit().getOrDefault(30), TimeUnit.MINUTES) // throws if rate limit is exceeded
|
||||||
|
|
||||||
//allow only alpha, numeric, hypen, underscore, dot in paths
|
|
||||||
val regex = Regex("^[a-zA-Z0-9\\-_\\.]+$")
|
|
||||||
|
|
||||||
ctx.path().split("/")
|
|
||||||
.dropWhile { it.isEmpty() }
|
|
||||||
.forEach {
|
|
||||||
if (!it.matches(regex)) {
|
|
||||||
throw IllegalArgumentException()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val authToken = ctx.header("Authorization")?.replace("Bearer ", "")
|
val authToken = ctx.header("Authorization")?.replace("Bearer ", "")
|
||||||
?.replace("Bearer: ", "")
|
?.replace("Bearer: ", "")
|
||||||
?.trim() ?: throw UnauthorizedResponse()
|
?.trim() ?: throw UnauthorizedResponse()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user