-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(jsonrpc): add resource restrict for jsonrpc #6728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
317787106
wants to merge
21
commits into
tronprotocol:develop
Choose a base branch
from
317787106:hotfix/restrict_jsonrpc_size
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b32fa58
restrict batch size and response size of jsonrpc
317787106 02a588f
add node.jsonrpc.maxAddressSize and node.jsonrpc.maxRequestTimeout
317787106 eff49b9
update comment
317787106 19217b8
add jsonRpcMaxBatchSize default as 10
317787106 0351c22
remove timeout restrict
317787106 acd51cb
remove input restrict
317787106 fa87c7e
optimize ContentType when writeJsonRpcError
317787106 01da427
add error code -32700
317787106 7b2585d
add getReader for CachedBodyRequestWrapper; set jsonRpcMaxBatchSize d…
317787106 a0f51f8
add getWriter() for BufferedResponseWrapper
317787106 38edfda
use overflow to replace exception
317787106 e70ea6b
reuse the PrintWriter
317787106 bf9a5a1
fix default maxResponseSize
317787106 9eb44ce
optimize JsonRpcServlet
317787106 d65f064
don't invoke getInputStream and getReader in HttpServletRequestWrappe…
317787106 801e7ae
add testcase of JsonRpcServlet and CachedBodyRequestWrapper
317787106 2bb35a8
format code of testcase
317787106 d09d720
Merge branch 'develop' into hotfix/restrict_jsonrpc_size
317787106 4bdc025
reject empty batch; use StandardCharsets.UTF_8
317787106 fd7fdf9
reorganize the package import
317787106 91a2f12
Merge branch 'develop' into hotfix/restrict_jsonrpc_size
317787106 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
framework/src/main/java/org/tron/core/services/filter/BufferedResponseWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package org.tron.core.services.filter; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.PrintWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import javax.servlet.ServletOutputStream; | ||
| import javax.servlet.WriteListener; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpServletResponseWrapper; | ||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * Buffers the response body without writing to the underlying response, | ||
| * so the caller can replay it after the handler returns. | ||
| * | ||
| * <p>If {@code maxBytes > 0} and the response would exceed that limit, the | ||
| * {@link #isOverflow()} flag is set instead of throwing. The caller should check this flag after | ||
| * the handler returns and write its own error response when true. | ||
| * | ||
| * <p>Header-mutating methods ({@code setStatus}, {@code setContentType}) are buffered here and | ||
| * only forwarded to the real response via {@link #commitToResponse()}. | ||
| */ | ||
| public class BufferedResponseWrapper extends HttpServletResponseWrapper { | ||
|
|
||
| private final HttpServletResponse actual; | ||
| private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
| private final int maxBytes; | ||
| private int status = HttpServletResponse.SC_OK; | ||
| private String contentType; | ||
| private boolean committed = false; | ||
| @Getter | ||
| private volatile boolean overflow = false; | ||
|
|
||
| private final ServletOutputStream outputStream = new ServletOutputStream() { | ||
| @Override | ||
| public void write(int b) { | ||
| if (overflow) { | ||
| return; | ||
| } | ||
| if (maxBytes > 0 && buffer.size() >= maxBytes) { | ||
| markOverflow(); | ||
| return; | ||
| } | ||
| buffer.write(b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) { | ||
| if (overflow) { | ||
| return; | ||
| } | ||
| if (maxBytes > 0 && buffer.size() + len > maxBytes) { | ||
| markOverflow(); | ||
| return; | ||
| } | ||
| buffer.write(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isReady() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void setWriteListener(WriteListener writeListener) { | ||
| } | ||
| }; | ||
|
|
||
| private final PrintWriter writer = | ||
| new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true); | ||
|
|
||
| /** | ||
| * @param response the wrapped response | ||
| * @param maxBytes max allowed response bytes; {@code 0} means no limit | ||
| */ | ||
| public BufferedResponseWrapper(HttpServletResponse response, int maxBytes) { | ||
| super(response); | ||
| this.actual = response; | ||
| this.maxBytes = maxBytes; | ||
| } | ||
|
|
||
| private void markOverflow() { | ||
| overflow = true; | ||
| buffer.reset(); | ||
| } | ||
|
|
||
| /** | ||
| * Early-detection path: if the framework reports the full content length before writing any | ||
| * bytes, we can flag overflow without buffering anything. | ||
| */ | ||
| @Override | ||
| public void setContentLength(int len) { | ||
| if (maxBytes > 0 && len > maxBytes) { | ||
| markOverflow(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setContentLengthLong(long len) { | ||
| if (maxBytes > 0 && len > maxBytes) { | ||
| markOverflow(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int getStatus() { | ||
| return this.status; | ||
| } | ||
|
|
||
| @Override | ||
| public void setStatus(int sc) { | ||
|
317787106 marked this conversation as resolved.
|
||
| this.status = sc; | ||
| } | ||
|
|
||
| @Override | ||
| public void setHeader(String name, String value) { | ||
| if ("content-length".equalsIgnoreCase(name)) { | ||
| try { | ||
| setContentLengthLong(Long.parseLong(value)); | ||
| } catch (NumberFormatException ignored) { | ||
| // malformed value, skip overflow check | ||
| } | ||
| } else { | ||
| super.setHeader(name, value); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addHeader(String name, String value) { | ||
| if ("content-length".equalsIgnoreCase(name)) { | ||
| try { | ||
| setContentLengthLong(Long.parseLong(value)); | ||
| } catch (NumberFormatException ignored) { | ||
| // malformed value, skip overflow check | ||
| } | ||
| } else { | ||
| super.addHeader(name, value); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setContentType(String type) { | ||
| this.contentType = type; | ||
| } | ||
|
|
||
| @Override | ||
| public ServletOutputStream getOutputStream() { | ||
| return outputStream; | ||
| } | ||
|
|
||
| @Override | ||
| public PrintWriter getWriter() { | ||
| return writer; | ||
| } | ||
|
|
||
| public void commitToResponse() throws IOException { | ||
|
317787106 marked this conversation as resolved.
|
||
| if (committed) { | ||
| throw new IllegalStateException("commitToResponse() already called"); | ||
| } | ||
| committed = true; | ||
| if (contentType != null) { | ||
| actual.setContentType(contentType); | ||
| } | ||
| actual.setStatus(status); | ||
| actual.setContentLength(buffer.size()); | ||
| buffer.writeTo(actual.getOutputStream()); | ||
| actual.getOutputStream().flush(); | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
framework/src/main/java/org/tron/core/services/filter/CachedBodyRequestWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package org.tron.core.services.filter; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.IllegalCharsetNameException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.charset.UnsupportedCharsetException; | ||
| import javax.servlet.ReadListener; | ||
| import javax.servlet.ServletInputStream; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletRequestWrapper; | ||
|
|
||
| /** | ||
| * Wraps a request and replays a pre-read body from a byte array. | ||
| */ | ||
| public class CachedBodyRequestWrapper extends HttpServletRequestWrapper { | ||
|
|
||
| private enum BodyAccessor { NONE, STREAM, READER } | ||
|
|
||
| private final byte[] body; | ||
| private BodyAccessor accessor = BodyAccessor.NONE; | ||
|
|
||
| public CachedBodyRequestWrapper(HttpServletRequest request, byte[] body) { | ||
| super(request); | ||
| this.body = body; | ||
| } | ||
|
|
||
| @Override | ||
| public ServletInputStream getInputStream() { | ||
|
317787106 marked this conversation as resolved.
|
||
| if (accessor == BodyAccessor.READER) { | ||
| throw new IllegalStateException("getReader() has already been called on this request"); | ||
| } | ||
| accessor = BodyAccessor.STREAM; | ||
| final ByteArrayInputStream bais = new ByteArrayInputStream(body); | ||
| return new ServletInputStream() { | ||
| @Override | ||
| public int read() { | ||
| return bais.read(); | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b, int off, int len) { | ||
| return bais.read(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFinished() { | ||
| return bais.available() == 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isReady() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void setReadListener(ReadListener readListener) { | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public BufferedReader getReader() { | ||
| if (accessor == BodyAccessor.STREAM) { | ||
| throw new IllegalStateException("getInputStream() has already been called on this request"); | ||
| } | ||
| accessor = BodyAccessor.READER; | ||
| String encoding = getCharacterEncoding(); | ||
| Charset charset; | ||
| try { | ||
| charset = encoding != null ? Charset.forName(encoding) : StandardCharsets.UTF_8; | ||
| } catch (IllegalCharsetNameException | UnsupportedCharsetException ex) { | ||
| charset = StandardCharsets.UTF_8; | ||
| } | ||
| return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(body), charset)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.