Fix crash parsing a mount directive with an empty key.#1896
Open
anxkhn wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Change
Motivation and Context
Parser.mount(_:)splits each comma-separated part of a mount spec on=and thenimmediately reads
keyVal[0]. Swift'ssplit(separator:)omits empty subsequences bydefault, so a part made up solely of
=characters produces an empty array and thekeyVal[0]read traps with a fatalIndex out of range, crashing the process on userinput.
This is reachable straight from the CLI. For example:
The existing
parts.count == 0guard only catches an empty or all-comma string; it doesnot catch a
=-only segment.The fix guards the first element and throws a
ContainerizationError(.invalidArgument, ...)so the malformed input is rejected the sameway other bad mount directives already are, instead of aborting the process. The change is
a single guarded read; the error message shape matches the sibling
.invalidArgumentthrows already in
mount(_:).This is the same class of user-input crash fix as #1612 ("Fix potential integer math crash
on
PublishPort."), which also hardenedParser.swiftand added a regression test.Testing
Added
testMountEmptyDirectiveSegmentinTests/ContainerAPIClientTests/ParserTest.swift,asserting that
"=","==", and"type=bind,=,dst=/foo"each throw aContainerizationErrorwhose description contains "invalid mount directive". Reverting onlythe guard makes the new test trap with
Fatal error: Index out of range(proving the crash);with the guard it passes.
swift formatreports no drift,swift format lint --strictpasses, and the full
ContainerAPIClientTestssuite (184 tests) is green.The diff (for reference; already committed locally on
patch-5, unsigned)Sources/Services/ContainerAPIService/Client/Parser.swift(+4/-1):Tests/ContainerAPIClientTests/ParserTest.swift(+17): new@Test testMountEmptyDirectiveSegment().