资源 · 37
保护 GraphQL API:授权、查询成本和证据
强制执行对象和字段权限、绑定服务器工作并测试拒绝路径。
已更新 · 14 min
本指南有助于实现的目标
- 绘制暴露的模式图
- 执行权限测试
- 绑定查询成本
- 验证拒绝操作
快速检查
- 一个账户能否读取另一个账户的对象?
- 敏感字段是否遵循其父级的访问决策?
- 深度、别名和分页的综合成本是多少?
- 哪些细节会通过错误泄露?
- 谁负责观察拒绝操作和滥用行为?
分步指南
- 1
清点操作
列出类型、字段、变更、角色、对象所有者和敏感数据。 包括当前客户端查询和不常用的路径。
交付成果:操作、角色、对象和字段矩阵。
- 2
测试每个访问决策
创建不同的测试账户并交换对象 ID,包括嵌套对象。 解析器必须在返回数据之前强制执行授权。
交付成果:可复现的允许和拒绝案例。
- 3
绑定服务器工作
测量字段和集合的实际成本。 同时测试深度、广度、别名、分页、批处理和时间,然后设置合适的限制。
交付成果:成本策略和对抗性查询集。
- 4
减少不必要的暴露
决定是否对上下文进行内省,隐藏内部错误详情,并移除超出客户端需求的信息字段。
交付成果:审查公共配置和错误响应。
- 5
谨慎观察
记录有用的技术身份、操作、成本、拒绝和关联信息,同时排除密钥和敏感参数。 分配警报负责人。
交付成果:仪表板和调查流程。
- 6
变更后重放
当字段、角色或客户端发生更改时,重复授权和加载用例。 检查现在遍历已修改图的旧查询。
交付成果:回归测试结果和发布决定。
管理指标
| 指标 | 衡量内容 | 首要行动 |
|---|---|---|
| 访问权限 | 对象和字段案例按预期被拒绝 | 修复解析器 |
| 成本 | 策略限制了高负载查询 | 调整字段和限制 |
| 错误 | 公开响应不包含内部详细信息 | 减少公开详细信息 |
| 回归 | 客户端操作在变更后重放 | 阻止意外破坏 |
常见错误
- 仅信任端点访问
- 限制深度但忽略广度
- 忽略嵌套字段授权
- 记录包含敏感数据的参数
常见问题解答
GraphQL 是否移除了普通 API 授权?
否。每个对象和字段的访问仍然需要基于身份和上下文的决策。
禁用内省是否足够?
否。它可能减少模式暴露,但无法解决访问或查询成本问题。
深度限制是否足够?
否。别名、广度、列表和字段特定成本也很重要。
GraphQL:错误、列表和部分数据
此语言版本的详细参考说明以英语提供。
How should a client read a GraphQL response when part of the data fails? Start with the processing phase, then follow Non-Null constraints to the first position that permits null. A screen that displays success simply because transport returned can hide an incomplete record. This dossier connects schema, JSON result and interface decisions through a fictional evidence-viewing scenario.
Reference: the published September 2025 edition, reviewed on 23 October 2026. The eight rules below are a CIE editorial selection, not a conformance certification. All scenarios and sample data are fictional.
GraphQL Specification · September 2025 · Published edition · September 2025
Eight points for interpreting the result
| Point | Client consequence | Source |
|---|---|---|
Request error result | An error preventing execution yields errors and no data entry. Invalid variables follow this route even when the document parses. | GraphQL 2025 |
Execution result | After execution, data is present. Its value may be a partial object or null. With no raised error, errors must be absent rather than an empty list. | GraphQL 2025 |
errors[].message | Each error includes a developer-facing message. Give visitors useful wording without exposing internal details or sensitive identifiers. | GraphQL 2025 |
errors[].locations | When a document position can be associated with an error, locations should give line and column starting at 1. These are not data paths. | GraphQL 2025 |
errors[].path | The path uses response names, including aliases, and zero-based integer indices. It keeps the failing position even when a parent is subsequently replaced with null. | GraphQL 2025 |
List / Non-Null | Separate list nullability from item nullability. A failing non-null item nulls the list; a non-null list propagates the error further to its parent. | GraphQL 2025 |
extensions | Response and error extensions are metadata maps defined by implementations. A code in extensions is not a universal GraphQL error-code catalogue. | GraphQL 2025 |
GraphQL / HTTP | The GraphQL specification does not define transport. GraphQL over HTTP is identified here as a working draft; test its media-type negotiation separately. | HTTP draft |
Identify the phase before choosing the message
| Situation | Expected shape | Journey action |
|---|---|---|
| Request rejected before execution | Non-empty errors; data absent | Fix the operation or its variables. Keep an actionable error instead of automatically replaying an unchanged request. |
| Execution with no raised error | data present; errors absent | Display returned data. A schema-permitted null may be normal and alone does not diagnose failure. |
| Execution with partial result | Object data; non-empty errors | Separate retained fields from failing positions. Prevent confirmation that depends on unavailable evidence. |
| Propagation reaches the root | data: null; non-empty errors | Present an unavailable state for this operation. Do not silently recycle a response belonging to another case. |
GraphQL空值传播模拟器
The model fixes a query using affaire as the alias of dossier. The case contains reference: String! and preuves, a three-item String list. statut: String! is a root sibling. Items are A-01, A-02 and A-03 before the injected failure. Choose the preuves type, dossier nullability and failing item.
query Demo {
affaire: dossier { reference preuves }
statut
}Local simulation of one item-resolution error on this fixed schema. No API call, query parser, permission test, cost calculation or HTTP verdict. Nested types, subscriptions and incremental delivery are outside the model. The JSON illustrates propagation and contains no real investigation data.
虚构JSON结果
Four list types, two distinct constraints
| preuves type | Null list allowed | Null item allowed | Single item failure |
|---|---|---|---|
| [String] | Yes | Yes | One item becomes null. |
| [String!] | Yes | No | The list becomes null. |
| [String]! | No | Yes | One item becomes null; the list remains present. |
| [String!]! | No | No | Propagation reaches dossier, then the root depending on its type. |
Eight reference results, readable without JavaScript
G01 · [String] · affaire: Dossier
Only item 1 becomes null; the other two items and the case remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": [
"A-01",
null,
"A-03"
]
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G02 · [String!] · affaire: Dossier
The preuves list becomes null; the case reference and status remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": null
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G03 · [String]! · affaire: Dossier
Only item 1 becomes null; the other two items and the case remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": [
"A-01",
null,
"A-03"
]
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G04 · [String!]! · affaire: Dossier
The affaire case becomes null; the sibling statut field remains available.
{
"data": {
"affaire": null,
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G05 · [String] · affaire: Dossier!
Only item 1 becomes null; the other two items and the case remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": [
"A-01",
null,
"A-03"
]
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G06 · [String!] · affaire: Dossier!
The preuves list becomes null; the case reference and status remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": null
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G07 · [String]! · affaire: Dossier!
Only item 1 becomes null; the other two items and the case remain readable.
{
"data": {
"affaire": {
"reference": "DEMO-01",
"preuves": [
"A-01",
null,
"A-03"
]
},
"statut": "fictif"
},
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
G08 · [String!]! · affaire: Dossier!
data becomes null: the chain of non-null response positions reaches the root.
{
"data": null,
"errors": [
{
"message": "Illustrative item resolution failure",
"path": [
"affaire",
"preuves",
1
]
}
]
}Acceptance check : Check the received JSON and retain errors[0].path = ["affaire", "preuves", 1], even when affaire is replaced by null.
Six business checks for a GraphQL viewing API
| CIE scenario | Evidence to collect | Decision criterion |
|---|---|---|
| G-ACC · Another tenant’s object | Two test identities and the same case ID, without production data. | The service enforces access on the object, relations and fields; possessing an ID grants no access. |
| G-ALIAS · Two aliases, one object | Two aliased selections and correlated traces in an authorized environment. | Aliases change neither access scope nor cost policy. The client links each error to the received response name. |
| G-CACHE · Account change | The sign-out journey followed by sign-in as another test account. | Cached data does not cross identity changes. Partial responses do not present an old proof as newly read. |
| G-COST · Volume and depth | Test measurements of cardinality, depth, time and service-specific quotas. | Depth alone is insufficient: bound list size, expensive fields and grouped operations. No universal threshold is invented. |
| G-ERROR · Public message | Examples of visitor-facing messages and operator-only traces. | Expose no stack, secret, table name or sensitive evidence. A non-sensitive correlation identifier supports troubleshooting. |
| G-SXO · Missing evidence blocks an action | Keyboard and screen-reader journey with a required field failing. | Announce the partial state, keep retained data readable, and explain then prevent confirmation that depends on the unavailable field. |
OWASP · GraphQL Cheat Sheet · Living guidance consulted 2026-10-23
A partial response proves neither a leak nor correct protection. Only service-side authorization tests can verify access. Link each acceptance case to the deployed schema, its version and client observations: data controls, interface resilience and transport need coordinated checks with distinct criteria.
Provenance, edition and licence
GraphQL Specification · September 2025 · reviewed 2026-10-23. CIE: original explanations, fictional scenarios and limited educational model.
GraphQL HTML SHA-256: 39fb3aeffd9b5acebb0830ddc6dcc243c1280a9f0e977897ae59c5cc165511d6
HTTP draft HTML SHA-256: 90a686f8251073b05e105697c6028288520d0c0fa6442924c0d1751cad3f14ac
GraphQL over HTTP · Current Working Draft · Working draft consulted 2026-10-23 · may change
GraphQL · Copyright and Licensing · Open Web Foundation Agreement 1.0
官方参考文献
参考文献支持该方法。 请根据您的实际情况调整检查;它们并非认证。 原始参考文献标题和源文档可能使用其他语言。
参考资料查阅日期 .
