Code quality & security audit: extract magic numbers, add structured audit report - #23
Conversation
… report Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
|
@copilot Please complete the inspection and fix of all items mentioned in the issue one by one. |
…from audit follow-up Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
Went through each open item from the audit and turned the previously-documented recommendations into actual fixes (commit 47ab6b5):
The one remaining open item — dashboard JWT stored in |
There was a problem hiding this comment.
Pull request overview
This PR implements outcomes from a requested code-quality & security audit by extracting a few repeated “magic numbers” into named constants, adding defense-in-depth HTTP security headers at the API layer, and documenting the audit results in a structured report.
Changes:
- Added
SecurityHeadersMiddlewareand wired it into the ASP.NET Core pipeline; added an integration test asserting the headers are present. - Extracted several magic numbers into named constants across API middleware, OTA update streaming, CLI banner delay, and
realpathtimeout. - Improved observability by adding debug logging in previously silent exception paths (AI SSE parsing, share-event JSON parsing,
realpathfallback) and addeddocs/CODE_AUDIT_REPORT.md.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/FortOS.Tests.Integration/Api/ApiGatewayTests.cs | Adds an integration test for security response headers. |
| src/FortOS.Modules.Update/Services/OtaUpdateService.cs | Extracts download streaming buffer size to a named constant. |
| src/FortOS.Modules.Share/Services/FilePathResolver.cs | Extracts realpath timeout, adds debug logging with log-forging sanitization helper. |
| src/FortOS.Cli/Program.cs | Extracts CLI banner delay to a named constant. |
| src/FortOS.Api/Services/AiAssistantService.cs | Adds optional logger injection and debug logging for skipped SSE JSON lines. |
| src/FortOS.Api/Program.cs | Registers the new security headers middleware in the request pipeline. |
| src/FortOS.Api/Middleware/SecurityHeadersMiddleware.cs | New middleware that attaches standard security headers to responses. |
| src/FortOS.Api/Middleware/IdempotencyMiddleware.cs | Extracts request-body copy buffer size to a named constant. |
| src/FortOS.Api/Grpc/ShareGrpcService.cs | Adds debug logging when share client event payload JSON parsing fails. |
| docs/CODE_AUDIT_REPORT.md | Adds the structured audit report documenting findings and recommendations. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| app.UseMiddleware<TraceIdMiddleware>(); | ||
| app.UseMiddleware<SecurityHeadersMiddleware>(); | ||
| app.UseMiddleware<ApiVersionCompatibilityMiddleware>(); |
| public async Task AnyResponse_IncludesSecurityHeaders() | ||
| { | ||
| using var factory = await ApiTestFactory.CreateAsync(nameof(AnyResponse_IncludesSecurityHeaders)); | ||
| using var client = factory.CreateClient(); | ||
|
|
||
| var response = await client.GetAsync("/api/health"); | ||
|
|
||
| Assert.Equal("nosniff", response.Headers.GetValues("X-Content-Type-Options").Single()); | ||
| Assert.Equal("DENY", response.Headers.GetValues("X-Frame-Options").Single()); | ||
| Assert.Equal("same-origin", response.Headers.GetValues("Referrer-Policy").Single()); | ||
| Assert.Equal("camera=(), microphone=(), geolocation=()", response.Headers.GetValues("Permissions-Policy").Single()); | ||
| } |
Requested a full code-quality and security audit against architecture, coding style, comment standards, dead-code, and a three-dimensional vulnerability scan (network / low-level runtime / business logic), with results in a mandated structured report format.
Audit outcome
CryptographicOperations.FixedTimeEqualsfor API key comparison, realpath-based symlink-safe path resolution, parameterized SQL, global capability-based authorization filter, and pinned dependencies with a documented CVE mitigation forSQLitePCLRaw.localStorage, exposing it to exfiltration if an XSS bug is ever introduced elsewhere. Remediating this requires a broader auth-transport redesign (HttpOnly cookie + CSRF token) that's out of scope for a minimal, low-risk fix — documented as a medium-term follow-up instead.Fixes applied
IdempotencyMiddleware: request-body hashing copy buffer size →RequestBodyCopyBufferBytesOtaUpdateService: download streaming copy buffer size →DownloadCopyBufferBytesFortOS.Cli/Program.cs: banner display delay →BannerDisplayDelayMillisecondsFilePathResolver:realpathsubprocess timeout →RealpathTimeoutSecondsDocumentation
docs/CODE_AUDIT_REPORT.mdcontaining the full audit in the fixed format requested: quality score, architecture/style/dead-code findings, severity-classified vulnerability list, refactoring roadmap, and a qualified-code acceptance checklist.