|
| 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 | +} |
0 commit comments