fix: More consistent protected env vars#3838
Conversation
1dc7cce to
bb51053
Compare
zhming0
left a comment
There was a problem hiding this comment.
It looks mostly good, I can see the rationale. I mainly have questions to prevent me misunderstanding this
| "BUILDKITE_GIT_CHECKOUT_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLEAN_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_MIRROR_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_FETCH_FLAGS": {allowWriteFromJob: true}, |
There was a problem hiding this comment.
is this change a feature? it feels like a big expansion to plugin capabilities.
There was a problem hiding this comment.
Plugins are already able to set these, e.g. https://github.com/buildkite-plugins/git-clean-buildkite-plugin/blob/master/hooks/pre-checkout
There was a problem hiding this comment.
ah then is this change re envs that are created as part of Pipeline yaml??
There was a problem hiding this comment.
All these env vars are ignored from the pipeline YAML or build creation dialog - that was already the case. The change here is to make it clearer that some of them can be set from plugins, hooks, or the command.
There was a problem hiding this comment.
All these env vars are ignored from the pipeline YAML or build creation dialog
TIL! I thought I got it working via Pipeline yaml before.
I have doubt but all good for now 👍🏿
| // These variables cannot be overwritten by job-level environment variables or | ||
| // secrets, but some may still be set in hooks or plugins. |
There was a problem hiding this comment.
Just for my understanding, why would we disallow an env to be mutated by job env var but instead allow it to be mutated by hook and plugin?
There was a problem hiding this comment.
Might it be something worth writing down as well?
There was a problem hiding this comment.
Good question. The main question I ask is: if someone has rights to start a build and provide env vars, could an env var they set be able to bypass other restrictions like no-command-eval? And would the env var be a reasonable way for a plugin to set config. Git flags are an example. There are some shell injections possible via Git flag, so they should be rejected from the build creation dialog, but plugins should be able to adjust Git flags prior to checkout. (Plugins can also implement the checkout hook, bypassing the default checkout process entirely.)
There was a problem hiding this comment.
I've written some of this into the comment.
There was a problem hiding this comment.
Interesting, I thought someone who has rights to start a build are often the one that has code commit permission, so this permission mechanism seems quite narrow.
But thanks for clarifying it!
| // The Job API is only accessible from within the job, so allow writes | ||
| // to vars that allow write from within job. | ||
| if env.IsProtectedFromWithinJob(c) { |
There was a problem hiding this comment.
Is this change "Prevent mutating some env vars via hook wrapper"?
If I am not mistaken, we didn't add any new protected env var in this PR?
There was a problem hiding this comment.
The lines where you've commented, the change is to allow more env vars to be changed via the Job API. Previously, the git flags vars would have been rejected here, even though they can be set via the hook wrapper.
The hook wrapper and Job API should have consistent logic, since they're both doing the same thing (allowing a script or command to change env vars), so the same check is now added to the hook wrapper.
There was a problem hiding this comment.
👍🏿 yeah, my question was mainly about where is the code change for this item in PR description?
Prevent mutating some env vars via hook wrapper
There was a problem hiding this comment.
The change in executor.go (
agent/internal/job/executor.go
Lines 631 to 644 in 0aef51d
902fef3 to
0aef51d
Compare
zhming0
left a comment
There was a problem hiding this comment.
LGTM though I think it might be worth to re-clarify some names.
| "BUILDKITE_GIT_CHECKOUT_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLEAN_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_MIRROR_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_FETCH_FLAGS": {allowWriteFromJob: true}, |
There was a problem hiding this comment.
Without reading more context, the allowWriteFromJob seems to be saying allow Job environment to modify these env vars.
But the comments above say:
But we don't want the job environment to be able to set them, because git is riddled with shell injections
| "BUILDKITE_GIT_CHECKOUT_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLEAN_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_CLONE_MIRROR_FLAGS": {allowWriteFromJob: true}, | ||
| "BUILDKITE_GIT_FETCH_FLAGS": {allowWriteFromJob: true}, |
There was a problem hiding this comment.
All these env vars are ignored from the pipeline YAML or build creation dialog
TIL! I thought I got it working via Pipeline yaml before.
I have doubt but all good for now 👍🏿
| // The Job API is only accessible from within the job, so allow writes | ||
| // to vars that allow write from within job. | ||
| if env.IsProtectedFromWithinJob(c) { |
There was a problem hiding this comment.
👍🏿 yeah, my question was mainly about where is the code change for this item in PR description?
Prevent mutating some env vars via hook wrapper
5979d5a to
089b97a
Compare
There was a problem hiding this comment.
bazel run :gazelle generated this
089b97a to
eee7173
Compare
Description
Make the whole situation with env var protection clearer.
Context
https://linear.app/buildkite/issue/A-991, and others
Testing
go test ./...). Buildkite employees may check this if the pipeline has run automatically.go tool gofumpt -extra -w .)Disclosures / Credits
The wet noodles of brain tissue curling around inside my skull