A vs B: Placing Your WAF Before or After a JSON API Gateway
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
A vs B: Placing Your WAF Before or After a JSON API Gateway
An automated attacker does not need a suspicious URL to probe for SQL injection. A normal-looking POST to a search endpoint can carry the payload inside a JSON filter, nested object, or array. If the application builds SQL by concatenating that value, the request body becomes a route to the database. For security owners investigating how to block sql injection attempts hidden in json bodies, placement matters: should inspection happen before an API gateway, or after the gateway has transformed the request? This comparison addresses API abuse through automated payload variation. It is a deployment decision guide, not a claim that the supplied SafeLine documentation guarantees inspection of every JSON representation.
自动化攻击者不需要可疑的 URL 来探测 SQL 注入。一个看起来正常的 POST 请求发送到搜索端点时,可以在 JSON 过滤器、嵌套对象或数组中携带攻击载荷。如果应用程序通过拼接该值来构建 SQL,请求体就成了通往数据库的路径。对于正在研究如何拦截隐藏在 JSON 体中 SQL 注入尝试的安全负责人来说,部署位置至关重要:检查应该发生在 API 网关之前,还是在网关转换请求之后?本比较旨在探讨通过自动化载荷变体进行的 API 滥用。这是一份部署决策指南,并非声称所提供的 SafeLine 文档能保证对每一种 JSON 表示形式进行检查。
The attack and the evidence Consider an illustrative request to an application you own: POST /api/orders/search HTTP/1.1 Content-Type: application/json {“filters”:{“customer”:”’ OR ‘1’=‘1”},“limit”:20} The attacker hopes the application will insert customer directly into a SQL expression. Automation can repeat the probe across fields, nesting levels, and accepted encodings. The payload is an attempted injection; its presence alone does not prove that the application is vulnerable. Defenders may see repeated requests to the same endpoint, changing filter values, application validation failures, database syntax errors, or unusual response sizes. None of these signals alone establishes successful exploitation. Correlate the request with gateway records, application traces, and database activity using a request identifier where available. Avoid recording credentials or entire sensitive bodies just to improve visibility. SafeLine’s official documentation describes it as a self-hosted WAF that filters and monitors HTTP traffic and lists SQL injection among the attacks it protects against. That establishes its intended role, but does not establish specific JSON parsing behavior.
攻击与证据:考虑一个你所拥有的应用程序的示例请求:POST /api/orders/search HTTP/1.1 Content-Type: application/json {“filters”:{“customer”:”’ OR ‘1’=‘1”},“limit”:20}。攻击者希望应用程序将 customer 直接插入到 SQL 表达式中。自动化工具可以在不同字段、嵌套层级和接受的编码中重复探测。该载荷是一次注入尝试;其存在本身并不能证明应用程序存在漏洞。防御者可能会看到针对同一端点的重复请求、过滤器值的变化、应用程序验证失败、数据库语法错误或异常的响应大小。这些信号中的任何一个都不能单独证明利用成功。在可用时,使用请求标识符将请求与网关记录、应用程序跟踪和数据库活动关联起来。避免为了提高可见性而记录凭据或整个敏感请求体。SafeLine 的官方文档将其描述为一种自托管的 WAF,用于过滤和监控 HTTP 流量,并将 SQL 注入列为其防御的攻击类型之一。这确立了其预期的角色,但并未确立具体的 JSON 解析行为。
SafeLine introduction A: Inspect before the API gateway The path is client → SafeLine → API gateway → application. This placement puts inspection ahead of gateway processing. It can be useful when the gateway is itself an exposed component or when several downstream APIs share one public entry point. SafeLine documents an HTTP/HTTPS reverse proxy model: configure an application’s domain, listening port, and upstream, then resolve the domain to the SafeLine server. In this arrangement, the gateway would be the upstream.
SafeLine 介绍 A:在 API 网关之前检查。路径为:客户端 → SafeLine → API 网关 → 应用程序。这种部署将检查置于网关处理之前。当网关本身是一个暴露的组件,或者多个下游 API 共享一个公共入口点时,这种方式很有用。SafeLine 文档记录了一种 HTTP/HTTPS 反向代理模型:配置应用程序的域名、监听端口和上游,然后将域名解析到 SafeLine 服务器。在这种安排中,网关将作为上游。
Application configuration The critical question is what happens after inspection. A gateway might transform an accepted body before forwarding it. If the application receives a materially different representation, testing only the incoming request leaves a visibility gap. Check which transformations actually occur. Compare a synthetic request at ingress with the request delivered to the application. Verify HTTPS handling, accepted content types, body limits, and whether alternate hostnames or direct origin access bypass the inspection path. Choose A when protecting the public entry point is the priority and tests demonstrate that inspection remains effective across the gateway’s transformations.
应用程序配置:关键问题在于检查之后会发生什么。网关可能会在转发前转换已接收的请求体。如果应用程序接收到的表示形式有实质性差异,仅测试传入请求会留下可见性盲区。检查实际发生了哪些转换。比较入口处的合成请求与交付给应用程序的请求。验证 HTTPS 处理、接受的内容类型、请求体限制,以及备用主机名或直接源站访问是否绕过了检查路径。当保护公共入口点是首要任务,且测试证明检查在网关转换后依然有效时,请选择 A。
B: Inspect after the API gateway The path is client → API gateway → SafeLine → application. This placement can bring inspection closer to the representation the application receives. It is useful to evaluate when a gateway performs required transformations before forwarding JSON. The tradeoff is coverage. The gateway receives hostile traffic before this WAF inspection point, and every relevant application route must pass through SafeLine. A service reachable through another ingress path can escape that coverage. Check the actual gateway-to-application route, including internal callers and administrative endpoints. Confirm that necessary authentication context survives forwarding. Verify how client identity reaches downstream systems before relying on it for attribution or traffic policies. Choose B when post-transformation visibility is the stronger requirement and routing controls ensure that application traffic crosses the inspection point. The supplied documentation does not prescribe this topology or guarantee compatibility with a particular gateway; validate it in your environment.
B:在 API 网关之后检查。路径为:客户端 → API 网关 → SafeLine → 应用程序。这种部署可以将检查点更贴近应用程序接收到的表示形式。当网关在转发 JSON 前执行必要的转换时,这种方式很有评估价值。其代价是覆盖范围。网关在到达此 WAF 检查点之前会接收到恶意流量,且每个相关的应用程序路由都必须通过 SafeLine。通过其他入口路径可访问的服务可能会逃避该覆盖范围。检查实际的网关到应用程序路由,包括内部调用者和管理端点。确认必要的身份验证上下文在转发后依然存在。在依赖客户端身份进行归因或流量策略之前,验证其如何到达下游系统。当转换后的可见性是更强的需求,且路由控制确保应用程序流量会经过检查点时,请选择 B。所提供的文档并未规定此拓扑结构,也不保证与特定网关的兼容性;请在你的环境中进行验证。
Make the choice with paired tests Run authorized tests against a staging endpoint with representative application behavior. Include ordinary filters, legitimate apostrophes, nested objects, arrays, malformed JSON, and synthetic injection strings. Add encoded representations only where your application accepts them. For each case, record three outcomes: the inspection decision, whether the application received the request, and what the application did with it. A blocked response is useful evidence, but it does not prove that every route or body representation is covered. SafeLine documents an attack list and a detail view in its console. Its published SQL injection walkthrough uses a URL query parameter, so treat that example as evidence for the documented demonstration, not as proof of JSON body coverage.
通过配对测试做出选择:针对具有代表性应用程序行为的预发布(staging)端点运行授权测试。包括普通过滤器、合法的撇号、嵌套对象、数组、格式错误的 JSON 和合成的注入字符串。仅在你的应用程序接受的情况下添加编码表示。对于每种情况,记录三个结果:检查决定、应用程序是否接收到请求,以及应用程序对请求的处理结果。被拦截的响应是有用的证据,但它不能证明每个路由或请求体表示形式都被覆盖。SafeLine 在其控制台中记录了攻击列表和详细视图。其发布的 SQL 注入演练使用了 URL 查询参数,因此请将该示例视为文档演示的证据,而非 JSON 请求体覆盖范围的证明。
Testing and attack details If a dangerous request passes, determine whether the cause is routing, a representation mismatch, or detection behavior before changing configuration. If legitimate traffic fails, narrow the investigation to the affected endpoint and input. Avoid treating an entire JSON API as trusted. Does valid JSON prevent SQL injection? No. JSON validity concerns the transport structure. A valid string can still become unsafe SQL if application code concatenates it into a query. Use parameterized queries and constrain dynamic identifiers such as sort columns with explicit allowlists. Do these sources confirm nested JSON inspection in SafeLine? No. They document general SQL injection protection and reverse proxy deployment, but do not specify nested JSON parsing, decoding depth, or inspection limits. Validate the deployed version with representative requests before making a coverage claim.
测试与攻击细节:如果危险请求通过了检查,在更改配置之前,请确定原因是路由问题、表示形式不匹配还是检测行为。如果合法流量失败,请将调查范围缩小到受影响的端点和输入。避免将整个 JSON API 视为可信。有效的 JSON 能防止 SQL 注入吗?不能。JSON 的有效性仅涉及传输结构。如果应用程序代码将有效的字符串拼接到查询中,它仍然可能变成不安全的 SQL。请使用参数化查询,并使用显式白名单约束排序字段等动态标识符。这些来源确认了 SafeLine 对嵌套 JSON 的检查吗?没有。它们记录了通用的 SQL 注入防护和反向代理部署,但并未明确嵌套 JSON 的解析、解码深度或检查限制。在做出覆盖范围声明之前,请使用具有代表性的请求验证已部署的版本。