envsubst: support a negative substring length instead of panicking - #1275
Open
arpitjain099 wants to merge 1 commit into
Open
envsubst: support a negative substring length instead of panicking#1275arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
toSubstr parsed the length argument and then sliced s[pos : pos+length]
without considering a negative length, so ${VAR:2:-1} produced a
backwards slice and panicked with
slice bounds out of range [2:1]
The template is the manifest text, so a Kustomization using post-build
substitution can take down the controller with a substitution expression
alone.
bash counts a negative length back from the end of the string, so
${VAR:2:-1} on "hello world" is "llo worl", and it rejects the
expression when the end lands before the offset. Match that, returning an
empty string for the rejected case since these helpers have no error
channel. Verified against bash 5.
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
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.
toSubstrparses the length argument and then slices with it directly:A negative length is never considered, so
${VAR:2:-1}builds a backwards slice and panics:That is reachable straight through the public API, not just the helper:
which matters because with post-build substitution the template is the manifest text, so the expression that crashes the process can come from a Kustomization rather than from the operator's own environment.
The rest of this file goes out of its way to match bash ("bash returns the string if the position cannot be parsed"), and bash has had negative lengths since 4.2: they count back from the end of the string, and the expression is rejected when the end lands before the offset. I checked the cases against bash 5 rather than going from the manual:
So this handles the negative length the same way, returning an empty string for the case bash errors on, since these helpers have no error channel. Positive lengths take exactly the same path as before.
Four cases added to
Test_substr. On the unmodified tree the first of them panics and takes the test binary down; with the changego test ./...passes across the envsubst module.