-
Notifications
You must be signed in to change notification settings - Fork 0
fix(security): sanitize user-controlled path in tenant validation logs #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,7 +49,7 @@ public async Task InvokeAsync( | |||||||||||||||||||||
| // Check if path is excluded | ||||||||||||||||||||||
| if (IsExcludedPath(context.Request.Path)) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| _logger.LogDebug("Path {Path} is excluded from tenant validation", context.Request.Path); | ||||||||||||||||||||||
| _logger.LogDebug("Path {Path} is excluded from tenant validation", SanitizeForLog(context.Request.Path.Value)); | ||||||||||||||||||||||
| await _next(context); | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -65,7 +65,7 @@ public async Task InvokeAsync( | |||||||||||||||||||||
| _logger.LogWarning( | ||||||||||||||||||||||
| "Tenant validation failed: {Error}. Path: {Path}", | ||||||||||||||||||||||
| validationResult.Error.Message, | ||||||||||||||||||||||
| context.Request.Path); | ||||||||||||||||||||||
| SanitizeForLog(context.Request.Path.Value)); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await WriteErrorResponse(context, validationResult.Error.Message, StatusCodes.Status403Forbidden); | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
|
|
@@ -80,7 +80,7 @@ public async Task InvokeAsync( | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (tenantResult.IsFailure || tenantResult.Value is null) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| _logger.LogWarning("Tenant {TenantId} not found or inactive", tenantId); | ||||||||||||||||||||||
| _logger.LogWarning("Tenant {TenantId} not found or inactive", SanitizeForLog(tenantId)); | ||||||||||||||||||||||
| await WriteErrorResponse( | ||||||||||||||||||||||
| context, | ||||||||||||||||||||||
| TenantErrors.TenantNotFound(tenantId).Message, | ||||||||||||||||||||||
|
|
@@ -90,7 +90,7 @@ await WriteErrorResponse( | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (!tenantResult.Value.IsActive) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| _logger.LogWarning("Tenant {TenantId} is not active", tenantId); | ||||||||||||||||||||||
| _logger.LogWarning("Tenant {TenantId} is not active", SanitizeForLog(tenantId)); | ||||||||||||||||||||||
| await WriteErrorResponse( | ||||||||||||||||||||||
| context, | ||||||||||||||||||||||
| TenantErrors.TenantAccessDenied(tenantId).Message, | ||||||||||||||||||||||
|
|
@@ -205,6 +205,13 @@ private bool IsExcludedPath(PathString path) | |||||||||||||||||||||
| pathValue.StartsWith(excluded, StringComparison.OrdinalIgnoreCase)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private static string SanitizeForLog(string? value) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (string.IsNullOrEmpty(value)) | ||||||||||||||||||||||
| return string.Empty; | ||||||||||||||||||||||
| return value.Replace("\r", "_").Replace("\n", "_").Replace("\t", "_"); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| return value.Replace("\r", "_").Replace("\n", "_").Replace("\t", "_"); | |
| var sanitized = new System.Text.StringBuilder(value.Length); | |
| foreach (var ch in value) | |
| { | |
| sanitized.Append(char.IsControl(ch) || ch == '\u2028' || ch == '\u2029' ? '_' : ch); | |
| } | |
| return sanitized.ToString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validationResult.Error.Messagecan contain user-controlled tenant identifiers (e.g., mismatch errors include header/subdomain/JWT values) and is currently logged without sanitization. To fully mitigate CWE-117/log-forging here, sanitize the{Error}value as well (or log a stable error code and keep the raw message only for the response).