-
Notifications
You must be signed in to change notification settings - Fork 37
Fix shell injection in generated gcloud IAM binding command in Namespace Admin > Service Accounts #1427
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
Open
herdiyana256
wants to merge
2
commits into
cdapio:develop
Choose a base branch
from
herdiyana256:fix-service-account-gcloud-command-injection
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix shell injection in generated gcloud IAM binding command in Namespace Admin > Service Accounts #1427
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
app/cdap/components/NamespaceAdmin/ServiceAccounts/__tests__/gcloudCommand.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright © 2026 Cask Data, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
| import { execSync } from 'child_process'; | ||
| import { getGcloudCommand, shellQuote } from 'components/NamespaceAdmin/ServiceAccounts/gcloudCommand'; | ||
|
|
||
| describe('shellQuote', () => { | ||
| // Round-trips a set of values through a real shell (printf) to confirm the | ||
| // quoted form is both safe (no injected command runs) and lossless (the shell | ||
| // sees exactly the original string as data, not as executed syntax). | ||
| test('round-trips arbitrary values through a real shell unexecuted', () => { | ||
| const values = [ | ||
| 'x@x.iam.gserviceaccount.com; touch /tmp/should-not-exist; echo done', | ||
| '$(touch /tmp/should-not-exist)', | ||
| '`touch /tmp/should-not-exist`', | ||
| "a'; touch /tmp/should-not-exist; echo '", | ||
| 'a && touch /tmp/should-not-exist', | ||
| 'plain-safe-value', | ||
| ]; | ||
|
|
||
| for (const value of values) { | ||
| const quoted = shellQuote(value); | ||
| const out = execSync(`printf '%s' ${quoted}`).toString(); | ||
| expect(out).toBe(value); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('getGcloudCommand', () => { | ||
| // Regression test: gsaEmail (sourced from the stored, server-side-unvalidated | ||
| // "serviceAccount" value) used to be interpolated into the generated command with | ||
| // no quoting at all, so a value containing shell metacharacters would execute as | ||
| // additional commands if an admin copy-pasted the generated string into a | ||
| // terminal, per the component's own "copy to clipboard" affordance. | ||
| test('a malicious gsaEmail cannot inject additional shell commands', () => { | ||
| const command = getGcloudCommand({ | ||
| identity: 'my-namespace-identity', | ||
| gsaEmail: 'x@x.iam.gserviceaccount.com; touch /tmp/should-not-exist; echo pwned', | ||
| k8snamespace: 'default', | ||
| k8sWorkloadIdentityPool: 'example-project.svc.id.goog', | ||
| }); | ||
|
|
||
| let threw = false; | ||
| try { | ||
| execSync(command, { stdio: 'pipe' }); | ||
| } catch (e) { | ||
| // gcloud isn't installed in the test environment -- that's expected and fine, | ||
| // what matters is that nothing after it ran as a separate command. | ||
| threw = true; | ||
| } | ||
| expect(threw).toBe(true); | ||
|
|
||
| const fs = require('fs'); | ||
| expect(fs.existsSync('/tmp/should-not-exist')).toBe(false); | ||
| }); | ||
|
|
||
| test('unsupplied parameters keep their literal, shell-expandable placeholder form', () => { | ||
| const command = getGcloudCommand({}); | ||
| expect(command).toContain('${TENANT_PROJECT_ID}'); | ||
| expect(command).toContain('${IDENTITY}'); | ||
| expect(command).toContain('${GSA_EMAIL}'); | ||
| expect(command).toContain('${GSA_PROJECT_ID}'); | ||
| }); | ||
|
|
||
| test('supplied parameters are individually quoted in the --member value', () => { | ||
| const command = getGcloudCommand({ | ||
| identity: 'my-identity', | ||
| k8snamespace: 'my-ns', | ||
| k8sWorkloadIdentityPool: 'my-pool', | ||
| }); | ||
| expect(command).toContain("--member serviceAccount:'my-pool'['my-ns'/'my-identity']"); | ||
| }); | ||
| }); | ||
68 changes: 68 additions & 0 deletions
68
app/cdap/components/NamespaceAdmin/ServiceAccounts/gcloudCommand.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright © 2026 Cask Data, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Quotes a value for safe use as a single POSIX shell word, so it can't be | ||
| * interpreted as additional shell syntax (metacharacters, command substitution, a | ||
| * new command after a `;`/`&&`/`|`, etc.) no matter what it contains. The | ||
| * `serviceAccount` value this command is built from is stored server-side with no | ||
| * format validation on the create path, so it must be treated as untrusted here. | ||
| */ | ||
| export const shellQuote = (value: string): string => `'${String(value).replace(/'/g, `'\\''`)}'`; | ||
|
|
||
| interface IGcloudCommandParams { | ||
| k8sWorkloadIdentityPool?: string; | ||
| identity?: string; | ||
| gsaEmail?: string; | ||
| gsaProjectId?: string; | ||
| k8snamespace?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Generates the gcloud cli command to add an IAM policy binding. If any of the | ||
| * parameters for the command is not provided when the command is generated, then | ||
| * the user should be able to provide them as environment variables in their shell. | ||
| * | ||
| * @param tenantProjectId string, defaults to "${TENANT_PROJECT_ID}" so it can be | ||
| * provided as the environment variable TENANT_PROJECT_ID when | ||
| * the command is run | ||
| * @param identity string, defaults to "${IDENTITY}" so that it can be provided as the | ||
| * environment variable IDENTITY when the command is run | ||
| * @param gsaEmail string, defaults to "${GSA_EMAIL}" so that it can be provided as the | ||
| * environment variable GSA_EMAIL when the command is run | ||
| * @return string, the gcloud cli command to run | ||
| */ | ||
| export const getGcloudCommand = ({ | ||
| k8sWorkloadIdentityPool, | ||
| identity, | ||
| gsaEmail, | ||
| gsaProjectId, | ||
| k8snamespace, | ||
| }: IGcloudCommandParams): string => { | ||
| // Real values are quoted so they can never break out of their argument position. | ||
| // The "${...}" fallbacks are meant to stay as literal, unquoted shell syntax so the | ||
| // user's own shell substitutes them from an environment variable when they run the | ||
| // command, per this function's own doc comment above. | ||
| const pool = k8sWorkloadIdentityPool | ||
| ? shellQuote(k8sWorkloadIdentityPool) | ||
| : '${TENANT_PROJECT_ID}.svc.id.goog'; | ||
| const ns = shellQuote(k8snamespace || 'default'); | ||
| const id = identity ? shellQuote(identity) : '${IDENTITY}'; | ||
| const email = gsaEmail ? shellQuote(gsaEmail) : '${GSA_EMAIL}'; | ||
| const projectId = gsaProjectId ? shellQuote(gsaProjectId) : '${GSA_PROJECT_ID}'; | ||
|
|
||
| return `gcloud iam service-accounts add-iam-policy-binding --role roles/iam.workloadIdentityUser --member serviceAccount:${pool}[${ns}/${id}] ${email} --project ${projectId}`; | ||
|
herdiyana256 marked this conversation as resolved.
Outdated
|
||
| }; | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the test assertion to expect the escaped brackets (
\[and\]) to match the updated command generation logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@herdiyana256 please update this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in a70e917 — the assertion now expects the escaped brackets:
--member serviceAccount:'my-pool'\['my-ns'/'my-identity']. Also added tests covering the bracket escaping, the discard-on-invalid-email behavior, and theisValidServiceAccountEmailvalidator.