From 8c9bab4d818b06d480ca28713004f7d338c4353e Mon Sep 17 00:00:00 2001 From: Anas Khan <83116240+anxkhn@users.noreply.github.com> Date: Fri, 3 Jul 2026 23:08:45 +0530 Subject: [PATCH] Fix crash parsing a mount directive with an empty key. Parser.mount split each comma-separated part on '=' and immediately read keyVal[0]. Swift's split omits empty subsequences by default, so a part consisting solely of '=' characters (for example `--mount "="` or a `=` segment inside a longer spec) produced an empty array and trapped with a fatal 'Index out of range', crashing the process on user input. Guard the first element and throw a ContainerizationError(.invalidArgument) so malformed input is rejected like other invalid mount directives. Add a regression test covering "=", "==", and an embedded empty segment. --- .../ContainerAPIService/Client/Parser.swift | 5 ++++- Tests/ContainerAPIClientTests/ParserTest.swift | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/Services/ContainerAPIService/Client/Parser.swift b/Sources/Services/ContainerAPIService/Client/Parser.swift index ef209df5c..3dd5fa3ba 100644 --- a/Sources/Services/ContainerAPIService/Client/Parser.swift +++ b/Sources/Services/ContainerAPIService/Client/Parser.swift @@ -369,7 +369,10 @@ public struct Parser { var directives = defaultDirectives for part in parts { let keyVal = part.split(separator: "=", maxSplits: 2) - var key = String(keyVal[0]) + guard let rawKey = keyVal.first else { + throw ContainerizationError(.invalidArgument, message: "invalid mount directive '\(part)' in \(mount)") + } + var key = String(rawKey) var skipValue = false switch key { case "type", "size", "mode": diff --git a/Tests/ContainerAPIClientTests/ParserTest.swift b/Tests/ContainerAPIClientTests/ParserTest.swift index 0dcc6f7cf..41419f44b 100644 --- a/Tests/ContainerAPIClientTests/ParserTest.swift +++ b/Tests/ContainerAPIClientTests/ParserTest.swift @@ -512,6 +512,23 @@ struct ParserTest { } } + @Test + func testMountEmptyDirectiveSegment() throws { + // A comma-segment consisting solely of "=" characters splits to an empty + // array under Swift's default omittingEmptySubsequences, so reading the + // key without a guard would trap. These must surface a validation error. + for input in ["=", "==", "type=bind,=,dst=/foo"] { + #expect { + _ = try Parser.mount(input) + } throws: { error in + guard let error = error as? ContainerizationError else { + return false + } + return error.description.contains("invalid mount directive") + } + } + } + @Test func testIsValidDomainNameOk() throws { let names = [