refactor(web): mask subtoken credentials in request audit path logs

This commit is contained in:
2026-02-25 14:48:02 +08:00
parent ab6d1204e6
commit d79ff57b89

View File

@@ -21,6 +21,16 @@ import org.slf4j.LoggerFactory
import java.io.File import java.io.File
private val requestLogger = LoggerFactory.getLogger("work.slhaf.hub.RequestAudit") private val requestLogger = LoggerFactory.getLogger("work.slhaf.hub.RequestAudit")
private val subTokenPathRegex = Regex("^/u/([^/]+)/")
private fun sanitizeRequestPath(path: String): String {
val match = subTokenPathRegex.find(path) ?: return path
val credential = match.groupValues[1]
val at = credential.indexOf('@')
if (at <= 0 || at == credential.lastIndex) return path
val nameOnly = credential.substring(0, at)
return path.replaceFirst("/u/$credential/", "/u/$nameOnly@***/")
}
private suspend inline fun withRequestAudit( private suspend inline fun withRequestAudit(
call: ApplicationCall, call: ApplicationCall,
@@ -41,13 +51,14 @@ private suspend inline fun withRequestAudit(
val tokenType = auth?.type?.name?.lowercase() ?: "none" val tokenType = auth?.type?.name?.lowercase() ?: "none"
val subToken = auth?.subTokenName ?: "-" val subToken = auth?.subTokenName ?: "-"
val script = call.parameters["script"] ?: "-" val script = call.parameters["script"] ?: "-"
val sanitizedPath = sanitizeRequestPath(call.request.path())
val status = call.response.status()?.value ?: if (thrown == null) 200 else 500 val status = call.response.status()?.value ?: if (thrown == null) 200 else 500
if (thrown == null) { if (thrown == null) {
requestLogger.info( requestLogger.info(
"endpoint={} method={} path={} status={} durationMs={} tokenType={} subToken={} script={}", "endpoint={} method={} path={} status={} durationMs={} tokenType={} subToken={} script={}",
endpoint, endpoint,
call.request.httpMethod.value, call.request.httpMethod.value,
call.request.path(), sanitizedPath,
status, status,
durationMs, durationMs,
tokenType, tokenType,
@@ -59,7 +70,7 @@ private suspend inline fun withRequestAudit(
"endpoint={} method={} path={} status={} durationMs={} tokenType={} subToken={} script={} error={}", "endpoint={} method={} path={} status={} durationMs={} tokenType={} subToken={} script={} error={}",
endpoint, endpoint,
call.request.httpMethod.value, call.request.httpMethod.value,
call.request.path(), sanitizedPath,
status, status,
durationMs, durationMs,
tokenType, tokenType,