Skip to content

fix: prevent ArrayIndexOutOfBoundsException in K8s ingress protocol annotation parsing - #6892

Merged
dengliming merged 7 commits into
apache:masterfrom
wy471x:fix_K8S-protocol-annotation-break-parsing
Sep 15, 2026
Merged

dengliming merged 7 commits into
apache:masterfrom
wy471x:fix_K8S-protocol-annotation-break-parsing

Conversation

@wy471x

@wy471x wy471x commented Aug 4, 2026

Copy link
Copy Markdown
Contributor
  • DivideIngressParser: add bounds check when indexing protocol array, fall back to "http://" when annotation has fewer entries than endpoint addresses
  • DubboIngressParser: fix NPE when annotation is missing, fix double-increment bug where protocols[i++] was evaluated twice in one expression
  • Add unit tests covering missing, exact, fewer, mixed, and empty annotation scenarios

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.

Summary

Bug Fixes

DivideIngressParser.java — The protocol array was indexed with protocol[i++] without any bounds check. When the shenyu.apache.org/upstreams-protocol annotation had fewer entries
than endpoint addresses, it threw ArrayIndexOutOfBoundsException. Fixed by switching to an indexed for-loop with a bounds guard (i >= protocol.length) that falls back to
"http://".

DubboIngressParser.java — Two bugs fixed:

  1. annotations.get(...).split(",") would NPE when the annotation was missing. Added null/containsKey guard, defaulting to "dubbo://".
  2. protocols[i++] was evaluated twice in the same ternary expression (Objects.isNull(protocols[i++]) ? "dubbo://" : protocols[i++]), double-incrementing i and causing both skipped
    entries and AIOOBE. Replaced with a single bounds-checked protocols[i].

Unit Tests

  • DivideIngressParserTest.java — 5 tests: annotation missing, exact match, fewer entries than addresses, mixed protocols, empty annotation.
  • DubboIngressParserTest.java — 5 tests covering the same scenarios plus the previously-NPE case.

close #6485

wy471x and others added 2 commits August 4, 2026 23:08
…nnotation parsing

- DivideIngressParser: add bounds check when indexing protocol array, fall back to "http://" when annotation has fewer entries than endpoint addresses
- DubboIngressParser: fix NPE when annotation is missing, fix double-increment bug where protocols[i++] was evaluated twice in one expression
- Add unit tests covering missing, exact, fewer, mixed, and empty annotation scenarios

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@Aias00

Aias00 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Clean fix — the double-increment in Objects.isNull(protocols[i++]) ? "dubbo://" : protocols[i++] was a classic bug (the condition's i++ always advances, and the true branch advances again, skipping elements), correctly resolved by computing upstreamProtocol once before the ternary. The AIOOBE bounds check and the annotation null-guard are both correct.

I did a variant search across the k8s parsers: WebSocketParser.java:170-192 already has the correct guard pattern (null-check + bounds-check + single evaluation), so this PR brings DivideIngressParser and DubboIngressParser in line with that reference implementation. No other parser carries the [i++] bug — SofaParser/GrpcParser don't use the annotation-based protocol array, so they're unaffected.

One minor pre-existing edge case (consistent across all parsers incl. WebSocketParser, so not a regression): when the annotation value is an empty string, "".split(",") returns [""], so the first upstream gets protocol = "" rather than the "http://"/"dubbo://" fallback — the bounds check only covers index-out-of-range, not empty entries. testEmptyProtocolAnnotation only asserts assertNotNull, sidestepping this. A StringUtils.isBlank(protocols[i]) check would make empty entries fall back to the default too. Fine to leave, just flagging for completeness.

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

Thanks @wy471x!

Both fixes are correct and close real defects:

  • DivideIngressParser: protocol[i++] with no bounds check threw ArrayIndexOutOfBoundsException whenever shenyu.apache.org/upstreams-protocol had fewer entries than endpoint addresses. The indexed loop with the i >= protocol.length fallback to http:// is the right behavior, and incrementing i per address (rather than only when a protocol was assigned) keeps the annotation entries aligned with their addresses.
  • DubboIngressParser: the old Objects.isNull(protocols[i++]) ? "dubbo://" : protocols[i++] both double-incremented i (skipping entries and eventually going out of bounds) and NPE'd when the annotation was missing. The single bounds-checked protocols[i] plus the annotations null/containsKey guard fix all three problems at once, with dubbo:// as the sensible default.

Tests cover missing/exact/fewer/mixed/empty annotation scenarios for both parsers and assert on the actual upstream protocol values, which is exactly the right level of verification.

⚠️ One coordination note (non-blocking): this PR and #6896 both add shenyu-kubernetes-controller/src/test/java/org/apache/shenyu/k8s/parser/DubboIngressParserTest.java with different test sets, so whichever merges second will need a rebase that merges the two test classes together (this PR's file also doesn't include the parseIngressRule null-safety tests from #6896). Worth reconciling before either lands.

LGTM, approving.

@dengliming
dengliming merged commit 824bcf5 into apache:master Sep 15, 2026
37 checks passed
eye-gu pushed a commit to eye-gu/shenyu that referenced this pull request Sep 30, 2026
…nnotation parsing (apache#6892)

- DivideIngressParser: add bounds check when indexing protocol array, fall back to "http://" when annotation has fewer entries than endpoint addresses
- DubboIngressParser: fix NPE when annotation is missing, fix double-increment bug where protocols[i++] was evaluated twice in one expression
- Add unit tests covering missing, exact, fewer, mixed, and empty annotation scenarios

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Co-authored-by: zhengpeng <847850277@qq.com>
Co-authored-by: Liming Deng <liming.d.pro@gmail.com>
@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] Kubernetes upstream protocol annotation can break Divide and Dubbo ingress parsing

4 participants