shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[BUG] Discovery upstream writes can mismatch handler and namespace ownership

Open sunnysabor opened this issue 3 weeks ago • 0 comments

Current behavior

DiscoveryUpstreamDTO validates that namespaceId and discoveryHandlerId each exist, but does not validate that they belong together. The write paths preserve this mismatch:

  • DiscoveryUpstreamServiceImpl.create() inserts the DTO-derived row and calls fetchAll(discoveryHandlerId).
  • createOrUpdateBatch() inserts/updates DTO-derived rows and refreshes each supplied handler.
  • updateBatch(handlerId, ...) deletes the current rows for that handler, then overwrites each DTO's handler ID with the path ID while retaining the submitted namespaceId.

fetchAll() loads upstream rows by handler ID and publishes them through the selector/proxy-selector associated with that handler. In contrast, listAllDataByNamespaceId() filters rows using the stored upstream namespace_id. A mismatched row can therefore be shown under namespace B while being synchronized as part of namespace A's handler. The batch replacement path can also remove the handler's existing rows before persisting rows tagged with the wrong namespace.

Reproduction

  1. Create namespaces A and B, and a discovery handler bound to a selector in A.
  2. Call PUT /discovery-upstream/{handlerA} with a valid upstream DTO whose namespaceId is B (all required fields and IDs are valid).
  3. Observe that the row is stored with discovery_handler_id=handlerA and namespace_id=B; the service then calls fetchAll(handlerA) and sends the handler's upstream snapshot to A's selector.
  4. Querying upstreams by namespace B also returns that row.

The same inconsistent association can be created through POST /discovery-upstream and POST /discovery-upstream/batch.

Expected behavior

The namespace must be derived from or checked against the namespace that owns the discovery handler's selector/proxy selector. Reject a mismatched write before deleting or inserting any upstream rows, and keep the persisted namespace consistent with the handler used for synchronization.

Source references

  • shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/DiscoveryUpstreamDTO.java: independent existence checks for namespaceId and discoveryHandlerId.
  • shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java: create, createOrUpdateBatch, updateBatch, and fetchAll.
  • shenyu-admin/src/main/resources/mappers/discovery-upstream-sqlmap.xml: inserts persist namespace_id; upstream lookup for synchronization is by discovery_handler_id.

This is a source-confirmed issue; I did not modify a live database or run an HTTP deployment. DiscoveryUpstreamServiceTest passes 49 tests but currently has no mismatched handler/namespace case.

sunnysabor avatar Nov 08 '26 09:11 sunnysabor