Skip to content

fix(admin): reject cross-namespace updates of namespace plugin relations - #7526

Merged
Aias00 merged 1 commit into
apache:masterfrom
Sean-Walker0:fix/namespace-plugin-update-namespace-check
Oct 10, 2026
Merged

Aias00 merged 1 commit into
apache:masterfrom
Sean-Walker0:fix/namespace-plugin-update-namespace-check

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Modifications

NamespacePluginServiceImpl#update loaded the relation by id (namespacePluginRelMapper.selectById) and wrote the DTO over it without comparing namespaces, so a request carrying another namespace's namespaceId could rewrite the relation (config / enabled / sort) — and, because the replacement row is rebuilt from the DTO, even move the relation into a different namespace.

The loaded relation is now validated before any write, using the Assert style of the sibling create path:

Assert.notNull(before, "Namespace plugin relation does not exist: " + namespacePluginDTO.getId());
Assert.isTrue(Objects.equals(before.getNamespaceId(), namespacePluginDTO.getNamespaceId()),
        "Namespace plugin relation does not belong to namespace: " + namespacePluginDTO.getNamespaceId());

Both failures reject the update with ValidFailException inside the @Transactional boundary. Part of the same namespace-consistency family as #7514 (create/update of proxy selector discovery) and #7524/#7525 (batch enable scoping), which started from the review follow-up on #7514.

Verifying this change

  • New NamespacePluginServiceTest cases: testUpdateRejectsRelationFromAnotherNamespace and testUpdateRejectsUnknownRelation — both red on the pre-fix code (the update proceeded and returned UPDATE_SUCCESS), green after; testUpdateAcceptsRelationFromSameNamespace guards the success path including the published onUpdated(now, before) event.
  • ./mvnw -pl shenyu-admin -am test -B -Dtest=NamespacePluginServiceTest — 8/8 green (5 pre-existing cases unchanged), module checkstyle clean.

Notes

  • Behavior change: an update whose DTO namespace does not match the stored relation now fails instead of silently rewriting/moving the relation; the previously unhandled missing-relation case (selectById == null) now fails cleanly instead of proceeding with a null before-state.
  • Orthogonality: OSPP2: Aggregate remote MCP tools in Agent Gateway #7512 (open) touches other methods of this class (agent-gateway MCP tool aggregation); its diff was compared line by line — the update method appears only as unchanged context, so the hunks do not overlap.

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true (module-scoped: shenyu-admin targeted test class + checkstyle green; full-module suite left to CI as with the other admin PRs).

update() loaded the relation by id and wrote the DTO over it without
comparing namespaces, so a request carrying another namespace's
namespaceId could rewrite the relation (config, enabled, sort) and even
move it to a different namespace via the rebuilt rel row.

Assert the loaded relation exists and belongs to the request
namespace before any write. Regression tests cover the
cross-namespace rejection, the unknown id rejection (both red on the
previous code), and the same-namespace success path.

@Aias00 Aias00 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.

Summary

Tightens tenant isolation on NamespacePluginServiceImpl.update by validating that the
relation being updated actually belongs to the request namespace, reusing the already-fetched
before row (no extra DB query) — consistent with the validate-before-write pattern.

Verification

  • java.util.Objects is already imported (line 58); Assert.notNull (line 42) and
    Assert.isTrue (line 82) both exist in org.apache.shenyu.admin.utils.Assert.
  • Checkstyle clean in CI. The pr_build failure is infra only: an mvnd daemon crash
    (DaemonException$StaleAddressException: No message received within 3000ms, daemon may have crashed) mid-build, after shenyu-common tests already passed. Not a code regression — please rerun.
  • Tests cover all three branches: foreign namespace -> rejected, unknown id -> rejected,
    same namespace -> updated, and updateSelective is verified never() called on rejection paths.

Non-blocking suggestion

If both before.namespaceId and the DTO namespaceId are null the guard passes; acceptable
given namespace is non-null by invariant, but an explicit null-namespace reject could be added
for defence in depth.

Conclusion

Approved. Nice, minimal hardening with solid test coverage.

@Aias00
Aias00 merged commit 75d4241 into apache:master Oct 10, 2026
58 of 60 checks passed

@wy471x wy471x 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.

Verified on the branch (JDK 21): ./mvnw -o -pl shenyu-admin -am test -Dtest=NamespacePluginServiceTest -> 8/8 green, module checkstyle clean. The validation is placed before any write, the relation cannot be moved to another namespace any more (the replacement row is still built from the DTO, but the equality assert makes that the same namespace id), and the previously unhandled missing-relation case fails cleanly instead of proceeding with a null before-state.

Assert.isTrue throws ValidFailException, which extends ShenyuAdminException and is therefore handled by ExceptionHandlers#handleShenyuException -> ShenyuAdminResult.error(message); the test asserting ShenyuAdminException matches that. Note the HTTP path already rejects an unknown id earlier (@Existed(provider = NamespacePluginRelMapper.class) on the path variable), so the notNull assert is defence in depth for non-HTTP callers — reasonable.

Non-blocking notes:

  1. Same family, still open: NamespacePluginServiceImpl#delete(List<String> ids) is invoked by DELETE /namespace/plugin/batch with a BatchNamespaceCommonDTO that carries namespaceId, but the controller passes only getIds() and the service ignores the namespace entirely — so a caller can still delete another namespace's relations by id, and that path cascades through namespacePluginEventPublisher.onDeleted. If this PR is the namespace-consistency sweep, that one looks like the next candidate (a follow-up PR/issue, not a blocker for this change).
  2. Both new messages interpolate the caller-supplied id / namespace id and are returned to the caller verbatim by handleShenyuException. Nothing sensitive leaks here, only flagging that these strings are caller-visible API output.
  3. Nit: the new org.apache.shenyu.admin.utils.Assert import is inserted between admin.exception.ShenyuAdminException and admin.mapper.NamespacePluginRelMapper, out of the alphabetical order used by the rest of the block (cosmetic only — CustomImportOrder is disabled in script/shenyu_checkstyle.xml). The three imports added to the test class have the same ordering nit.
  4. The three new tests cover reject-foreign / reject-missing / accept-same well; the accept case's thenReturn(before, after) correctly models the re-read after the update.

@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
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.

3 participants