上限扩展到512kb
This commit is contained in:
1 parent
02dd5668d5
commit
6358dff6e2
5 files changed
+42
-22
No files matched your search
@@ -108,7 +108,7 @@ node --env-file=.env src/index.js
|
|||||||
|
|
||||||
## 6. 外部商户签名接口
|
## 6. 外部商户签名接口
|
||||||
|
|
||||||
`POST /api/rental/v1/create`、`query`、`packages`,仅JSON,最多64KiB,不允许查询串。请求头为 `X-Rent-Key`、`X-Rent-Timestamp`(Unix毫秒)、`X-Rent-Nonce`、`X-Rent-Signature`。认证窗口前后60秒,nonce保留180秒后可清理。
|
`POST /api/rental/v1/create`、`query`、`packages`,仅JSON,最多512KiB(524,288字节),不允许查询串。请求头为 `X-Rent-Key`、`X-Rent-Timestamp`(Unix毫秒)、`X-Rent-Nonce`、`X-Rent-Signature`。认证窗口前后60秒,nonce保留180秒后可清理。
|
||||||
|
|
||||||
签名原文用换行连接,末尾不加换行:
|
签名原文用换行连接,末尾不加换行:
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
package com.ruoyi.system.rental;
|
package com.ruoyi.system.rental;
|
||||||
|
|
||||||
import static com.ruoyi.system.rental.RentalValues.*;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import java.io.ByteArrayOutputStream;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/** 商户HTTPS API,仅接受JSON POST;HMAC覆盖实际UTF-8请求体,禁止二次序列化验签。 */
|
import static com.ruoyi.system.rental.RentalValues.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商户HTTPS API,仅接受JSON POST;HMAC覆盖实际UTF-8请求体,禁止二次序列化验签。
|
||||||
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/rental/v1")
|
@RequestMapping("/api/rental/v1")
|
||||||
public class RentalApiController {
|
public class RentalApiController {
|
||||||
@@ -42,7 +47,7 @@ public class RentalApiController {
|
|||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int size;
|
int size;
|
||||||
while ((size = request.getInputStream().read(buffer)) != -1) {
|
while ((size = request.getInputStream().read(buffer)) != -1) {
|
||||||
require(output.size() + size <= 65536, "请求体超过64KiB");
|
require(output.size() + size <= RentalJson.MAX_HTTP_BODY_BYTES, "请求体超过512KiB");
|
||||||
output.write(buffer, 0, size);
|
output.write(buffer, 0, size);
|
||||||
}
|
}
|
||||||
String body =
|
String body =
|
||||||
@@ -51,7 +56,7 @@ public class RentalApiController {
|
|||||||
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
||||||
.decode(java.nio.ByteBuffer.wrap(output.toByteArray()))
|
.decode(java.nio.ByteBuffer.wrap(output.toByteArray()))
|
||||||
.toString();
|
.toString();
|
||||||
RentalJson.bounded(body);
|
RentalJson.bounded(body, RentalJson.MAX_HTTP_BODY_BYTES);
|
||||||
Map<String, Object> credential =
|
Map<String, Object> credential =
|
||||||
identity.authenticate(
|
identity.authenticate(
|
||||||
request.getHeader("X-Rent-Key"),
|
request.getHeader("X-Rent-Key"),
|
||||||
@@ -66,15 +71,16 @@ public class RentalApiController {
|
|||||||
Map<String, Object> app = store.get("app", number(credential, "owner_id"), false);
|
Map<String, Object> app = store.get("app", number(credential, "owner_id"), false);
|
||||||
require("ACTIVE".equals(text(app, "status")), "应用已禁用");
|
require("ACTIVE".equals(text(app, "status")), "应用已禁用");
|
||||||
Map<String, Object> data =
|
Map<String, Object> data =
|
||||||
json.readValue(body, new TypeReference<Map<String, Object>>() {});
|
json.readValue(body, new TypeReference<Map<String, Object>>() {
|
||||||
|
});
|
||||||
if ("packages".equals(operation)) {
|
if ("packages".equals(operation)) {
|
||||||
List<Map<String, Object>> products =
|
List<Map<String, Object>> products =
|
||||||
store.rows(
|
store.rows(
|
||||||
"SELECT"
|
"SELECT"
|
||||||
+ " id,package_code,revision,name,network,resource_type,resource_quantity,duration_seconds,sale_amount"
|
+ " id,package_code,revision,name,network,resource_type,resource_quantity,duration_seconds,sale_amount"
|
||||||
+ " FROM rent_package p WHERE status='ACTIVE' AND network=?"
|
+ " FROM rent_package p WHERE status='ACTIVE' AND network=?"
|
||||||
+ " AND EXISTS(SELECT 1 FROM rent_user_package b WHERE b.package_id=p.id AND b.user_id=? AND b.status='ACTIVE' AND b.default_slot=1) ORDER BY"
|
+ " AND EXISTS(SELECT 1 FROM rent_user_package b WHERE b.package_id=p.id AND b.user_id=? AND b.status='ACTIVE' AND b.default_slot=1) ORDER BY"
|
||||||
+ " id",
|
+ " id",
|
||||||
network(input(data, "network", 16)), number(app, "user_id"));
|
network(input(data, "network", 16)), number(app, "user_id"));
|
||||||
return AjaxResult.success(products);
|
return AjaxResult.success(products);
|
||||||
}
|
}
|
||||||
@@ -85,8 +91,8 @@ public class RentalApiController {
|
|||||||
RentalHttp.checkUrl(text(data, "notify_url").trim());
|
RentalHttp.checkUrl(text(data, "notify_url").trim());
|
||||||
}
|
}
|
||||||
Map<String, Object> product = store.one("SELECT p.* FROM rent_package p"
|
Map<String, Object> product = store.one("SELECT p.* FROM rent_package p"
|
||||||
+ " JOIN rent_user_package b ON b.package_id=p.id WHERE b.user_id=?"
|
+ " JOIN rent_user_package b ON b.package_id=p.id WHERE b.user_id=?"
|
||||||
+ " AND b.status='ACTIVE' AND b.default_slot=1 AND p.status='ACTIVE' AND p.id=?",
|
+ " AND b.status='ACTIVE' AND b.default_slot=1 AND p.status='ACTIVE' AND p.id=?",
|
||||||
number(app, "user_id"), number(data, "package_id"));
|
number(app, "user_id"), number(data, "package_id"));
|
||||||
require(product != null, "套餐未绑定或已下架");
|
require(product != null, "套餐未绑定或已下架");
|
||||||
RentalOrders.validatePackage(product);
|
RentalOrders.validatePackage(product);
|
||||||
|
|||||||
+2
-2
@@ -62,13 +62,13 @@ public class RentalCallbackDemoController {
|
|||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int size;
|
int size;
|
||||||
while ((size = request.getInputStream().read(buffer)) != -1) {
|
while ((size = request.getInputStream().read(buffer)) != -1) {
|
||||||
require(output.size() + size <= 65536, "通知超过64KiB");
|
require(output.size() + size <= RentalJson.MAX_HTTP_BODY_BYTES, "通知超过512KiB");
|
||||||
output.write(buffer, 0, size);
|
output.write(buffer, 0, size);
|
||||||
}
|
}
|
||||||
body = StandardCharsets.UTF_8.newDecoder()
|
body = StandardCharsets.UTF_8.newDecoder()
|
||||||
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
||||||
.decode(java.nio.ByteBuffer.wrap(output.toByteArray())).toString();
|
.decode(java.nio.ByteBuffer.wrap(output.toByteArray())).toString();
|
||||||
RentalJson.bounded(body);
|
RentalJson.bounded(body, RentalJson.MAX_HTTP_BODY_BYTES);
|
||||||
verify(apiKey, apiSecret, request.getHeader("X-Rent-Key"),
|
verify(apiKey, apiSecret, request.getHeader("X-Rent-Key"),
|
||||||
Long.parseLong(request.getHeader("X-Rent-Timestamp")),
|
Long.parseLong(request.getHeader("X-Rent-Timestamp")),
|
||||||
request.getHeader("X-Rent-Nonce"), request.getRequestURI(), body,
|
request.getHeader("X-Rent-Nonce"), request.getRequestURI(), body,
|
||||||
|
|||||||
@@ -4,10 +4,18 @@ import static com.ruoyi.system.rental.RentalValues.require;
|
|||||||
|
|
||||||
/** Jackson2.10没有流深度限制,解析前先检查结构深度;字符串内括号不计入。 */
|
/** Jackson2.10没有流深度限制,解析前先检查结构深度;字符串内括号不计入。 */
|
||||||
public final class RentalJson {
|
public final class RentalJson {
|
||||||
|
/** 商户HTTP请求及回调请求的UTF-8字节上限:512 KiB。读取流时执行字节校验。 */
|
||||||
|
public static final int MAX_HTTP_BODY_BYTES = 512 * 1024;
|
||||||
|
|
||||||
private RentalJson() {}
|
private RentalJson() {}
|
||||||
|
|
||||||
public static void bounded(String value) {
|
public static void bounded(String value) {
|
||||||
require(value != null && value.length() <= 65536, "JSON消息超限");
|
bounded(value, 65536);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 按传输通道限制解析字符数;HTTP入口另外按原始字节数限制,节点帧保持原上限。 */
|
||||||
|
public static void bounded(String value, int maxCharacters) {
|
||||||
|
require(value != null && value.length() <= maxCharacters, "JSON消息超限");
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
boolean quoted = false;
|
boolean quoted = false;
|
||||||
boolean escaped = false;
|
boolean escaped = false;
|
||||||
|
|||||||
@@ -1312,6 +1312,12 @@ public class RentalBusinessTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void boundedJsonAndSsrfGuard() throws Exception {
|
public void boundedJsonAndSsrfGuard() throws Exception {
|
||||||
|
// HTTP允许恰好512 KiB的ASCII JSON,超出1字节拒绝;节点通道仍保持64 KiB。
|
||||||
|
String httpBoundary = "\"" + String.join("", Collections.nCopies(
|
||||||
|
RentalJson.MAX_HTTP_BODY_BYTES - 2, "a")) + "\"";
|
||||||
|
RentalJson.bounded(httpBoundary, RentalJson.MAX_HTTP_BODY_BYTES);
|
||||||
|
expectFailure(() -> RentalJson.bounded(httpBoundary + " ", RentalJson.MAX_HTTP_BODY_BYTES));
|
||||||
|
expectFailure(() -> RentalJson.bounded(httpBoundary));
|
||||||
expectFailure(
|
expectFailure(
|
||||||
() ->
|
() ->
|
||||||
RentalJson.bounded(
|
RentalJson.bounded(
|
||||||
|
|||||||
Reference in new issue
Block a user