-
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
Changes from all commits
b32fa58
02a588f
eff49b9
19217b8
0351c22
acd51cb
fa87c7e
01da427
7b2585d
a0f51f8
38edfda
e70ea6b
bf9a5a1
9eb44ce
d65f064
801e7ae
2bb35a8
d09d720
4bdc025
fd7fdf9
91a2f12
7bdddbb
2fdb6b8
c329b47
998f52f
e6eca1c
498c9da
90eabc6
3908770
f28beb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| 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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MUST]
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is little possibility to int overflow, so stays. |
||
| 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; | ||
| // Flush the PrintWriter's OutputStreamWriter encoder into our ByteArrayOutputStream. | ||
| // PrintWriter(autoFlush=true) only auto-flushes on println/printf/format, not print/write, | ||
| // so bytes can remain buffered in the encoder until an explicit flush. | ||
| writer.flush(); | ||
| if (overflow) { | ||
| return; | ||
| } | ||
| if (contentType != null) { | ||
| actual.setContentType(contentType); | ||
| } | ||
| actual.setStatus(status); | ||
| actual.setContentLength(buffer.size()); | ||
|
waynercheung marked this conversation as resolved.
|
||
| buffer.writeTo(actual.getOutputStream()); | ||
| actual.getOutputStream().flush(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| 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 to replay a pre-read body from a byte array, | ||
| * allowing the body to be read more than once. | ||
| * | ||
| * <p><b>Scope:</b> designed for synchronous, raw-body POST endpoints | ||
| * (e.g. JSON-RPC). It is NOT compatible with: | ||
| * <ul> | ||
| * <li>{@code application/x-www-form-urlencoded} — cached body cannot back | ||
| * {@code getParameter*}.</li> | ||
| * <li>multipart — {@code getPart()/getParts()} read from the original | ||
| * (already-consumed) stream.</li> | ||
| * <li>async non-blocking I/O — see {@code setReadListener}.</li> | ||
| * <li>request dispatch / forward chains.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Multiple calls to {@code getInputStream()} (or {@code getReader()}) | ||
| * are allowed and each returns a fresh stream over the same cached body — | ||
| * a deliberate extension of the standard servlet contract. | ||
| */ | ||
| 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) { | ||
|
317787106 marked this conversation as resolved.
|
||
| throw new UnsupportedOperationException( | ||
| "async I/O is not supported on cached body"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @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)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.