using System; using System.Collections.Concurrent; using System.IO; using System.IO.Compression; using System.Security.Cryptography; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using ClassNotify.Server.Data; using ClassNotify.Server.Domain.Entities; using ClassNotify.Server.Services; using ClassNotify.Shared.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace ClassNotify.Server.Controllers; /// /// 终端诊断包上传与上报接口 (P2.5 RC1-3 Phase 2: Device-Authenticated Secure Upload) /// 严格满足 SB4 (设备越权阻断)、SB5 (白名单校验)、SB6 (防秘密注入)、SB7 (5MiB硬限制)、 /// SB8 (恶意路径阻断)、SB9 (First-Wins 原子胜出) 与防压缩炸弹约束 /// [ApiController] [Route("api/device/support-bundles")] public class DeviceSupportBundlesController : ControllerBase { private readonly ClassNotifyDbContext _db; private readonly ISecurityService _securityService; private readonly ILogger _logger; private readonly string _storageBaseDir; private static readonly ConcurrentDictionary _bundleLocks = new(); public DeviceSupportBundlesController( ClassNotifyDbContext db, ISecurityService securityService, ILogger logger) { _db = db; _securityService = securityService; _logger = logger; // 私有安全存储目录 (绝不暴露在 wwwroot 下,杜绝直接 HTTP 静态访问) var envStorage = Environment.GetEnvironmentVariable("CLASSNOTIFY_SUPPORT_BUNDLE_STORAGE"); _storageBaseDir = !string.IsNullOrWhiteSpace(envStorage) ? envStorage : Path.Combine(AppContext.BaseDirectory, "storage", "support-bundles"); } /// /// 终端上传诊断包 ZIP (P2-1 ~ P2-10, SB4, SB5, SB7, SB8, SB9) /// [HttpPost("{supportBundleId}/upload")] [DisableRequestSizeLimit] public async Task UploadSupportBundle( [FromRoute] string supportBundleId, CancellationToken ct) { // 1. 终端鉴权 (X-Device-Id, X-Device-Token) var deviceId = Request.Headers["X-Device-Id"].ToString(); var deviceToken = Request.Headers["X-Device-Token"].ToString(); if (string.IsNullOrWhiteSpace(deviceId) || string.IsNullOrWhiteSpace(deviceToken)) { return Unauthorized(new { errorCode = "Unauthorized", message = "Missing device authentication credentials." }); } var device = await _securityService.ValidateDeviceAsync(deviceId, deviceToken); if (device == null) { return Unauthorized(new { errorCode = "Unauthorized", message = "Invalid device credentials." }); } // 2. 加载诊断包任务记录 var bundle = await _db.DeviceSupportBundles.FirstOrDefaultAsync(b => b.Id == supportBundleId, ct); if (bundle == null) { return NotFound(new { errorCode = "SupportBundleNotFound", message = $"Support bundle '{supportBundleId}' not found." }); } // 3. 校验设备越权与租户隔离 (SB4: Device A 不得上传 Device B 的 Bundle,跨校拦截) if (!string.Equals(bundle.DeviceId, device.Id, StringComparison.OrdinalIgnoreCase) || !string.Equals(bundle.TenantId, device.TenantId, StringComparison.OrdinalIgnoreCase)) { _logger.LogWarning("Device {DeviceId} (Tenant={Tenant}) attempted unauthorized upload for bundle {BundleId} owned by device {OwnerDevice} (Tenant={OwnerTenant}). 403 Forbidden.", device.Id, device.TenantId, supportBundleId, bundle.DeviceId, bundle.TenantId); return StatusCode(403, new { errorCode = SupportBundleErrorCodes.DeviceMismatch, message = "Device is not authorized to upload this support bundle." }); } // 4. 状态校验 (只允许 Collecting 状态接收上传;终态已超时/失败禁止复活) if (bundle.Status == SupportBundleStatuses.TimedOut || bundle.Status == SupportBundleStatuses.Failed || bundle.Status == SupportBundleStatuses.Expired) { return Conflict(new { errorCode = SupportBundleErrorCodes.BundleNotAcceptingUpload, message = $"Support bundle is in terminal state '{bundle.Status}' and cannot accept uploads." }); } // 5. 流式写入唯一临时文件,并实施 5 MiB 硬限制 (SB7) var tmpDir = Path.Combine(_storageBaseDir, "tmp"); Directory.CreateDirectory(tmpDir); var tempFilePath = Path.Combine(tmpDir, $"{supportBundleId}.{Guid.NewGuid():N}.upload"); const long maxSizeBytes = 5 * 1024 * 1024; // 5 MiB long totalBytesRead = 0; try { await using (var fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 81920, useAsync: true)) { var buffer = new byte[81920]; int read; while ((read = await Request.Body.ReadAsync(buffer.AsMemory(0, buffer.Length), ct)) > 0) { totalBytesRead += read; if (totalBytesRead > maxSizeBytes) { await fileStream.DisposeAsync(); TryDeleteFile(tempFilePath); return StatusCode(413, new { errorCode = SupportBundleErrorCodes.BundleTooLarge, message = "Uploaded bundle exceeds maximum allowed size (5 MiB)." }); } await fileStream.WriteAsync(buffer.AsMemory(0, read), ct); } } if (totalBytesRead == 0) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidZip, message = "Uploaded bundle stream is empty." }); } // 6. ZIP 安全合规核验 (P2-1, P2-4, P2-7, P2-8) ZipArchive archive; try { archive = ZipFile.OpenRead(tempFilePath); } catch (Exception) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidZip, message = "Uploaded payload is not a valid ZIP archive." }); } using (archive) { // Entry 数量严格限制为 4 (SB5) if (archive.Entries.Count != 4) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.UnexpectedEntry, message = $"ZIP must contain exactly 4 entries, found {archive.Entries.Count}." }); } var allowedEntries = new HashSet(StringComparer.Ordinal) { "manifest.json", "runtime.json", "selfcheck.json", "events.ndjson" }; long totalExpandedSize = 0; ZipArchiveEntry? manifestEntry = null; foreach (var entry in archive.Entries) { // 恶意路径防护 (SB8: 严禁目录、父级相对路径 ../、绝对路径等) if (entry.FullName.Contains('/') || entry.FullName.Contains('\\') || entry.FullName.Contains("..") || entry.FullName.Contains(':')) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidZip, message = $"Malicious path in ZIP entry: '{entry.FullName}'." }); } if (!allowedEntries.Contains(entry.FullName)) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.UnexpectedEntry, message = $"Forbidden entry '{entry.FullName}' found in bundle." }); } // 防压缩炸弹检查 (P2-8: 解压后总量不超过 10 MiB) totalExpandedSize += entry.Length; if (totalExpandedSize > 10 * 1024 * 1024) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.ExpandedSizeExceeded, message = "Expanded bundle size exceeds 10 MiB limit." }); } if (entry.FullName == "manifest.json") { manifestEntry = entry; } } // 7. manifest.json 二次核验 (P2-9) if (manifestEntry == null) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidManifest, message = "manifest.json missing." }); } using var manifestStream = manifestEntry.Open(); SupportBundleManifestV1? manifest; try { manifest = await JsonSerializer.DeserializeAsync(manifestStream, cancellationToken: ct); } catch (Exception) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidManifest, message = "Failed to parse manifest.json." }); } if (manifest == null || manifest.FormatVersion != 1 || !string.Equals(manifest.SupportBundleId, supportBundleId, StringComparison.OrdinalIgnoreCase) || !string.Equals(manifest.DeviceId, device.Id, StringComparison.OrdinalIgnoreCase)) { TryDeleteFile(tempFilePath); return BadRequest(new { errorCode = SupportBundleErrorCodes.InvalidManifest, message = "manifest.json attributes do not match route ID or authenticated device." }); } } // 8. 计算整个 ZIP 文件 SHA256 (全系统小写 64 hex) string sha256; using (var sha = SHA256.Create()) await using (var fileStream = System.IO.File.OpenRead(tempFilePath)) { var hashBytes = await sha.ComputeHashAsync(fileStream, ct); sha256 = Convert.ToHexString(hashBytes).ToLowerInvariant(); } // 9. 存储至私有不可覆盖的 immutable blob 文件路径 var todayStr = DateTime.UtcNow.ToString("yyyyMMdd"); var relativeFilePath = $"{todayStr}/{supportBundleId}.{sha256}.zip"; var targetDir = Path.Combine(_storageBaseDir, todayStr); Directory.CreateDirectory(targetDir); var finalFilePath = Path.Combine(targetDir, $"{supportBundleId}.{sha256}.zip"); // 将临时文件移动到最终路径 (若已存在相同哈希则保留) if (!System.IO.File.Exists(finalFilePath)) { System.IO.File.Move(tempFilePath, finalFilePath); } else { TryDeleteFile(tempFilePath); } // 10. 数据库原子条件更新 (First-Wins, SB9) // 严格仅在 Status == 'Collecting' 时允许流转为 'Available' // 并且 DB 中 StoragePath 严格仅保存相对路径 (yyyyMMdd/sb_xxx.sha.zip) var now = DateTimeOffset.UtcNow; int affected; if (_db.Database.IsRelational()) { affected = await _db.DeviceSupportBundles .Where(x => x.Id == supportBundleId && x.DeviceId == device.Id && x.Status == SupportBundleStatuses.Collecting) .ExecuteUpdateAsync(setters => setters .SetProperty(x => x.Status, SupportBundleStatuses.Available) .SetProperty(x => x.StoragePath, relativeFilePath) .SetProperty(x => x.SizeBytes, totalBytesRead) .SetProperty(x => x.Sha256, sha256) .SetProperty(x => x.CompletedAtUtc, now) .SetProperty(x => x.ExpiresAtUtc, now.AddHours(24)) .SetProperty(x => x.UpdatedAtUtc, now), ct); } else { var sem = _bundleLocks.GetOrAdd(supportBundleId, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(ct); try { var b = await _db.DeviceSupportBundles .FirstOrDefaultAsync(x => x.Id == supportBundleId && x.DeviceId == device.Id && x.Status == SupportBundleStatuses.Collecting, ct); if (b != null) { b.Status = SupportBundleStatuses.Available; b.StoragePath = relativeFilePath; b.SizeBytes = totalBytesRead; b.Sha256 = sha256; b.CompletedAtUtc = now; b.ExpiresAtUtc = now.AddHours(24); b.UpdatedAtUtc = now; await _db.SaveChangesAsync(ct); affected = 1; } else { affected = 0; } } finally { sem.Release(); } } if (affected == 1) { _logger.LogInformation("Support bundle {BundleId} successfully transitioned to Available (Winner). Size={Size}, SHA256={Sha256}", supportBundleId, totalBytesRead, sha256); return Ok(new { success = true, duplicate = false, supportBundleId = supportBundleId, status = SupportBundleStatuses.Available, sha256 = sha256, sizeBytes = totalBytesRead }); } // 若 affected == 0,说明存在并发竞争,检查是否已被前一个上传完成 var current = await _db.DeviceSupportBundles.AsNoTracking().FirstOrDefaultAsync(x => x.Id == supportBundleId, ct); if (current != null && current.Status == SupportBundleStatuses.Available) { // 核心安全原则:任何文件在删除之前,必须证明它没有被当前 DB StoragePath / Sha256 引用 if (string.Equals(current.StoragePath, finalFilePath, StringComparison.OrdinalIgnoreCase) || string.Equals(current.Sha256, sha256, StringComparison.OrdinalIgnoreCase)) { // DB winner 正在引用这个 canonical file。无论此请求是不是创建文件的人,都绝不能删。 return Ok(new { success = true, duplicate = true, supportBundleId = supportBundleId, status = SupportBundleStatuses.Available, sha256 = current.Sha256, sizeBytes = current.SizeBytes }); } // 当前请求生成的是另一个内容不同、未被 DB 引用的 candidate,清理其孤立文件 (SB9) TryDeleteFile(finalFilePath); _logger.LogWarning("Concurrent upload collision for bundle {BundleId}: loser with SHA {Sha} rejected and unreferenced blob cleaned up.", supportBundleId, sha256); return Conflict(new { errorCode = SupportBundleErrorCodes.BundleAlreadyCompleted, message = "Support bundle was already completed by a concurrent upload." }); } // 非 Available 终态或异常:仅当该文件未被当前 DB 记录引用时才清理 if (!string.Equals(current?.StoragePath, finalFilePath, StringComparison.OrdinalIgnoreCase)) { TryDeleteFile(finalFilePath); } return Conflict(new { errorCode = SupportBundleErrorCodes.BundleNotAcceptingUpload, message = $"Support bundle is in state '{current?.Status}' and cannot be updated." }); } catch (Exception ex) { TryDeleteFile(tempFilePath); _logger.LogError(ex, "Unexpected error processing support bundle upload for {BundleId}", supportBundleId); return StatusCode(500, new { errorCode = SupportBundleErrorCodes.StorageFailure, message = "Internal storage error processing bundle upload." }); } } /// /// 终端主动上报采集失败 (P2-12) /// [HttpPost("{supportBundleId}/failed")] public async Task ReportFailure( [FromRoute] string supportBundleId, [FromBody] SupportBundleFailureReportDto? report, CancellationToken ct) { var deviceId = Request.Headers["X-Device-Id"].ToString(); var deviceToken = Request.Headers["X-Device-Token"].ToString(); if (string.IsNullOrWhiteSpace(deviceId) || string.IsNullOrWhiteSpace(deviceToken)) { return Unauthorized(new { errorCode = "Unauthorized", message = "Missing device authentication credentials." }); } var device = await _securityService.ValidateDeviceAsync(deviceId, deviceToken); if (device == null) { return Unauthorized(new { errorCode = "Unauthorized", message = "Invalid device credentials." }); } var bundle = await _db.DeviceSupportBundles.FirstOrDefaultAsync(b => b.Id == supportBundleId, ct); if (bundle == null) { return NotFound(new { errorCode = "SupportBundleNotFound", message = $"Support bundle '{supportBundleId}' not found." }); } if (!string.Equals(bundle.DeviceId, device.Id, StringComparison.OrdinalIgnoreCase) || !string.Equals(bundle.TenantId, device.TenantId, StringComparison.OrdinalIgnoreCase)) { return StatusCode(403, new { errorCode = SupportBundleErrorCodes.DeviceMismatch, message = "Device is not authorized to update this support bundle." }); } var now = DateTimeOffset.UtcNow; var errorCode = report?.ErrorCode ?? SupportBundleErrorCodes.CollectionFailure; int affected; if (_db.Database.IsRelational()) { affected = await _db.DeviceSupportBundles .Where(x => x.Id == supportBundleId && x.DeviceId == device.Id && x.Status == SupportBundleStatuses.Collecting) .ExecuteUpdateAsync(setters => setters .SetProperty(x => x.Status, SupportBundleStatuses.Failed) .SetProperty(x => x.ErrorCode, errorCode) .SetProperty(x => x.CompletedAtUtc, now) .SetProperty(x => x.UpdatedAtUtc, now), ct); } else { var sem = _bundleLocks.GetOrAdd(supportBundleId, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(ct); try { var b = await _db.DeviceSupportBundles .FirstOrDefaultAsync(x => x.Id == supportBundleId && x.DeviceId == device.Id && x.Status == SupportBundleStatuses.Collecting, ct); if (b != null) { b.Status = SupportBundleStatuses.Failed; b.ErrorCode = errorCode; b.CompletedAtUtc = now; b.UpdatedAtUtc = now; await _db.SaveChangesAsync(ct); affected = 1; } else { affected = 0; } } finally { sem.Release(); } } if (affected == 1) { _logger.LogInformation("Support bundle {BundleId} marked as Failed by device {DeviceId}. ErrorCode={ErrorCode}", supportBundleId, device.Id, errorCode); return Ok(new { success = true, supportBundleId, status = SupportBundleStatuses.Failed }); } return Conflict(new { errorCode = SupportBundleErrorCodes.BundleNotAcceptingUpload, message = "Support bundle is not in Collecting state." }); } private static void TryDeleteFile(string path) { try { if (System.IO.File.Exists(path)) System.IO.File.Delete(path); } catch { } } }