-
Notifications
You must be signed in to change notification settings - Fork 0
fix: address CodeQL findings (file service + VNC secret at rest) #11
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 all commits
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| using System.Net.Http.Json; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
@@ -37,7 +39,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| try | ||
| { | ||
| var secretFile = Path.Combine(_opt.EnrollmentDir, "vnc.secret"); | ||
| string? password = File.Exists(secretFile) ? File.ReadAllText(secretFile).Trim() : null; | ||
| string? password = ReadSecret(secretFile); | ||
|
|
||
| var msi = Path.Combine(AppContext.BaseDirectory, "vnc", "tightvnc.msi"); | ||
|
|
||
|
|
@@ -48,7 +50,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| { | ||
| await VncProvisioner.EnsureInstalledAsync(msi); | ||
| VncProvisioner.ApplyHardening(password); | ||
| File.WriteAllText(secretFile, password); | ||
| WriteSecret(secretFile, password); | ||
| logger.LogInformation(L.VncProvisioningService_VNCProvisionedPerDevicePassword); | ||
| } | ||
| catch (Exception ex) | ||
|
|
@@ -87,14 +89,33 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| try | ||
| { | ||
| var secretFile = Path.Combine(_opt.EnrollmentDir, "vnc.secret"); | ||
| var pw = File.Exists(secretFile) ? File.ReadAllText(secretFile).Trim() : null; | ||
| var pw = ReadSecret(secretFile); | ||
| if (!string.IsNullOrEmpty(pw)) | ||
| await VncProvisioner.EnsureHealthyAsync(pw, msiPath); | ||
| } | ||
| catch (Exception ex) { logger.LogDebug(ex, L.VncProvisioningService_VNCPasswordReportFailed); } | ||
| } | ||
| } | ||
|
|
||
| // vnc.secret stores the local VNC password. Encrypt it at rest with DPAPI (machine scope; the agent | ||
| // runs as SYSTEM), transparently migrating any legacy plaintext file the first time it is read. | ||
| private static string? ReadSecret(string file) | ||
| { | ||
| if (!File.Exists(file)) return null; | ||
| var raw = File.ReadAllBytes(file); | ||
| if (raw.Length == 0) return null; | ||
| try { return Encoding.UTF8.GetString(ProtectedData.Unprotect(raw, null, DataProtectionScope.LocalMachine)).Trim(); } | ||
| catch | ||
| { | ||
| var plain = Encoding.UTF8.GetString(raw).Trim(); // legacy plaintext | ||
| try { WriteSecret(file, plain); } catch { /* best-effort migration */ } | ||
| return plain; | ||
| } | ||
|
Comment on lines
+107
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Differentiate legacy plaintext from decryption failure to avoid silent secret corruption.
Proposed hardening+ private static ReadOnlySpan<byte> SecretHeader => "DPAPI1:"u8;
+
private static string? ReadSecret(string file)
{
if (!File.Exists(file)) return null;
var raw = File.ReadAllBytes(file);
if (raw.Length == 0) return null;
- try { return Encoding.UTF8.GetString(ProtectedData.Unprotect(raw, null, DataProtectionScope.LocalMachine)).Trim(); }
- catch
+ // New encrypted format: header + DPAPI blob
+ if (raw.AsSpan().StartsWith(SecretHeader))
{
- var plain = Encoding.UTF8.GetString(raw).Trim(); // legacy plaintext
- try { WriteSecret(file, plain); } catch { /* best-effort migration */ }
- return plain;
+ var blob = raw.AsSpan(SecretHeader.Length).ToArray();
+ return Encoding.UTF8.GetString(
+ ProtectedData.Unprotect(blob, null, DataProtectionScope.LocalMachine)).Trim();
}
+
+ // Legacy plaintext migration path only.
+ var plain = Encoding.UTF8.GetString(raw).Trim();
+ try { WriteSecret(file, plain); } catch { /* best-effort migration */ }
+ return plain;
}
private static void WriteSecret(string file, string password) =>
- File.WriteAllBytes(file, ProtectedData.Protect(Encoding.UTF8.GetBytes(password), null, DataProtectionScope.LocalMachine));
+ File.WriteAllBytes(
+ file,
+ SecretHeader.ToArray()
+ .Concat(ProtectedData.Protect(Encoding.UTF8.GetBytes(password), null, DataProtectionScope.LocalMachine))
+ .ToArray());🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| private static void WriteSecret(string file, string password) => | ||
| File.WriteAllBytes(file, ProtectedData.Protect(Encoding.UTF8.GetBytes(password), null, DataProtectionScope.LocalMachine)); | ||
|
|
||
| private async Task ReportSecretAsync(string password, CancellationToken ct) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(_opt.Telemetry.IngestUrl)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.