Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow almost all printable ASCII characters in environment variables #4373

Merged

Conversation

HirazawaUi
Copy link
Contributor

  • One-line PR description: Allow special characters in environment variables
  • Other comments:

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Dec 21, 2023
@k8s-ci-robot k8s-ci-robot added kind/kep Categorizes KEP tracking issues and PRs modifying the KEP directory sig/node Categorizes an issue or PR as relevant to SIG Node. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 21, 2023
@bart0sh bart0sh added this to Triage in SIG Node PR Triage Dec 22, 2023
@bart0sh bart0sh moved this from Triage to Waiting on Author in SIG Node PR Triage Dec 22, 2023
@bart0sh
Copy link
Contributor

bart0sh commented Dec 22, 2023

/cc

@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch 2 times, most recently from aeac2c2 to b75f5bb Compare December 24, 2023 13:42
- "@HirazawaUi"
owning-sig: sig-node
participating-sigs:
- sig-api-machinery
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this relevant to SIG Security?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the description, I assume this has nothing to do with SIG Security?https://github.com/kubernetes/community/tree/master/sig-security#security-special-interest-group

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be good to get a consultation - security holes are rarely where reasonable people expect them to be. :)

@IanColdwater @tabbysable ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch 2 times, most recently from b833deb to 950107c Compare January 8, 2024 13:45
Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I am OK with this proposal. Caution is warranted, but reasonable caution, IMO.

@liggitt or @SergeyKanzhelev objections or further consultations you want?


## Proposal

* Implements relaxed validation at the top level validation method when validating API create requests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you detail what the new validation will be? Is it "anything but '=' or is it something more precise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to being explicit here. The summary says Allow all printable ASCII except '='. Is that well-defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The summary says Allow all printable ASCII except '='. Is that well-defined?

I wrote a demo to verify the ASCII characters that can be set as environment variables

package main

import (
	"fmt"
	"os"
)

func setEnvVars() {
	for i := 0; i < 128; i++ {
		envKey := "a" + string(i)
		if err := os.Setenv(envKey, "abc"); err != nil {
			fmt.Println(i)
			fmt.Printf("set character %s to env name failed \n", envKey)
		}
	}
}

func main() {
	setEnvVars()
}

it results is

➜  ~ ./envvar
0
set character a to env name failed
61
set character a= to env name failed

It looks like all ASCII characters can be set as environment variables except for NUL with serial number 0 and = with serial number 61.... But I'd like to check if the CRI API restricts them first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is the main open issue, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the test results in #4373 (comment), I think we can constrain the characters to characters 33-126 in ASCII. I will add some regex to KEP, and elaborate it in more detail.

- "@HirazawaUi"
owning-sig: sig-node
participating-sigs:
- sig-api-machinery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be good to get a consultation - security holes are rarely where reasonable people expect them to be. :)

@IanColdwater @tabbysable ?

reviewers:
- "@liggitt"
- "@thockin"
- TBD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyKanzhelev or someone else from sig-node may want to look at it.


### Risks and Mitigations

Relaxed validation can break upgrade and rollback scenarios, but our use of feature gate to control whether it's enabled or not will make it a manageable risk, with the user having the autonomy to choose whether or not to enable it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature gates aren't intended to be a long-term configuration option. How is the risk managed as the feature gate graduates to GA and is enabled by default without user intervention, or is locked on and can no longer be disabled?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume it's like any other - locked once GA. Any reason not to follow this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My opinion is the same as @thockin, it should be the same as other feature gates, if we think it has risks when upgrade and rollback scenarios, we can let the beta time duration be long enough


## Proposal

* Implements relaxed validation at the top level validation method when validating API create requests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to being explicit here. The summary says Allow all printable ASCII except '='. Is that well-defined?


* Add a test to `test/e2e/common/secret.go` to test that the special characters in secret are consumed by the environment variable.

