GeezerVison is showing github.com — Allow parsing of graphQL queries (#14) · majorcontext/keep@41266c0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | 7 | 7 |
| 8 | 8 | ## [Unreleased] | 9 | 9 |
| | 10 | +### Added | | 11 | + | | 12 | +- **`graphql()` CEL function** — parses a GraphQL-over-HTTP request body (`graphql(params.body)`) into `{valid, error, operations, queries, mutations, subscriptions}`. Each root field reports its real `name`, its `alias`, and `args` with variables and variable defaults substituted. Every operation in the document is reported regardless of `operationName`, root fragment spreads and inline fragments are expanded, and directives are ignored, so a rule sees every root field the document contains. The body is parsed in its original case even in case-insensitive scopes, since GraphQL names and node IDs are case-sensitive. Malformed input returns `valid: false` with empty lists rather than an error. | | 13 | +- **`Engine.RequiresBodyFor(scope, call)`** — narrows `RequiresBody` by operation: a rule that reads `params.body` counts only when its `operation` glob matches the call. Pinning a body rule to one endpoint (for example `operation: "post api.github.com/graphql"`) lets a gatekeeper buffer bodies for that endpoint alone instead of making every request in the scope JSON-only. An unknown scope returns `true`. | | 14 | + | | 15 | +### Fixed | | 16 | + | | 17 | +- Fix `ValidateRuleBytes` rejecting rule files that use `defs` — previously it compiled each `when` expression without substituting defs, so a file that `LoadFromBytes` accepted failed validation with "undeclared reference". Validation now prepares expressions exactly as the engine does. | | 18 | +- Fix the original-case rewrite for `hasSecrets` — previously `hasSecrets(` inside a string literal or at the end of a longer identifier was rewritten too, corrupting the literal. | | 19 | + | 10 | 20 | ## [0.6.0] - 2026-06-22 | 11 | 21 |
| 12 | 22 | ### Added | |
Collapse file Copy file name to clipboardExpand all lines: docs/content/concepts/02-expressions.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -135,7 +135,7 @@ when: "params.name == 'Bash'" | 135 | 135 |
| 136 | 136 | Operation names are also lowered, so glob patterns and operation literals should use lowercase. | 137 | 137 |
| 138 | | -**What is preserved:** Secret detection (`hasSecrets` and `redact.secrets`) and regex redaction patterns operate on original-case values. Gitleaks patterns like `AKIA[0-9A-Z]{16}` require exact case to match, so the engine threads original params to these functions automatically. Audit trail entries also preserve original operation names and parameter values. | | 138 | +**What is preserved:** Secret detection (`hasSecrets` and `redact.secrets`), GraphQL parsing (`graphql`), and regex redaction patterns operate on original-case values. Gitleaks patterns like `AKIA[0-9A-Z]{16}` require exact case to match, so the engine threads original params to these functions automatically. Audit trail entries also preserve original operation names and parameter values. | 139 | 139 |
| 140 | 140 | **Opting out:** Set `case_sensitive: true` at the scope level to disable normalization entirely. In this mode, `params.name == 'Bash'` only matches exactly `"Bash"`. Use this for scopes where exact-case matching is required (e.g., credential vaults, case-sensitive file paths). | 141 | 141 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/content/guides/06-go-library.md +19Lines changed: 19 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -124,6 +124,25 @@ func middleware(engine *keep.Engine, scope string) http.HandlerFunc { | 124 | 124 |
| 125 | 125 | `RequiresBody` fails safe: it detects every idiomatic body reference (`params.body.x`, `params["body"]`, `has(params.body)`, comprehensions, the `in` operator), and for any unrecognized use of the `params` map -- or an unknown scope name -- it returns `true` so the body is buffered rather than silently skipped. | 126 | 126 |
| | 127 | +`RequiresBody` answers for the whole scope: one body rule makes every request in the scope need its body. When a body rule only applies to one endpoint, pin it with `operation` and call `Engine.RequiresBodyFor(scope, call)` instead. It counts a body rule only when the rule's `operation` glob matches the call (a body rule without `operation` still counts for every call), so other requests in the scope -- including ones with non-JSON bodies -- are never buffered: | | 128 | + | | 129 | +```yaml | | 130 | +- name: no-issue-deletion | | 131 | + match: | | 132 | + operation: "post api.linear.app/graphql" | | 133 | + when: "graphql(params.body).mutations.exists(m, m.name == 'issueDelete')" | | 134 | + action: deny | | 135 | +``` | | 136 | + | | 137 | +```go | | 138 | +probe := keep.NewHTTPCall(r.Method, r.Host, r.URL.Path) // same operation, no body | | 139 | +if engine.RequiresBodyFor(scope, probe) { | | 140 | + // buffer and decode the body as above | | 141 | +} | | 142 | +``` | | 143 | + | | 144 | +The operation is normalized the same way `Evaluate` normalizes it, so `POST API.Linear.app/graphql` matches the lowercase pattern in a case-insensitive scope. Like `RequiresBody`, it returns `true` for an unknown scope. | | 145 | + | 127 | 146 | ## Handle decisions | 128 | 147 |
| 129 | 148 | `result.Decision` is one of three values: | |
Collapse file Copy file name to clipboardExpand all lines: docs/content/reference/02-rule-file.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -139,7 +139,7 @@ Def names and profile alias names must not use any of the following identifiers: | 139 | 139 | |----------|-------| | 140 | 140 | | Top-level variables | `params`, `context`, `now` | | 141 | 141 | | CEL standard functions | `size`, `has`, `matches`, `startsWith`, `endsWith`, `contains`, `exists`, `all`, `filter`, `exists_one` | | 142 | | -| Keep custom functions | `containsAny`, `estimateTokens`, `inTimeWindow`, `rateCount`, `lower`, `upper`, `matchesDomain`, `dayOfWeek`, `hasSecrets` | | | 142 | +| Keep custom functions | `containsAny`, `estimateTokens`, `inTimeWindow`, `rateCount`, `lower`, `upper`, `matchesDomain`, `dayOfWeek`, `hasSecrets`, `graphql` | | 143 | 143 | | CEL type identifiers | `int`, `uint`, `double`, `bool`, `string`, `bytes`, `list`, `map`, `type`, `null_type` | | 144 | 144 | | Literal keywords | `true`, `false`, `null` | | 145 | 145 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/content/reference/03-cel-functions.md +64Lines changed: 64 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -223,3 +223,67 @@ hasSecrets(params.body.headers) // scan a nested object | 223 | 223 | ``` | 224 | 224 |
| 225 | 225 | > **Note:** Pattern-based detection. It catches common secret formats but does not guarantee detection of all secrets, and false positives are possible. | | 226 | +
| | 227 | +## Request parsing | | 228 | + | | 229 | +### `graphql` | | 230 | + | | 231 | +```text | | 232 | +graphql(body) -> map | | 233 | +``` | | 234 | + | | 235 | +Parses a GraphQL-over-HTTP request body -- the decoded JSON object `{"query": ..., "variables": ..., "operationName": ...}` -- and returns its operations and root fields. Use it to write rules against GraphQL APIs, where every request is a `POST` to one endpoint and the operation lives in the body. | | 236 | + | | 237 | +| Parameter | Type | Description | | | 238 | +|-----------|------|-------------| | | 239 | +| `body` | `dyn` | The decoded request body, typically `params.body` | | | 240 | + | | 241 | +The result is a map: | | 242 | + | | 243 | +| Key | Type | Description | | | 244 | +|-----|------|-------------| | | 245 | +| `valid` | `bool` | `false` if the body is not a JSON object, `query` is missing or not a string, `variables` is not an object, the document does not parse, or a fragment is unknown, cyclic, or expands past the limits below | | | 246 | +| `error` | `string` | Why the body is invalid; `""` when `valid` is `true` | | | 247 | +| `operations` | `list(map)` | One entry per operation: `{type, name, fields}`. `type` is `"query"`, `"mutation"`, or `"subscription"`; `name` is `""` for an anonymous operation | | | 248 | +| `queries` | `list(map)` | Root fields of every query operation | | | 249 | +| `mutations` | `list(map)` | Root fields of every mutation operation | | | 250 | +| `subscriptions` | `list(map)` | Root fields of every subscription operation | | | 251 | + | | 252 | +Each field is `{name, alias, args}`. `alias` is `""` when the field has none. `args` maps argument names to values with variables substituted. | | 253 | + | | 254 | +**Example:** | | 255 | + | | 256 | +```yaml | | 257 | +- name: no-issue-deletion | | 258 | + match: | | 259 | + operation: "post api.linear.app/graphql" | | 260 | + when: "graphql(params.body).mutations.exists(m, m.name == 'issueDelete')" | | 261 | + action: deny | | 262 | + message: "Deleting issues is not permitted." | | 263 | + | | 264 | +- name: unparseable-graphql | | 265 | + match: | | 266 | + operation: "post api.linear.app/graphql" | | 267 | + when: "!graphql(params.body).valid" | | 268 | + action: deny | | 269 | + message: "GraphQL request could not be parsed." | | 270 | +``` | | 271 | +
| | 272 | +```cel | | 273 | +graphql(params.body).mutations.exists(m, | | 274 | + has(m.args.input) && has(m.args.input.repositoryId) && | | 275 | + !(m.args.input.repositoryId in ["R_kgDOAbCd"])) | | 276 | +``` | | 277 | + | | 278 | +Semantics: | | 279 | + | | 280 | +- **Every operation is reported.** `operationName` is ignored, so a mutation is visible to rules even when the client asks the server to run a different operation from the same document. | | 281 | +- **Root fragments are expanded.** Fragment spreads and inline fragments in an operation's root selection set are flattened into its fields. Type conditions and directives are ignored -- a field marked `@skip(if: true)` is still reported. | | 282 | +- **Aliases do not hide fields.** `name` is the field's real name; `alias` is reported separately. | | 283 | +- **Variables are substituted.** A variable takes its value from `variables` (an explicit `null` counts as a value), then from its default in the operation's variable definitions. A variable with neither is omitted from an object argument and becomes `null` inside a list. Variables nested in object and list literals are substituted too. | | 284 | +- **Literal values** become strings, ints, doubles, booleans, or `null`. Enum values become strings. String escapes are decoded. An integer outside the 64-bit range is kept as its literal text. | | 285 | +- **Original case.** GraphQL names and node IDs are case-sensitive, so `graphql(params.body)` parses the body as the client sent it, even when the scope lowercases params for matching (the default). Compare names and values with their exact case, or wrap them in `lower()`. | | 286 | +- **Limits.** Documents over 100,000 tokens, fragment nesting deeper than 32 levels, or more than 1,000 expanded root fields are invalid. | | 287 | +- **Never errors.** Invalid input returns `valid: false` with empty lists, so `.exists()` over `mutations` is always safe. Pair content rules with a `!graphql(params.body).valid` deny rule so an unparseable body fails closed. | | 288 | + | | 289 | +> **Note:** `graphql` reads `params.body`, so the scope requires the request body. Pin GraphQL rules to the endpoint with `operation` (as above) so a gatekeeper using `Engine.RequiresBodyFor` buffers bodies only for that endpoint. See [Inspecting the request body](../guides/06-go-library.md#inspecting-the-request-body). |
Collapse file Copy file name to clipboardExpand all lines: go.mod +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | require ( | 6 | 6 | github.com/google/cel-go v0.27.0 | 7 | 7 | github.com/spf13/cobra v1.10.2 | | 8 | + github.com/vektah/gqlparser/v2 v2.5.64 | 8 | 9 | github.com/zricethezav/gitleaks/v8 v8.30.1 | 9 | 10 | gopkg.in/yaml.v3 v3.0.1 | 10 | 11 | ) | |
Collapse file Copy file name to clipboardExpand all lines: go.sum +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,8 @@ github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe | 31 | 31 | github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0= | 32 | 32 | github.com/STARRY-S/zip v0.2.1 h1:pWBd4tuSGm3wtpoqRZZ2EAwOmcHK6XFf7bU9qcJXyFg= | 33 | 33 | github.com/STARRY-S/zip v0.2.1/go.mod h1:xNvshLODWtC4EJ702g7cTYn13G53o1+X9BWnPFpcWV4= | | 34 | +github.com/agnivade/levenshtein v1.2.2 h1:fI8RgiztYX1Nn5pBHHqQ9EMCCklhrmXqPRIedFmx5M4= | | 35 | +github.com/agnivade/levenshtein v1.2.2/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU= | 34 | 36 | github.com/andybalholm/brotli v1.1.2-0.20250424173009-453214e765f3 h1:8PmGpDEZl9yDpcdEr6Odf23feCxK3LNUNMxjXg41pZQ= | 35 | 37 | github.com/andybalholm/brotli v1.1.2-0.20250424173009-453214e765f3/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= | 36 | 38 | github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= | @@ -179,8 +181,6 @@ github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ | 179 | 181 | github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= | 180 | 182 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | 181 | 183 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | 182 | | -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= | 183 | | -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | 184 | 184 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= | 185 | 185 | github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | 186 | 186 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= | @@ -220,8 +220,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P | 220 | 220 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | 221 | 221 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | 222 | 222 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | 223 | | -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | 224 | | -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | | 223 | +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= | | 224 | +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= | 225 | 225 | github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= | 226 | 226 | github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= | 227 | 227 | github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I= | @@ -231,6 +231,8 @@ github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0B | 231 | 231 | github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= | 232 | 232 | github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc= | 233 | 233 | github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= | | 234 | +github.com/vektah/gqlparser/v2 v2.5.64 h1:N32yM6hQ6Kd5RdjCCEJsUrbCebVoA9yLLMOIa88Mkk0= | | 235 | +github.com/vektah/gqlparser/v2 v2.5.64/go.mod h1:nILRSUbsWDTuf8hSmAgFxuYh0SaMZdkvl6JSEbkJX8g= | 234 | 236 | github.com/wasilibs/go-re2 v1.9.0 h1:kjAd8qbNvV4Ve2Uf+zrpTCrDHtqH4dlsRXktywo73JQ= | 235 | 237 | github.com/wasilibs/go-re2 v1.9.0/go.mod h1:0sRtscWgpUdNA137bmr1IUgrRX0Su4dcn9AEe61y+yI= | 236 | 238 | github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 h1:OvLBa8SqJnZ6P+mjlzc2K7PM22rRUPE1x32G9DTPrC4= | @@ -246,8 +248,9 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= | 246 | 248 | go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= | 247 | 249 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= | 248 | 250 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= | 249 | | -go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= | 250 | 251 | go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= | | 252 | +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= | | 253 | +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= | 251 | 254 | go4.org v0.0.0-20230225012048-214862532bf5 h1:nifaUDeh+rPaBCMPMQHZmvJf+QdpLFnuQPwx+LxVmtc= | 252 | 255 | go4.org v0.0.0-20230225012048-214862532bf5/go.mod h1:F57wTi5Lrj6WLyswp5EYV1ncrEbFGHD4hhz6S1ZYeaU= | 253 | 256 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | |
Collapse file Copy file name to clipboardExpand all lines: internal/cel/env.go +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -304,6 +304,28 @@ func NewEnv(opts ...EnvOption) (*Env, error) { | 304 | 304 | }), | 305 | 305 | ), | 306 | 306 | ), | | 307 | + | | 308 | + // graphql(body) map — parse a GraphQL-over-HTTP request body into its | | 309 | + // operations and root fields (see ParseGraphQLRequest). Never errors: | | 310 | + // malformed input returns {valid: false, ...} with empty lists. | | 311 | + cel.Function("graphql", | | 312 | + cel.Overload("graphql_dyn", | | 313 | + []*cel.Type{cel.DynType}, | | 314 | + cel.MapType(cel.StringType, cel.DynType), | | 315 | + cel.UnaryBinding(graphqlVal), | | 316 | + ), | | 317 | + // graphql(loweredBody, originalBody) — the engine rewrites | | 318 | + // graphql(params.body) to graphql(params.body, _originalParams.body) | | 319 | + // so the case-sensitive GraphQL document (and node IDs) are parsed | | 320 | + // as the client sent them, not as case normalization lowered them. | | 321 | + cel.Overload("graphql_dyn_dyn", | | 322 | + []*cel.Type{cel.DynType, cel.DynType}, | | 323 | + cel.MapType(cel.StringType, cel.DynType), | | 324 | + cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val { | | 325 | + return graphqlVal(originalGraphQLBody(lhs, rhs)) | | 326 | + }), | | 327 | + ), | | 328 | + ), | 307 | 329 | ) | 308 | 330 | if err != nil { | 309 | 331 | return nil, fmt.Errorf("cel: create env: %w", err) | |
Collapse file Copy file name to clipboardExpand all lines: internal/cel/fuzz_test.go +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package cel_test | 2 | 2 |
| 3 | 3 | import ( | | 4 | + "encoding/json" | 4 | 5 | "testing" | 5 | 6 |
| 6 | 7 | keepcel "github.com/majorcontext/keep/internal/cel" | @@ -47,3 +48,40 @@ func FuzzCompile(f *testing.F) { | 47 | 48 | _, _ = env.Compile(expr) | 48 | 49 | }) | 49 | 50 | } | | 51 | + | | 52 | +// FuzzParseGraphQLRequest checks the GraphQL request parser never panics and | | 53 | +// always returns the documented shape, whatever the query or variables hold. | | 54 | +func FuzzParseGraphQLRequest(f *testing.F) { | | 55 | + seeds := []string{ | | 56 | + `{ viewer { login } }`, | | 57 | + `mutation($input: CreatePullRequestInput!) { createPullRequest(input: $input) { clientMutationId } }`, | | 58 | + `mutation { ...A } fragment A on Mutation { ...B } fragment B on Mutation { createRef(input: {repositoryId: "R"}) }`, | | 59 | + `mutation { ...A } fragment A on Mutation { ...A }`, | | 60 | + `query Q { a } mutation M { b(x: [1, 2.5, "s", true, null, ENUM, {k: $v}]) }`, | | 61 | + `subscription { s }`, | | 62 | + `mutation {`, | | 63 | + `"""desc""" query { a }`, | | 64 | + "", | | 65 | + "\x00\xff", | | 66 | + } | | 67 | + for _, s := range seeds { | | 68 | + f.Add(s, `{"v": 1}`) | | 69 | + } | | 70 | + f.Fuzz(func(t *testing.T, query, varsJSON string) { | | 71 | + var vars any | | 72 | + _ = json.Unmarshal([]byte(varsJSON), &vars) | | 73 | + res := keepcel.ParseGraphQLRequest(map[string]any{"query": query, "variables": vars}) | | 74 | + valid, ok := res["valid"].(bool) | | 75 | + if !ok { | | 76 | + t.Fatalf("valid is %T", res["valid"]) | | 77 | + } | | 78 | + if msg, _ := res["error"].(string); valid == (msg != "") { | | 79 | + t.Fatalf("valid=%v but error=%q", valid, msg) | | 80 | + } | | 81 | + for _, k := range []string{"operations", "queries", "mutations", "subscriptions"} { | | 82 | + if _, ok := res[k].([]any); !ok { | | 83 | + t.Fatalf("%s is %T, want []any", k, res[k]) | | 84 | + } | | 85 | + } | | 86 | + }) | | 87 | +} |
|