Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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.Objectsis already imported (line 58);Assert.notNull(line 42) and
Assert.isTrue(line 82) both exist inorg.apache.shenyu.admin.utils.Assert.- Checkstyle clean in CI. The
pr_buildfailure 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, andupdateSelectiveis verifiednever()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.
wy471x
left a comment
There was a problem hiding this comment.
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:
- Same family, still open:
NamespacePluginServiceImpl#delete(List<String> ids)is invoked byDELETE /namespace/plugin/batchwith aBatchNamespaceCommonDTOthat carriesnamespaceId, but the controller passes onlygetIds()and the service ignores the namespace entirely — so a caller can still delete another namespace's relations by id, and that path cascades throughnamespacePluginEventPublisher.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). - 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. - Nit: the new
org.apache.shenyu.admin.utils.Assertimport is inserted betweenadmin.exception.ShenyuAdminExceptionandadmin.mapper.NamespacePluginRelMapper, out of the alphabetical order used by the rest of the block (cosmetic only —CustomImportOrderis disabled inscript/shenyu_checkstyle.xml). The three imports added to the test class have the same ordering nit. - 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.
Modifications
NamespacePluginServiceImpl#updateloaded the relation by id (namespacePluginRelMapper.selectById) and wrote the DTO over it without comparing namespaces, so a request carrying another namespace'snamespaceIdcould 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
Assertstyle of the siblingcreatepath:Both failures reject the update with
ValidFailExceptioninside the@Transactionalboundary. 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
NamespacePluginServiceTestcases:testUpdateRejectsRelationFromAnotherNamespaceandtestUpdateRejectsUnknownRelation— both red on the pre-fix code (the update proceeded and returnedUPDATE_SUCCESS), green after;testUpdateAcceptsRelationFromSameNamespaceguards the success path including the publishedonUpdated(now, before)event../mvnw -pl shenyu-admin -am test -B -Dtest=NamespacePluginServiceTest— 8/8 green (5 pre-existing cases unchanged), module checkstyle clean.Notes
selectById == null) now fails cleanly instead of proceeding with anullbefore-state.updatemethod appears only as unchanged context, so the hunks do not overlap.Make sure that:
./mvnw clean install -Dmaven.javadoc.skip=true(module-scoped:shenyu-admintargeted test class + checkstyle green; full-module suite left to CI as with the other admin PRs).