Skip to content

[type:fix] guard the rule handle of the general context plugin - #7167

Merged
Aias00 merged 4 commits into
apache:masterfrom
HY-love-sleep:fix/general-context-null-handle
Sep 23, 2026
Merged

Aias00 merged 4 commits into
apache:masterfrom
HY-love-sleep:fix/general-context-null-handle

Conversation

@HY-love-sleep

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6657. GeneralContextPlugin.doExecute dereferenced the rule handle map without a null check:

Map<String, List<GeneralContextHandle>> generalContextHandleMap = GeneralContextPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
if (generalContextHandleMap.isEmpty()) {

CommonHandleCache.obtainHandle returns null on a miss, so a rule that reaches the plugin without a cached
handle makes it throw a NullPointerException — the request fails instead of continuing through the chain.
Every other plugin of this set guards its handle before using it (ContextPathPlugin, RewritePlugin, ...),
this one was the exception.

What changed

// the rule may have no cached handle, the cache returns null on a miss
if (Objects.isNull(generalContextHandleMap) || generalContextHandleMap.isEmpty()) {
    return chain.execute(exchange);
}

A rule without a cached handle now passes through the chain untouched, exactly like a rule with an empty
handle map does today.

Testing

./mvnw -pl shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-general-context -am test

BUILD SUCCESS, checkstyle clean, GeneralContextPluginTest 4/4 including the new case: with no cached handle
for the rule the request reaches the chain unchanged and no general context attribute is added to the exchange.

HY-love-sleep and others added 2 commits September 22, 2026 14:49
CommonHandleCache.obtainHandle returns null on a miss, so a rule without a cached handle made
doExecute dereference it and fail the request with a NullPointerException instead of passing it
through the chain. The other plugins of this set already guard their handle this way.

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

APPROVE — the NPE is real and the guard is the right fix.

Verified against CommonHandleCache on master:

public V obtainHandle(final K key) {
    return cached.get(key);          // ConcurrentHashMap#get -> null on a miss
}

public void cachedHandle(final K key, final V value) {
    Optional.ofNullable(key).ifPresent(k -> Optional.ofNullable(value).ifPresent(v -> cached.put(k, v)));
}

So obtainHandle genuinely returns null when no handle was ever cached for the rule, and cachedHandle never stores a null — meaning the previous generalContextHandleMap.isEmpty() dereferenced a null map. GeneralContextPluginDataHandler only caches when the rule carries a handle, so a rule registered without one (or one whose handle failed to parse into a map) hits this path directly. Adding the null check is correct, and handleSelectorIfNull-style pass-through (chain.execute) is the right behaviour for "no general-context configuration".

The regression test does the right thing too: it removes the handle from the cache rather than mocking the cache, then asserts the chain is still invoked once and no GENERAL_CONTEXT attribute is set — that would fail before the fix with an NPE.

One thing worth considering separately (not blocking): obtainHandle(key) returning null is a footgun that every caller has to remember to null-check, and several other plugins call it. Returning an empty collection (or adding an obtainHandleOrDefault) would remove the whole class of NPE rather than patching each call site. Up to you whether to fold that in here or file it.

@Aias00
Aias00 merged commit 9b16d97 into apache:master Sep 23, 2026
22 checks passed
@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.

[BUG] GeneralContextPlugin NPE on null cached handle

3 participants