Visitar URL original
fix(iam): handle undefined policy bindings in Cloud Run and Storage helpers by reginaldalfret · Pull Request #335 · GoogleCloudPlatform/cloud-run-mcp · GitHub
Skip to content

fix(iam): handle undefined policy bindings in Cloud Run and Storage helpers - #335

Open
reginaldalfret wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/cloud-run-mcp-regression
Open

reginaldalfret wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/cloud-run-mcp-regression

Conversation

@reginaldalfret

Copy link
Copy Markdown

Summary

Safely handle undefined bindings and members when retrieving IAM policies in Cloud Run (setServicePublicAccess) and Cloud Storage (grantBucketAccess) helpers.

Root cause

In Google Cloud client libraries, getIamPolicy and bucket.iam.getPolicy return a policy object where the bindings array is omitted (undefined) when a newly created Cloud Run service or Cloud Storage bucket has no custom IAM bindings.

Previously, setServicePublicAccess in lib/cloud-api/run.js and grantBucketAccess in lib/cloud-api/storage.js directly called:

const binding = policy.bindings.find((b) => b.role === role);

without first verifying that policy or policy.bindings is defined. This causes an unhandled TypeError: Cannot read properties of undefined (reading 'find'). In addition, if a binding exists without initialized members, calling .includes() directly raises TypeError: Cannot read properties of undefined (reading 'includes').

This failure directly interrupts Cloud Run deployments whenever public access (skipIamCheck) is requested or when setting bucket permissions for deployments. In contrast, lib/cloud-api/secrets.js already guarded policy.bindings = policy.bindings || [].

Implementation

  1. In lib/cloud-api/run.js:
    • Initialize policy = res || {} and policy.bindings = policy.bindings || [].
    • Ensure invokerBinding.members = invokerBinding.members || [] before calling .includes().
  2. In lib/cloud-api/storage.js:
    • Initialize policy = res || {} and policy.bindings = policy.bindings || [].
    • Guard binding.members before calling .includes() and initialize binding.members = binding.members || [] when updating an existing role binding.
  3. Added unit tests:
    • In test/local/cloud-api/run.test.js: added test asserting setServicePublicAccess successfully handles an empty IAM policy [{}] without bindings.
    • In test/local/cloud-api/storage.test.js: added test asserting grantBucketAccess successfully handles an empty IAM policy [{}] without bindings.

Tests

  • Ran node --test test/local/cloud-api/run.test.js (3 passed, 0 failed).
  • Ran node --test test/local/cloud-api/storage.test.js (14 passed, 0 failed).
  • Ran npx prettier --check lib/cloud-api/run.js lib/cloud-api/storage.js test/local/cloud-api/run.test.js test/local/cloud-api/storage.test.js (clean).

…elpers

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves robustness when retrieving and updating IAM policies in Cloud Run and Cloud Storage helpers by safely handling missing policies, bindings, or members. Corresponding unit tests have been added to verify these scenarios. Feedback on these changes suggests simplifying the conditional logic for initializing invokerBinding.members in run.js and utilizing optional chaining (?.) to streamline the member check in storage.js.

Comment thread lib/cloud-api/run.js
Comment on lines 299 to 304
if (!invokerBinding) {
invokerBinding = { role: INVOKER_ROLE, members: [] };
policy.bindings.push(invokerBinding);
} else if (!invokerBinding.members) {
invokerBinding.members = [];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The conditional structure can be simplified. By moving the initialization of invokerBinding.members outside of the if block, we can eliminate the else if branch and make the code more concise and readable.

      if (!invokerBinding) {
        invokerBinding = { role: INVOKER_ROLE, members: [] };
        policy.bindings.push(invokerBinding);
      }
      invokerBinding.members = invokerBinding.members || [];

Comment thread lib/cloud-api/storage.js Outdated
// Check if member already has the role
const binding = policy.bindings.find((b) => b.role === role);
if (binding && binding.members.includes(member)) {
if (binding && binding.members && binding.members.includes(member)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This conditional check can be simplified using optional chaining (?.), which is more concise and idiomatic in modern JavaScript.

Suggested change
if (binding && binding.members && binding.members.includes(member)) {
if (binding?.members?.includes(member)) {

…l chaining

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
@reginaldalfret

Copy link
Copy Markdown
Author

Applied the review suggestions:

  1. Simplified invokerBinding.members initialization in lib/cloud-api/run.js by removing the else if branch and handling fallback assignment unconditionally after ensuring the binding object exists.
  2. Adopted optional chaining (�inding?.members?.includes(member)) in lib/cloud-api/storage.js.
  3. Verified all local unit tests pass and code formatting conforms to Prettier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant