apache / apache/shenyu

[BUG] Mock plugin fails requests when httpStatusCode is missing or invalid

Open
#6,501 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

### Search before asking

- [x] I had searched in the [issues](https://github.com/apache/shenyu/issues) and found no similar issues.

### Apache ShenYu Component

shenyu-plugin

### What happened

`MockPlugin` assumes every cached mock rule has a valid `httpStatusCode`:

```java
MockHandle mockHandle = MockPluginHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
...
exchange.getResponse().setStatusCode(HttpStatus.valueOf(mockHandle.getHttpStatusCode()));
```

But `MockHandle.httpStatusCode` is nullable and has no model-level default:

```java
private Integer httpStatusCode;
```

The admin plugin handle metadata also marks this field as not required while only providing a UI/default metadata value:

```sql
INSERT INTO plugin_handle ... 'httpStatusCode' ... '{"required":"0","defaultValue":"200","rule":""}'
```

So a rule synced or saved with a missing `httpStatusCode` produces a `NullPointerException` from unboxing in `HttpStatus.valueOf(...)`. An out-of-range value such as `999` produces `IllegalArgumentException`. In both cases the mock rule cannot return its configured response and the request fails with a gateway error instead of a controlled mock response or a validation error when the rule is created.

### Expected behavior

Mock rules should not be able to crash request processing because of an absent/invalid status code. Either:

- validate `httpStatusCode` as required and within a valid HTTP status range when the rule is saved/synced, or
- default missing values to `200` and reject/normalize invalid values before calling `HttpStatus.valueOf(...)`.

### How to reproduce

1. Enable the mock plugin.
2. Create or sync a mock rule whose handle omits `httpStatusCode`, for example:

```json
{"responseContent":"{\"ok\":true}"}
```

or uses an invalid code:

```json
{"httpStatusCode":999,"responseContent":"{\"ok\":true}"}
```

3. Send a request matching the mock rule.
4. The request fails because `MockPlugin` calls `HttpStatus.valueOf(mockHandle.getHttpStatusCode())` without a null/range check.

### Debug logs

_No response_

### Environment

Current `master` branch.

### Are you willing to submit a PR?

- [ ] Yes I am willing to submit a PR!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in MockPlugin at the call to HttpStatus.valueOf(mockHandle.getHttpStatusCode()), then trace MockHandle and MockPluginHandler.CACHED_HANDLE to see where mock rules are saved or synced. Check the plugin_handle metadata and reproduce with a missing or 999 status; done means request processing no longer crashes and invalid values are handled before this call.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.