Files
Vyncint Ng ff44a5b683 Redact environment variable values in vminitd debug logs (#813)
Fixes #518.

## What

vminitd logs the full OCI spec and exec process at debug level in
`ManagedContainer` ("created bundle with spec …", "creating exec process
with …"), which puts every `NAME=value` environment entry into the boot
log. Environment variables routinely carry secrets, so `container logs
--boot web | grep PASSWORD` reproduces the leak exactly as described in
#518.

Rather than redacting at the call sites, this makes the redacted form
the *default* rendering of the types that own an environment: `Process`
and `Hook` conform to `CustomStringConvertible` with values masked and
names kept. `Spec` and `Hooks` inherit it, because Swift's
reflection-based description renders a nested value through that value's
own `description`.

The effect is that any `\(spec)` or `\(process)` is safe without the
author knowing this file exists, which is what stops a log line added
later from reintroducing the leak. The two existing log sites are
unchanged, so this no longer touches vminitd at all.

Two details worth calling out:

- **`Codable` is untouched.** `description` governs text rendering only,
so an encoded spec still carries the real values and nothing changes
about what is written to disk or sent to the guest. The unredacted
environment also remains available to callers through `process.env`.
- **`description` renders through a mirror** rather than a hand-written
field list. `Process` has 13 fields; listing them by hand would drop the
rest from the log line and would rot as fields are added.

## Verification

- New `SpecRedactionTests` (9 tests) cover: a whole `Spec` interpolated
into a log line never renders the values; `String(describing:)` and
`String(reflecting:)` are redacted too; variable names survive;
`NAME`-only inherit entries pass through; `NAME=` and values containing
further `=` are masked whole; encoding round-trips with the real values;
rendering does not mutate; and the other fields are still rendered.
- Negative control: with the redaction disabled the suite fails with 13
issues, and the output shows the secret in the clear, reproducing #518.
- Full `ContainerizationOCITests` passes, 58 tests in 9 suites.
- `swift format lint --strict --configuration .swift-format-nolint` is
clean, and `swift format` leaves both files unchanged.

Every line here is one I can explain and justify; the reasoning above is
the complete rationale for each change.
2026-07-29 13:49:56 -04:00

70 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
// Environment variables routinely carry secrets, so rendering a process or a
// hook as text must not expose their values. These conformances make the
// redacted form the *default* rendering rather than something a caller has to
// opt into: any `\(spec)` or `\(process)`, in this repo or downstream, is safe
// without the author knowing this file exists.
//
// Only the two types that own an `env` need conforming. Swift's reflection
// based description uses a nested value's own `description`, so `Spec` and
// `Hooks` inherit the redaction through the values they hold.
//
// This affects text rendering only. `Codable` is untouched, so an encoded spec
// still carries the real values, and the unredacted environment remains
// available to callers through `process.env`.
extension Process: CustomStringConvertible {
public var description: String {
var copy = self
copy.env = redactingEnvironmentValues(copy.env)
return describeFields(of: copy)
}
}
extension Hook: CustomStringConvertible {
public var description: String {
var copy = self
copy.env = redactingEnvironmentValues(copy.env)
return describeFields(of: copy)
}
}
/// Replaces the value of every `NAME=value` entry with `<redacted>`, keeping
/// the name, which is still useful for seeing *which* variables were set.
/// Entries without an `=` are kept as-is: they name a variable to inherit and
/// carry no value of their own.
private func redactingEnvironmentValues(_ env: [String]) -> [String] {
env.map { entry in
guard let separator = entry.firstIndex(of: "=") else {
return entry
}
return entry[..<separator] + "=<redacted>"
}
}
/// Renders `TypeName(label: value, ...)`, the shape Swift's own description
/// produces. Going through a mirror rather than listing the fields by hand
/// keeps every field in the log line, and means a field added later shows up
/// without anyone remembering to edit this file.
private func describeFields<T>(of value: T) -> String {
let fields = Mirror(reflecting: value).children.map { child in
"\(child.label ?? "_"): \(String(describing: child.value))"
}
return "\(T.self)(\(fields.joined(separator: ", ")))"
}