* Add a test to `test/e2e/common/expansion` to test environment variable can contain special characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add details about what this will exercise and that it checks all the way into the container (e.g. from kubernetes/kubernetes#53201 (comment))

e2e tests demonstrating we can actually construct an environment with envvar names at the edges of what we support and it makes it into the container successfully (envvar names containing newlines/whitespace, unprintable characters, multi-byte characters, "difficult" characters like '"*?;/\()&$, etc)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any CRI-specific (containerd vs crio vs docker vs ...) or OS-specific (linux or windows) oddities about support for envvars with weird characters in the names? do CRI implementations have to make adjustments to support this? does the CRI API specify allowed envvar names or is it arbitrary / opaque

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do CRI implementations have to make adjustments to support this? does the CRI API specify allowed envvar names or is it arbitrary / opaque

Let me do some research...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a demo to test envvar name support for different container runtimes that allow all printable ASCII characters as envvar names, even "=" is allowed, They have more relaxed restrictions on envvar than k8s.
ref: https://github.com/HirazawaUi/verfiy-container-env

Comment on lines +25 to +31
# The most recent milestone for which work toward delivery of this KEP has been
# done. This can be the current (upcoming) milestone, if it is being actively
# worked on.
latest-milestone: "v1.30"

# The milestone at which this feature was, or is targeted to be, at each stage.
milestone:
alpha: "v1.30"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure these are right for a provisional KEP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will modify it if it is not implemented in 1.30.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provisional KEPs are not supposed to have code merging, versus implementable
The idea being, provisional KEPs may have part of an idea merged to iterate on but aren't agreed to implement yet. An implementable KEP would be expected to have stronger approval and would have a target release for implementing. https://github.com/kubernetes/enhancements/blob/master/keps/sig-architecture/0000-kep-process/README.md#kep-workflow

@HirazawaUi
Copy link
Contributor Author

HirazawaUi commented Jan 28, 2024

Ping when updated, please

@thockin Sorry for the delay, has already been updated, my initial thought was to update after all the comments have been processed over the weekend

@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch 2 times, most recently from d5a17e0 to c1ef3d7 Compare January 28, 2024 15:14
Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

* Strict validation follows the current design, which only allows envvar names passed the regular expression `[-._a-zA-Z][-._a-zA-Z0-9]*`.

- Relaxed validation
* Relaxed verification allows all ASCII characters with numbers in the range 33-126 as envvar name, and its regular expression is `^[!-~]{1,}$`, matches string that contains ASCII characters from the `!` to the `~` and is at least 1 in length.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe describe this (or implement it) as unicode.IsPrint(r) && r < unicode.MaxASCII

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to filter out "=" from all printable ASCII characters, it will be easier to use regex.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I prefer unicode.IsPrint(r) && r < unicode.MaxASCII to a range or regex is that it's not reasonable to expect people to keep the ASCII range in their heads.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all right, I agree with your opinion.

- "@HirazawaUi"
owning-sig: sig-node
participating-sigs:
- sig-api-machinery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 29, 2024
@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch from c1ef3d7 to fd39ea5 Compare January 30, 2024 16:02
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 30, 2024
@HirazawaUi
Copy link
Contributor Author

@thockin I updated the documentation to weaken "printable ASCII characters" and emphasize using the range of decimal numbers in ASCII to describe it, may you take a look at it again?

@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch 2 times, most recently from f3056fc to 8131cc9 Compare January 30, 2024 16:30
@HirazawaUi HirazawaUi force-pushed the special-characters-environment branch from 8131cc9 to 53c565c Compare January 30, 2024 16:33
@HirazawaUi
Copy link
Contributor Author

Before, I mistakenly thought that the space character was not a printable ASCII character, but it turned out that I was wrong.
ref: https://en.wikipedia.org/wiki/ASCII#Printable_characters
And space character can also be set as an envvar name, so I re-adjusted the ASCII character range to 32-126.

@thockin
Copy link
Member

thockin commented Jan 30, 2024

I'm still LGTM to proceed on this.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 30, 2024
@HirazawaUi
Copy link
Contributor Author

HirazawaUi commented Feb 1, 2024

Since it's close to the enhancement freeze, I'll contact the maintainers of sig node & sig security on slack to review it (if they're interested).

@HirazawaUi
Copy link
Contributor Author

@liggitt Do you have any other thoughts on it?

@thockin
Copy link
Member

thockin commented Feb 3, 2024

This PR also needs to add a file in keps/prod-readiness to trigger PRR.

@thockin thockin changed the title Allow special characters environment variable Allow almost all printable ASCII characters in environment variables Feb 3, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: HirazawaUi, mrunalp, thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 3, 2024
@k8s-ci-robot k8s-ci-robot merged commit ff669dc into kubernetes:master Feb 3, 2024
4 checks passed
SIG Node PR Triage automation moved this from Waiting on Author to Done Feb 3, 2024
@k8s-ci-robot k8s-ci-robot added this to the v1.30 milestone Feb 3, 2024
@thockin
Copy link
Member

thockin commented Feb 3, 2024

@HirazawaUi You still need to trigger PRR or this will not move forward

@HirazawaUi
Copy link
Contributor Author

@HirazawaUi You still need to trigger PRR or this will not move forward

all right.

@jpbetz jpbetz mentioned this pull request Feb 5, 2024

### Monitoring Requirements

- N/A
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not N/A when going beta, GA. ok for alpha

Comment on lines +25 to +31
# The most recent milestone for which work toward delivery of this KEP has been
# done. This can be the current (upcoming) milestone, if it is being actively
# worked on.
latest-milestone: "v1.30"

# The milestone at which this feature was, or is targeted to be, at each stage.
milestone:
alpha: "v1.30"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provisional KEPs are not supposed to have code merging, versus implementable
The idea being, provisional KEPs may have part of an idea merged to iterate on but aren't agreed to implement yet. An implementable KEP would be expected to have stronger approval and would have a target release for implementing. https://github.com/kubernetes/enhancements/blob/master/keps/sig-architecture/0000-kep-process/README.md#kep-workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/kep Categorizes KEP tracking issues and PRs modifying the KEP directory lgtm "Looks good to me", indicates that a PR is ready to be merged. sig/node Categorizes an issue or PR as relevant to SIG Node. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

9 participants