Skip to content

Restrict index access when cargo_auth_required is set - #71

Merged
dralley merged 1 commit into
pulp:mainfrom
BaptisteCentreon:private-cargo-index
Nov 24, 2026
Merged

dralley merged 1 commit into
pulp:mainfrom
BaptisteCentreon:private-cargo-index

Conversation

@pkriko

@pkriko pkriko commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

📜 Checklist

  • Commits are cleanly separated with meaningful messages (simple features and bug fixes should be squashed to one commit)
  • A changelog entry or entries has been added for any significant changes
  • Follows the Pulp policy on AI Usage
  • (For new features) - User documentation and test coverage has been added

Follow-up of #61, as discussed there: when cargo_auth_required is set, reading the index requires the view permission on the distribution.

  • config.json, the sparse index and the download endpoint require rust.view_rustdistribution on the distribution (e.g. the rust.rustdistribution_viewer role).
  • Anonymous requests get a 401 with WWW-Authenticate: Cargo, so Cargo retries with its credential; authenticated users without the permission get a 403.
  • The index views accept the same credentials as the other Cargo endpoints (Cargo token first, then the instance's authenticators), and a Cargo token scoped to other distributions cannot read this one.
  • The check runs in the views, not in the access policies, so a customized access policy cannot bypass it.
  • Not covered: the download endpoint redirects to the content app, so the .crate files still need a content guard. Happy to discuss how to handle that in a separate PR.

See: Pull Request Walkthrough

@pkriko

pkriko commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Hey @dralley can I have a review when it's still fresh in my mind ? :)

@dralley

dralley commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Yup. Looking

Comment thread docs/user/guides/authentication.md Resolved
Comment thread pulp_rust/app/views.py Outdated
"action": ["retrieve"],
"principal": "*",
"effect": "allow",
"condition": "can_read_index",

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.

Given that this is now it's own separate condition, and that in theory users can customize the access policies, it might be a good idea to document that any custom access policies should take this into consideration, OR else just keep it separate from the access policy system.

@gerrod3 Your opinion here would be welcome as you have more experience with access policies.

Unless we forsee a need to customize rules around index access, I'm slightly partial to "keep it separate", which I think would look something like this.

diff --git a/pulp_rust/app/global_access_conditions.py b/pulp_rust/app/global_access_conditions.py
index 6317f14..08c71cf 100644
--- a/pulp_rust/app/global_access_conditions.py
+++ b/pulp_rust/app/global_access_conditions.py
@@ -32,8 +32,3 @@ def can_read_distribution(request, distribution):
     if user.has_perm(permission) or user.has_perm(permission, distribution):
         return True
     return settings.DOMAIN_ENABLED and user.has_perm(permission, obj=request.pulp_domain)
-
-
-def can_read_index(request, view, action):
-    """Access condition for the Cargo index views, which expose their distribution."""
-    return can_read_distribution(request, view.distribution)
diff --git a/pulp_rust/app/views.py b/pulp_rust/app/views.py
index 5bec56d..1c40a51 100644
--- a/pulp_rust/app/views.py
+++ b/pulp_rust/app/views.py
@@ -65,6 +65,15 @@ def cargo_error(detail, status=400):
     )
 
 
+def require_distribution_read(request, distribution):
+    """Raise the appropriate response when a request cannot read a distribution."""
+    if can_read_distribution(request, distribution):
+        return
+    if request.user.is_authenticated:
+        raise PermissionDenied()
+    raise NotAuthenticated()
+
+
 def repository_write_error(distro):
     """Return a Cargo error response if the distribution can't be written to, else None.
 
@@ -175,6 +184,7 @@ class ApiMixin:
     def initial(self, request, *args, **kwargs):
         """Perform common initialization tasks for API endpoints."""
         super().initial(request, *args, **kwargs)
+        require_distribution_read(request, self.distribution)
         domain_name = get_domain().name
         repo = self.kwargs["repo"]
         if settings.DOMAIN_ENABLED:
@@ -202,7 +212,6 @@ class CargoIndexApiViewSet(CargoAuthMixin, ApiMixin, ViewSet):
                 "action": ["retrieve"],
                 "principal": "*",
                 "effect": "allow",
-                "condition": "can_read_index",
             },
         ],
     }
@@ -330,7 +339,6 @@ class IndexRoot(CargoAuthMixin, ApiMixin, ViewSet):
                 "action": ["retrieve"],
                 "principal": "*",
                 "effect": "allow",
-                "condition": "can_read_index",
             },
         ],
     }
@@ -505,10 +513,7 @@ class CargoDownloadApiView(CargoAuthMixin, APIView):
         - api/v1/crates/{name}/{version}/readme - not yet implemented
         """
         distro = self.get_distribution()
-        if not can_read_distribution(request, distro):
-            if request.user.is_authenticated:
-                raise PermissionDenied()
-            raise NotAuthenticated()
+        require_distribution_read(request, distro)
 
         if rest == "download":
             relative_path = f"{name}/{name}-{version}.crate"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have no opinion on this. In pulp-python we have custom access policy conditions for reading the python index that users can modify. https://pulpproject.org/pulp_python/docs/admin/guides/rbac/#index-specific-access-conditions.

When cargo_auth_required is set on a distribution, the index
config.json, the sparse index files and the download endpoint now
require the rust.view_rustdistribution permission on the distribution.
Anonymous requests get a 401 with a "WWW-Authenticate: Cargo"
challenge, so Cargo retries with its credential. A Cargo token scoped
to other distributions cannot read this one.

The check runs in the views rather than in the access policies, so a
customized access policy cannot bypass it.

The index views now accept the same credentials as the other Cargo
endpoints: the Cargo token first, then the instance's default
authentication classes.

The download endpoint redirects to the content app, which this does
not cover; a content guard is still needed to protect the crate files.

Assisted By: Claude Opus 5.5
@pkriko
pkriko force-pushed the private-cargo-index branch from a15cb10 to 3b8d4e7 Compare November 24, 2026 15:48
@pkriko pkriko changed the title Reject anonymous reads of private Cargo registries Restrict index access when cargo_auth_required is set Nov 24, 2026
@pkriko

pkriko commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thank you both of you for your reviews 👍

I had address the request changes.

@dralley
dralley merged commit 4406589 into pulp:main Nov 24, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants