Skip to content

Commit 5424632

Browse files
vyncintclaude
andauthored
Redact environment variable values in vminitd debug logs
vminitd logged the full OCI spec/process at debug level, exposing secrets passed through environment variables (#518). Add redactingEnvironmentValues() to ContainerizationOCI's Spec, Process, Hook, and Hooks - masking values while keeping variable names - and use it at both log sites in ManagedContainer. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 50f7722 commit 5424632

3 files changed

Lines changed: 193 additions & 2 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the Containerization project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
extension Spec {
18+
/// Returns a copy of the spec that is safe to include in log output:
19+
/// the environment variable values of the container process and of all
20+
/// lifecycle hooks are replaced with `<redacted>`, keeping only the
21+
/// variable names. Environment variables routinely carry secrets, and
22+
/// logs must not expose them.
23+
public func redactingEnvironmentValues() -> Spec {
24+
var copy = self
25+
copy.process = copy.process?.redactingEnvironmentValues()
26+
copy.hooks = copy.hooks?.redactingEnvironmentValues()
27+
return copy
28+
}
29+
}
30+
31+
extension Process {
32+
/// Returns a copy of the process whose environment variable values are
33+
/// replaced with `<redacted>`, keeping only the variable names. Use this
34+
/// when rendering a process into log output so secrets injected through
35+
/// the environment are not exposed.
36+
public func redactingEnvironmentValues() -> Process {
37+
var copy = self
38+
copy.env = redactedEnvironment(copy.env)
39+
return copy
40+
}
41+
}
42+
43+
extension Hook {
44+
/// Returns a copy of the hook whose environment variable values are
45+
/// replaced with `<redacted>`, keeping only the variable names.
46+
public func redactingEnvironmentValues() -> Hook {
47+
var copy = self
48+
copy.env = redactedEnvironment(copy.env)
49+
return copy
50+
}
51+
}
52+
53+
extension Hooks {
54+
/// Returns a copy of the hooks whose environment variable values are
55+
/// replaced with `<redacted>`, keeping only the variable names.
56+
public func redactingEnvironmentValues() -> Hooks {
57+
var copy = self
58+
copy.prestart = copy.prestart.map { $0.redactingEnvironmentValues() }
59+
copy.createRuntime = copy.createRuntime.map { $0.redactingEnvironmentValues() }
60+
copy.createContainer = copy.createContainer.map { $0.redactingEnvironmentValues() }
61+
copy.startContainer = copy.startContainer.map { $0.redactingEnvironmentValues() }
62+
copy.poststart = copy.poststart.map { $0.redactingEnvironmentValues() }
63+
copy.poststop = copy.poststop.map { $0.redactingEnvironmentValues() }
64+
return copy
65+
}
66+
}
67+
68+
/// Replaces the value of every `NAME=value` entry with `<redacted>`. Entries
69+
/// without an `=` are kept as-is: they name variables to inherit and carry
70+
/// no value of their own.
71+
private func redactedEnvironment(_ env: [String]) -> [String] {
72+
env.map { entry in
73+
guard let separator = entry.firstIndex(of: "=") else {
74+
return entry
75+
}
76+
return entry[..<separator] + "=<redacted>"
77+
}
78+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the Containerization project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import Testing
19+
20+
@testable import ContainerizationOCI
21+
22+
struct SpecRedactionTests {
23+
@Test func processEnvironmentValuesAreRedacted() {
24+
let process = ContainerizationOCI.Process(
25+
args: ["python3", "-m", "http.server"],
26+
cwd: "/app",
27+
env: [
28+
"PATH=/usr/local/bin:/usr/bin",
29+
"MY_SUPER_SECRET_PASSWORD=guest",
30+
"EMPTY=",
31+
"TOKEN=abc=def",
32+
"INHERITED_NO_VALUE",
33+
]
34+
)
35+
36+
let redacted = process.redactingEnvironmentValues()
37+
38+
#expect(
39+
redacted.env == [
40+
"PATH=<redacted>",
41+
"MY_SUPER_SECRET_PASSWORD=<redacted>",
42+
"EMPTY=<redacted>",
43+
"TOKEN=<redacted>",
44+
"INHERITED_NO_VALUE",
45+
])
46+
// Everything except env is untouched.
47+
#expect(redacted.args == process.args)
48+
#expect(redacted.cwd == process.cwd)
49+
// The original is not mutated.
50+
#expect(process.env.contains("MY_SUPER_SECRET_PASSWORD=guest"))
51+
}
52+
53+
@Test func redactedProcessRendersWithoutSecretValues() {
54+
// Mirrors the report in issue #518: interpolating the process into a
55+
// log message must not expose environment variable values.
56+
let process = ContainerizationOCI.Process(
57+
args: ["echo", "hello"],
58+
env: ["MY_OTHER_SECRET_PASSWORD=abc123"]
59+
)
60+
61+
let logged = "\(process.redactingEnvironmentValues())"
62+
63+
#expect(!logged.contains("abc123"))
64+
#expect(logged.contains("MY_OTHER_SECRET_PASSWORD"))
65+
}
66+
67+
@Test func specRedactsProcessAndHookEnvironments() {
68+
let hook = Hook(
69+
path: "/usr/local/bin/hook",
70+
args: ["hook"],
71+
env: ["HOOK_SECRET=hunter2"],
72+
timeout: nil
73+
)
74+
let spec = Spec(
75+
hooks: Hooks(
76+
prestart: [hook],
77+
createRuntime: [hook],
78+
createContainer: [hook],
79+
startContainer: [hook],
80+
poststart: [hook],
81+
poststop: [hook]
82+
),
83+
process: ContainerizationOCI.Process(env: ["MY_SUPER_SECRET_PASSWORD=guest"]),
84+
hostname: "web",
85+
mounts: [Mount(type: "proc", source: "proc", destination: "/proc")]
86+
)
87+
88+
let redacted = spec.redactingEnvironmentValues()
89+
90+
let logged = "\(redacted)"
91+
#expect(!logged.contains("guest"))
92+
#expect(!logged.contains("hunter2"))
93+
#expect(logged.contains("MY_SUPER_SECRET_PASSWORD"))
94+
#expect(logged.contains("HOOK_SECRET"))
95+
96+
// Everything except environments is untouched.
97+
#expect(redacted.hostname == spec.hostname)
98+
#expect(redacted.mounts.count == spec.mounts.count)
99+
#expect(redacted.process?.env == ["MY_SUPER_SECRET_PASSWORD=<redacted>"])
100+
#expect(redacted.hooks?.prestart.first?.env == ["HOOK_SECRET=<redacted>"])
101+
#expect(redacted.hooks?.poststop.first?.env == ["HOOK_SECRET=<redacted>"])
102+
// The original is not mutated.
103+
#expect(spec.process?.env == ["MY_SUPER_SECRET_PASSWORD=guest"])
104+
}
105+
106+
@Test func specWithoutProcessOrHooksIsUnchanged() {
107+
let spec = Spec(version: "1.2.0")
108+
let redacted = spec.redactingEnvironmentValues()
109+
#expect(redacted.process == nil)
110+
#expect(redacted.hooks == nil)
111+
#expect(redacted.version == spec.version)
112+
}
113+
}

‎vminitd/Sources/VminitdCore/ManagedContainer.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public actor ManagedContainer {
5555
path: Self.craftBundlePath(id: id),
5656
spec: spec
5757
)
58-
log.debug("created bundle with spec \(spec)")
58+
log.debug("created bundle with spec \(spec.redactingEnvironmentValues())")
5959

6060
let cgManager = Cgroup2Manager(
6161
group: URL(filePath: cgroupsPath),
@@ -164,7 +164,7 @@ extension ManagedContainer {
164164
stdio: HostStdio,
165165
process: ContainerizationOCI.Process
166166
) throws {
167-
log.debug("creating exec process with \(process)")
167+
log.debug("creating exec process with \(process.redactingEnvironmentValues())")
168168

169169
// Write the process config to the bundle, and pass this on
170170
// over to ManagedProcess to deal with.

0 commit comments

Comments
 (0)