You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "AlinsRan (via GitHub)" <gi...@apache.org> on 2026/08/17 20:54:02 UTC

[PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

AlinsRan opened a new pull request, #13840:
URL: https://github.com/apache/apisix/pull/13840

   ### Description
   
   `graphql-limit-count` charges the quota by the query's maximum nesting depth. Depth ignores both fan-out and width — `users(first: 10000)` and `users(first: 1)` have the same depth, so they cost the same — which makes it hard to protect an upstream from the queries that actually hurt it.
   
   This adds two cost strategies next to the existing `depth`:
   
   | `cost_strategy` | cost of a field |
   |---|---|
   | `depth` | unchanged: the query's maximum nesting depth |
   | `default` | `( Σ the fields selected under it ) × mul_value + add_value` |
   | `node_quantifier` | only fields carrying a quantifier argument are charged, multiplied by how many times the field is resolved |
   
   **`depth` stays the default**, so an existing configuration keeps its current cost after the upgrade.
   
   ### Configuring the weights
   
   The per-field weights are configured inline on the plugin as `field_costs`:
   
   ```json
   {
     "cost_strategy": "node_quantifier",
     "max_cost": 5000,
     "field_costs": [
       {"field_path": "Query.products", "mul_arguments": ["first"], "add_value": 1},
       {"field_path": "Product.reviews", "mul_arguments": ["first"], "add_value": 1}
     ]
   }
   ```
   
   `add_value` / `mul_value` are the literals; `add_arguments` / `mul_arguments` name query arguments whose values fold into them, which is how a paginating argument turns into fan-out. `*_value` and `*_arguments` are the same quantity from two sources.
   
   A `field_path` is a chain of GraphQL name tokens, matched by walking it token by token:
   
   - `Product.reviews` weights `reviews` **wherever** it is selected on a `Product`.
   - `Query.products.nodes.reviews` pins the weight to **one chain** of selections and leaves a `reviews` reached any other way alone.
   
   Where a shallow and a deep path name the same field they merge key by key, longer path last, so the result is a function of the configuration rather than of the order the entries are listed in.
   
   A field with no matching entry weighs 1 and multiplies by 1, so a route with no `field_costs` is charged its node count.
   
   ### Schema introspection
   
   `field_costs` addresses a field by its GraphQL type, while the query AST carries only field names — `Person.name` cannot be told from `Vehicle.name` without the schema. The plugin issues an introspection query to the upstream on the first request that needs one, single-flights it with `resty.lock`, and caches it per worker; a schema change on a live upstream takes effect after a reload. **No introspection request is made** when `field_costs` is unset or `cost_strategy` is `depth`, so a route that does not use the cost model is unaffected.
   
   `introspection_endpoint` is there for deployments where the endpoint cannot be derived from the route's upstream. A failed introspection is remembered for 10s, so an unresponsive endpoint does not draw one upstream request per client request, and the request path derived for the call is escaped before it reaches the introspection request line.
   
   ### Also added
   
   - `max_cost` — 403 once a query costs more than this, whatever quota is left. The quota is charged first, and 403 wins over the rate-limit rejection.
   - `score_factor` — scales the raw cost before it is charged, so a cost model with large numbers still fits a sane quota.
   - `resolve_variables` (default `false`) — read a quantifier passed as a GraphQL variable, or defaulted by the schema, instead of treating it as absent.
   - `pass_all_downstream_headers` — forward the downstream headers on the introspection request; only `Authorization` is forwarded by default.
   - `X-Graphql-Query-Cost` on the response, behind the existing `show_limit_quota_header`.
   
   ### Deliberate handling worth a look
   
   - **A quantifier that is not a number is treated as absent.** `first: "ten"` and `first: true` are client-controlled; letting them reach the arithmetic would let any caller fail their own request. A numeric string still multiplies.
   - **A field the introspected schema does not declare counts as a plain unweighted node.** `__typename` is in no schema's `fields` and clients add it automatically; a schema can also drift from the running upstream. Neither should fail a request.
   - **`operationName` decides what is charged.** A document with several operations only executes the one `operationName` selects, so that is the one costed. Without it the whole document is costed and the dearest operation charged, which cannot under-charge whichever one the upstream runs.
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [x] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (`depth` remains the default; no introspection request is made unless the cost model is configured)
   
   ### Tests
   
   `t/plugin/graphql-limit-count2.t`, 16 cases:
   
   - schema validation of `cost_strategy`, `score_factor` and `field_path`, including a four-segment path
   - the engine on a paginating query under both strategies, and with no weights at all
   - fragment spreads counted per spread site, inline fragments not moving the type cursor, variables with `resolve_variables` on and off, and a deep `field_path`
   - end to end: introspection from the upstream and the cost applied; quantifiers multiplying across nesting; `resolve_variables` counting a variable and a schema argument default; `max_cost` rejecting with 403; `score_factor`; `operationName` selecting the charged operation; and `depth` still being the default strategy
   
   `t/lib/server.lua` gains a mock GraphQL upstream that answers both the introspection query and normal queries from the same endpoint, which is how the plugin derives the introspection endpoint when `introspection_endpoint` is unset.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13840:
URL: https://github.com/apache/apisix/pull/13840#discussion_r3819305663


##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -25,30 +28,115 @@ local pairs   = pairs
 local ipairs  = ipairs
 local pcall   = pcall
 local max     = math.max
+local ceil    = math.ceil
 local tonumber = tonumber
 
 local GRAPHQL_DEFAULT_MAX_SIZE = 1048576
+local QUERY_COST_HEADER = "X-Graphql-Query-Cost"
 
 local plugin_name = "graphql-limit-count"
+
+-- The plugin reuses the whole limit-count configuration surface and adds the cost
+-- model on top of it. limit_count.schema is shared with limit-count,
+-- limit-count-advanced and ai-rate-limiting, so it must not be mutated in place.
+local schema = core.table.deepcopy(limit_count.schema)
+
+schema.properties.cost_strategy = {
+    type = "string",
+    enum = {"depth", "complexity", "node_quantifier"},
+    -- "depth" is what this plugin has always done; keeping it as the default means
+    -- an existing configuration keeps its current cost after the upgrade.
+    default = "depth",
+}
+schema.properties.max_cost = {
+    type = "number",
+    minimum = 0,
+    default = 0,
+    description = "reject with 403 above this cost, 0 disables the check",
+}
+schema.properties.score_factor = {
+    type = "number",
+    exclusiveMinimum = 0,
+    default = 1,
+    description = "scaling applied to the raw cost before the quota is charged",
+}
+schema.properties.resolve_variables = {
+    type = "boolean",
+    -- On by default: with it off, moving `first: 10000` to `first: $n` makes the
+    -- same request cost a fraction of what the literal costs, which is a bypass
+    -- of max_cost and of the quota. Turn it off only to reproduce an engine that
+    -- ignores variables.
+    default = true,
+    description = "resolve GraphQL variables and schema argument defaults when " ..
+                  "computing the cost, instead of treating them as absent",
+}
+schema.properties.introspection_endpoint = {
+    type = "string",
+    pattern = "^https?://",
+    description = "explicit schema introspection endpoint, derived from the " ..
+                  "upstream when unset",
+}
+schema.properties.introspection_headers = {
+    type = "object",
+    patternProperties = {["^[^:]+$"] = {type = "string"}},
+    -- Deliberately not taken from the request. The schema is cached per service,
+    -- so an introspection whose result depends on the caller would let whichever
+    -- request warms a worker pick the schema every later request is costed
+    -- against, and would let one caller's bad credentials cache a failure that
+    -- rejects everyone else. Credentials for it belong to the operator.
+    description = "headers sent on the schema introspection request, for an " ..
+                  "upstream whose introspection needs credentials",
+}

Review Comment:
   Fixed in `d8804a5b`, and it was slightly worse than described: this plugin declared no `encrypt_fields` of its own at all, only what came along with `limit-count`'s schema deepcopy. `introspection_headers` is appended to that list, and a case asserts both it and `redis_password` are in it.



##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -146,6 +245,63 @@ local function node_depth(node, fragments, visited, memo, cycle)
 end
 
 
+-- Returns the depth, or nil plus the log line and the client message when the
+-- document is not valid GraphQL to begin with.
+local function query_depth(operations, fragments)
+    local depth = 0
+    local memo = {}
+    local cycle = {found = false}
+    for _, op in ipairs(operations) do
+        depth = max(depth, node_depth(op, fragments, {}, memo, cycle))
+    end
+
+    if cycle.found then
+        return nil, "invalid graphql request: fragment spreads form a cycle",
+               "Invalid graphql request: fragment spreads must not form cycles"
+    end
+
+    depth = max(depth, 1)
+    core.log.info("graphql node depth: ", depth)
+    return depth
+end
+
+
+-- Returns the raw cost of the query, or nil plus an error message.
+local function raw_query_cost(conf, ctx, operations, fragments, variables)
+    if conf.cost_strategy == "depth" then
+        return query_depth(operations, fragments)
+    end
+
+    -- Decorations are owned by the service; a route that is not bound to one has
+    -- no place to hang them, so the cost model simply does not apply there.
+    local service_decorations
+    if ctx.service_id then
+        service_decorations = decorations.get(ctx.service_id)
+    else
+        -- info, not warn: this is on the request path, and the effect is already
+        -- visible on every response through X-Graphql-Query-Cost
+        core.log.info("the route is not bound to a service, so it has no graphql ",
+                      "cost decorations; the query cost degenerates to the node count")
+    end
+
+    local schema_index
+    if service_decorations then
+        local err
+        schema_index, err = introspection.get(conf, ctx)
+        if not schema_index then
+            return nil, err
+        end
+    end
+
+    return gql_cost.query_cost(conf.cost_strategy, operations, fragments, {
+        decorations  = service_decorations,
+        schema       = schema_index,
+        variables    = conf.resolve_variables and variables or nil,
+        use_defaults = conf.resolve_variables,
+    })

Review Comment:
   The documentation was wrong here, not the behaviour -- fixed in `d8804a5b`. `node_quantifier` charges only nodes that carry a quantifier argument, so with no decorations there is nothing to charge and the floor gives 1. Falling back to the complexity walk would make the strategy mean something different depending on whether a decoration happens to exist, which is worse than the current answer. The docs now state what each strategy does when there are no decorations.



##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -203,23 +360,66 @@ function _M.access(conf, ctx)
         return 400, {message = "Invalid graphql request: empty graphql query"}
     end
 
-    local depth = 0
-    local memo = {}
-    local cycle = {found = false}
-    for _, op in ipairs(operations) do
-        local d = node_depth(op, fragments, {}, memo, cycle)
-        depth = max(depth, d)
+    -- A document with several operations only executes the one `operationName`
+    -- selects, so that is the one to charge for. Without it the request is not a
+    -- valid multi operation request at all; the whole document is then costed and
+    -- the most expensive operation charged, which cannot under charge whichever
+    -- one the upstream ends up running.
+    if operation_name and #operations > 1 then
+        for _, op in ipairs(operations) do
+            if op.name and op.name.value == operation_name then
+                operations = {op}
+                break
+            end
+        end
     end
 
-    if cycle.found then
-        core.log.error("invalid graphql request: fragment spreads form a cycle")
-        return 400, {message = "Invalid graphql request: fragment spreads must not form cycles"}
+    local raw_cost, client_msg
+    raw_cost, err, client_msg = raw_query_cost(conf, ctx, operations, fragments, variables)
+    if not raw_cost then
+        -- a malformed document reports its own message; anything else is the
+        -- introspection failing
+        core.log.error(client_msg and err
+                       or "failed to compute the graphql query cost: " .. err)
+        return 400, {message = client_msg
+                               or "Invalid graphql request: failed to introspect the "
+                                  .. "upstream graphql schema"}
     end
 
-    depth = max(depth, 1)
-    core.log.info("graphql query depth: ", depth)
+    -- The +0.01 floor makes a query whose nodes are all undecorated still cost 1.
+    -- "depth" is never 0 and has always charged exactly the depth, so it is left
+    -- alone: with the default score_factor of 1 the cost is unchanged.
+    if conf.cost_strategy ~= "depth" then
+        raw_cost = raw_cost + 0.01
+    end
+
+    -- ceil keeps the value an integer, which the Redis backend requires anyway
+    local cost = max(ceil(raw_cost * (conf.score_factor or 1)), 1)

Review Comment:
   Correct on the arithmetic: `max(..., 1)` already provides the minimum charge, and the epsilon rounds every integral cost up by one. It is deliberate -- part of the cost model's definition rather than a floor -- and the published cost of every documented example includes it, so dropping it means restating them all. The comment claimed to be the thing that makes an undecorated query cost 1, which was wrong; `d8804a5b` makes it say what it actually does. Happy to remove it if the cleaner arithmetic is worth restating the numbers, but that is a product call rather than a correctness one.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5352292990

   Checked all nine against the code. Seven were real and are fixed in `d8804a5b`; one is a deliberate part of the cost model whose comment was wrong; one is not reachable.
   
   **Negative quantifiers — real, and the worst of them.** Measured: a query whose honest cost is 2002002 was charged **1** by writing `first: -1` on the outer field. The negative multiplier flips the sign of everything below and the floor of 1 then charges the minimum, so the floor was the bypass rather than the protection. A quantifier is a count, so it now has to be finite and non-negative; anything else is treated as absent, which charges the structure without the fan-out. Zero stays a real count — the upstream resolves nothing under it.
   
   **Fragments under a deep index — real.** Reproduced with exactly the case @membphis gave: `Product.expensive` costs 102 alone and 3 as soon as any type-level or three-segment rule elsewhere on the service flips the index to the trie walk. The queue is now reseeded from the fragment's concrete type while the inherited pinned candidates are kept, so a deep path reaching past the fragment still applies. Both fragment forms. Regression asserts 102 under a flat index, under an unrelated three-segment rule and under an unrelated type-level rule, plus a case where a three-segment rule reaching past the fragment does apply — so the test fails if the reseed drops the inherited candidates instead of adding to them.
   
   **Shared `introspection_endpoint` cache key — real.** The service now wins as the key and the endpoint is only a fallback. The regression makes both requests inside one block, because Test::Nginx starts a fresh server per block and two separate blocks could never observe the leak; with the old key order the credential-less service is answered `200` from the other's cached schema.
   
   **`ofType` depth — real.** Four links; `[[Product!]!]!` is five. Now seven, as a standard introspection query asks. Test asserts the query's expressible depth and that a chain truncated to it still resolves.
   
   **`introspection_headers` not encrypted — real,** and slightly worse than stated: this plugin never declared `encrypt_fields` at all beyond what came along with `limit-count`'s schema deepcopy. Appended.
   
   **`node_quantifier` fallback — the documentation was wrong, not the behaviour.** That strategy charges only nodes carrying a quantifier, so with no decorations there is nothing to charge and the floor gives 1. Falling back to the complexity walk would make the strategy mean something different depending on whether a decoration exists. Docs now say what each strategy does with no decorations.
   
   **`+ 0.01` — deliberate; the comment was wrong.** You are right that `max(..., 1)` already provides the minimum charge and that this rounds every integral cost up by one. It is part of the cost model's definition rather than a floor, and every documented example's published cost includes it, so dropping it means restating them all. The comment claimed to be the thing that makes an undecorated query cost 1; it now says what it actually does. Happy to remove it if you would rather have the arithmetic clean than the published numbers stable — that is a product call, not a correctness one.
   
   **Subscription root type — not reachable.** The `graphql` 0.0.2 parser has no `subscription` production: such a document fails `parse` and the plugin answers 400 before the cost engine is entered. It is listed under the parse gaps in the description. Adding `subscriptionType` to the index would be dead code until the parser gains it.
   
   **Decoration id colliding with a Service id — not reachable.** The runtime `value.id` of a decoration is not its own id: `config_etcd` overwrites it with the key relative to the prefix, so it reads `aaa-owner/graphql_cost_decorations/hc-svc`. A Service id cannot contain a slash (`id_schema` is `^[a-zA-Z0-9-_.]+$`), so no decoration can ever match a Service id in that lookup. I first added the filter, then reverted it — a guard against something that cannot happen, with a comment saying it can, is worse than none. There is a regression pinning the invariant, since it depends on how `config_etcd` derives the id rather than on anything local to that function.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan merged PR #13840:
URL: https://github.com/apache/apisix/pull/13840


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5350866594

   Confirmed, and fixed in `67b72e57`. Both halves reproduce from the code:
   
   - `build_headers` forwarded the caller's `Authorization` (or every downstream header under `pass_all_downstream_headers`), while `schema_cache` / `failure_cache` are keyed by `conf.introspection_endpoint or ctx.service_id` — no identity dimension.
   - `fetch_schema` treats any non-200 as an error, and `fetch_and_cache` stores it under that same key for 10s. So one request whose credentials the upstream rejects answers 400 to every other caller on that worker until it expires. That is the more serious half: it needs no schema variation at all, only a bad token.
   
   I did not take the second option you offered. Keying the cache by identity makes the key space the token space, and it would still leave each identity able to poison its own entry. The design's granularity is the service — decorations and the quota both are — so the introspection has to be identity-independent to match.
   
   `pass_all_downstream_headers` is therefore gone and nothing is taken from the request. `introspection_headers` replaces it: operator-supplied headers sent verbatim, applied after `Content-Type`/`Host` so an upstream needing different ones can have them. The plugin is unreleased, so there is no compatibility to keep.
   
   Tests:
   
   - a case reaching `build_headers` through the shipped module's upvalues, asserting that a caller sending `Authorization` and `Cookie` contributes neither, and that configured headers do arrive. Restoring the forwarding reddens it.
   - an end-to-end pair against a new fixture upstream that 401s introspection without `Bearer operator-token`: the first request sends that exact token as the **client's** own `Authorization` and still gets 400 with `unexpected status 401` in the log — which is the proof it is not forwarded — and the second succeeds once the same value is configured as `introspection_headers`.
   
   On the "outside the request path" part: with credentials now static, prefetching becomes possible, but it still needs a `/services`-watcher-driven registry and a refresher timer. That is the P2 (1) I said I would track separately, and this fix does not depend on it.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13840:
URL: https://github.com/apache/apisix/pull/13840#discussion_r3819302745


##########
apisix/plugins/graphql-limit-count/cost.lua:
##########
@@ -0,0 +1,527 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- GraphQL query cost engine.
+--
+-- Implements the `complexity` and `node_quantifier` cost strategies on the AST
+-- produced by the `graphql` rock.
+--
+-- Cost decorations are keyed by a position in the schema graph -- a `<GraphQL
+-- type>`, or a `<GraphQL type>` followed by the chain of fields that reaches it
+-- -- so matching a query field against a decoration requires the upstream schema:
+-- the walker carries a type cursor that descends alongside the selection tree.
+-- Without a schema no decoration can match and every node falls back to its
+-- default weights.
+--
+local core   = require("apisix.core")
+
+local ipairs     = ipairs
+local type       = type
+local tonumber   = tonumber
+local math_max   = math.max
+local str_gmatch = string.gmatch
+local tab_sort   = table.sort
+
+local _M = {}
+
+-- the fields a decoration contributes; everything else on the value is metadata
+local WEIGHT_KEYS = {"add_value", "mul_value", "add_arguments", "mul_arguments"}
+
+local EMPTY_INDEX = {flat = {}, root = {children = {}}, deep = false}
+
+-- How many selections the walk may expand. A real query is in the hundreds; a
+-- document that spreads fragments into an exponential DAG reaches this in
+-- milliseconds and is rejected instead of costed.
+local EXPANSION_BUDGET = 100000
+
+
+local function field_name(node)
+    return node.name and node.name.value
+end
+
+
+-- A field_path is a chain of GraphQL name tokens naming a position in the schema
+-- graph. `Person` weights every field that returns a `Person`; `Person.name`
+-- weights the field `name` wherever it is selected on a `Person`;
+-- `Query.products.nodes.reviews` pins one specific chain of fields. All three live
+-- in the same trie, one node per token, so matching is a walk rather than a string
+-- compare.
+local function insert_path(root, deco)
+    local node = root
+    local depth = 0
+
+    for token in str_gmatch(deco.field_path, "[^.]+") do
+        depth = depth + 1
+        local children = node.children
+        if not children then
+            children = {}
+            node.children = children
+        end
+
+        local child = children[token]
+        if not child then
+            child = {}
+            children[token] = child
+        end
+        node = child
+    end
+
+    -- an empty path names nothing; decorating the trie root would weight every
+    -- field in the document
+    if depth == 0 then
+        return 0
+    end
+
+    node.decoration = deco
+    node.depth = depth
+    return depth
+end
+
+
+---
+-- Compiles a service's decorations into the index `query_cost` consumes. Built
+-- once per configuration version, not per request.
+-- @tparam table list decoration values, each carrying a `field_path`
+function _M.build_index(list)
+    local index = {flat = {}, root = {children = {}}, deep = false}
+
+    for _, deco in ipairs(list or {}) do
+        if type(deco.field_path) == "string" then
+            local depth = insert_path(index.root, deco)
+            if depth == 2 then
+                index.flat[deco.field_path] = deco
+            elseif depth ~= 0 then
+                -- a single token is a type level rule and a longer path is pinned
+                -- to a chain; neither can be answered by the `<type>.<field>`
+                -- lookup, so the whole index falls back to the trie walk
+                index.deep = true
+            end
+        end
+    end
+
+    return index
+end
+
+
+-- Advances the candidate paths for one field: the ones inherited from the parent
+-- move on by this field's name, and a fresh candidate is seeded from the root by
+-- this field's own type. Seeding by type is what lets a two segment path match at
+-- any depth, while carrying the parent's candidates forward keeps a longer path
+-- pinned to its chain.
+local function advance(root, queue, type_name, name)
+    local next_queue
+
+    if queue then
+        for _, candidate in ipairs(queue) do
+            local children = candidate.children
+            local child = children and children[name]
+            if child then
+                next_queue = next_queue or {}
+                next_queue[#next_queue + 1] = child
+            end
+        end
+    end
+
+    local seed = type_name and root.children[type_name]
+    if seed then
+        next_queue = next_queue or {}
+        next_queue[#next_queue + 1] = seed
+    end
+
+    return next_queue
+end
+
+
+-- Two paths can name the same field: `Product.reviews` and
+-- `Query.products.nodes.reviews` both match one node. They are merged key by key,
+-- least specific first, so the longer path wins wherever they disagree. Resolving
+-- by path length rather than by storage order keeps the cost a function of the
+-- configuration alone.
+local function matched_decoration(queue)
+    if not queue then
+        return nil
+    end
+
+    local first, overlapping
+    for _, candidate in ipairs(queue) do
+        if candidate.decoration then
+            if not first then
+                first = candidate
+            else
+                overlapping = overlapping or {first}
+                overlapping[#overlapping + 1] = candidate
+            end
+        end
+    end
+
+    if not first then
+        return nil
+    end
+
+    if not overlapping then
+        return first.decoration
+    end
+
+    tab_sort(overlapping, function (a, b)
+        return a.depth < b.depth
+    end)
+
+    local merged = {}
+    for _, candidate in ipairs(overlapping) do
+        for _, key in ipairs(WEIGHT_KEYS) do
+            local value = candidate.decoration[key]
+            if value ~= nil then
+                merged[key] = value
+            end
+        end
+    end
+
+    return merged
+end
+
+
+-- Resolves a GraphQL argument literal to a Lua value.
+-- Only scalar literals carry a `.value`; list / inputObject nodes do not and are
+-- therefore reported as absent. A variable resolves only under
+-- `resolve_variables`, from the request's `variables` map first and then from the
+-- default the operation declares for it.
+local function literal_value(state, value_node)
+    if type(value_node) ~= "table" then
+        return nil
+    end
+
+    if value_node.kind == "variable" then
+        local name = value_node.name and value_node.name.value
+        if not name then
+            return nil
+        end
+
+        if state.variables then
+            local value = state.variables[name]
+            if value ~= nil then
+                return value
+            end
+        end
+
+        -- The client did not supply the variable, so the upstream executes with
+        -- the default the operation itself declares. Reading only the supplied
+        -- map would let `query Q($n: Int = 10000)` sent with no variables at all
+        -- cost the same as an absent quantifier -- the bypass `resolve_variables`
+        -- exists to close, reached through the document instead of the map.
+        if state.use_defaults then
+            local var_default = state.var_defaults and state.var_defaults[name]
+            return var_default and var_default.value
+        end
+
+        return nil
+    end
+
+    return value_node.value
+end
+
+
+local function argument_literal(state, node, name)
+    local args = node.arguments
+    if not args then
+        return nil
+    end
+
+    for _, arg in ipairs(args) do
+        if arg.name and arg.name.value == name then
+            return literal_value(state, arg.value)
+        end
+    end
+
+    return nil
+end
+
+
+-- Returns the numeric value of `name` for this node, or nil when the argument is
+-- absent or not usable in arithmetic. `first: "ten"` is client-controlled, so it
+-- must not reach the arithmetic: treating it as absent keeps the request on the
+-- fast path instead of turning a client-controlled value into a 500.
+local function resolve_argument(state, node, name, field_def)
+    local value = argument_literal(state, node, name)
+
+    -- By default a query that omits a paginating argument is free, because the
+    -- schema default is not consulted; `resolve_variables` opts into reading it.
+    if value == nil and state.use_defaults and field_def and field_def.args then
+        local arg_def = field_def.args[name]
+        value = arg_def and arg_def.default_value
+    end
+
+    return tonumber(value)

Review Comment:
   Fixed in `d8804a5b`. Confirmed first: a query whose honest cost is 2002002 was charged **1** by writing `first: -1` on the outer field, so the floor was the bypass rather than the protection. `resolve_argument` now rejects anything that is not a finite, non-negative count and treats it as absent, which charges the structure without the fan-out. Zero stays a real count -- the upstream resolves nothing under it. Regression in `t/plugin/graphql-limit-count2.t`, and removing the check reddens it.



##########
apisix/plugins/graphql-limit-count/cost.lua:
##########
@@ -0,0 +1,527 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- GraphQL query cost engine.
+--
+-- Implements the `complexity` and `node_quantifier` cost strategies on the AST
+-- produced by the `graphql` rock.
+--
+-- Cost decorations are keyed by a position in the schema graph -- a `<GraphQL
+-- type>`, or a `<GraphQL type>` followed by the chain of fields that reaches it
+-- -- so matching a query field against a decoration requires the upstream schema:
+-- the walker carries a type cursor that descends alongside the selection tree.
+-- Without a schema no decoration can match and every node falls back to its
+-- default weights.
+--
+local core   = require("apisix.core")
+
+local ipairs     = ipairs
+local type       = type
+local tonumber   = tonumber
+local math_max   = math.max
+local str_gmatch = string.gmatch
+local tab_sort   = table.sort
+
+local _M = {}
+
+-- the fields a decoration contributes; everything else on the value is metadata
+local WEIGHT_KEYS = {"add_value", "mul_value", "add_arguments", "mul_arguments"}
+
+local EMPTY_INDEX = {flat = {}, root = {children = {}}, deep = false}
+
+-- How many selections the walk may expand. A real query is in the hundreds; a
+-- document that spreads fragments into an exponential DAG reaches this in
+-- milliseconds and is rejected instead of costed.
+local EXPANSION_BUDGET = 100000
+
+
+local function field_name(node)
+    return node.name and node.name.value
+end
+
+
+-- A field_path is a chain of GraphQL name tokens naming a position in the schema
+-- graph. `Person` weights every field that returns a `Person`; `Person.name`
+-- weights the field `name` wherever it is selected on a `Person`;
+-- `Query.products.nodes.reviews` pins one specific chain of fields. All three live
+-- in the same trie, one node per token, so matching is a walk rather than a string
+-- compare.
+local function insert_path(root, deco)
+    local node = root
+    local depth = 0
+
+    for token in str_gmatch(deco.field_path, "[^.]+") do
+        depth = depth + 1
+        local children = node.children
+        if not children then
+            children = {}
+            node.children = children
+        end
+
+        local child = children[token]
+        if not child then
+            child = {}
+            children[token] = child
+        end
+        node = child
+    end
+
+    -- an empty path names nothing; decorating the trie root would weight every
+    -- field in the document
+    if depth == 0 then
+        return 0
+    end
+
+    node.decoration = deco
+    node.depth = depth
+    return depth
+end
+
+
+---
+-- Compiles a service's decorations into the index `query_cost` consumes. Built
+-- once per configuration version, not per request.
+-- @tparam table list decoration values, each carrying a `field_path`
+function _M.build_index(list)
+    local index = {flat = {}, root = {children = {}}, deep = false}
+
+    for _, deco in ipairs(list or {}) do
+        if type(deco.field_path) == "string" then
+            local depth = insert_path(index.root, deco)
+            if depth == 2 then
+                index.flat[deco.field_path] = deco
+            elseif depth ~= 0 then
+                -- a single token is a type level rule and a longer path is pinned
+                -- to a chain; neither can be answered by the `<type>.<field>`
+                -- lookup, so the whole index falls back to the trie walk
+                index.deep = true
+            end
+        end
+    end
+
+    return index
+end
+
+
+-- Advances the candidate paths for one field: the ones inherited from the parent
+-- move on by this field's name, and a fresh candidate is seeded from the root by
+-- this field's own type. Seeding by type is what lets a two segment path match at
+-- any depth, while carrying the parent's candidates forward keeps a longer path
+-- pinned to its chain.
+local function advance(root, queue, type_name, name)
+    local next_queue
+
+    if queue then
+        for _, candidate in ipairs(queue) do
+            local children = candidate.children
+            local child = children and children[name]
+            if child then
+                next_queue = next_queue or {}
+                next_queue[#next_queue + 1] = child
+            end
+        end
+    end
+
+    local seed = type_name and root.children[type_name]
+    if seed then
+        next_queue = next_queue or {}
+        next_queue[#next_queue + 1] = seed
+    end
+
+    return next_queue
+end
+
+
+-- Two paths can name the same field: `Product.reviews` and
+-- `Query.products.nodes.reviews` both match one node. They are merged key by key,
+-- least specific first, so the longer path wins wherever they disagree. Resolving
+-- by path length rather than by storage order keeps the cost a function of the
+-- configuration alone.
+local function matched_decoration(queue)
+    if not queue then
+        return nil
+    end
+
+    local first, overlapping
+    for _, candidate in ipairs(queue) do
+        if candidate.decoration then
+            if not first then
+                first = candidate
+            else
+                overlapping = overlapping or {first}
+                overlapping[#overlapping + 1] = candidate
+            end
+        end
+    end
+
+    if not first then
+        return nil
+    end
+
+    if not overlapping then
+        return first.decoration
+    end
+
+    tab_sort(overlapping, function (a, b)
+        return a.depth < b.depth
+    end)
+
+    local merged = {}
+    for _, candidate in ipairs(overlapping) do
+        for _, key in ipairs(WEIGHT_KEYS) do
+            local value = candidate.decoration[key]
+            if value ~= nil then
+                merged[key] = value
+            end
+        end
+    end
+
+    return merged
+end
+
+
+-- Resolves a GraphQL argument literal to a Lua value.
+-- Only scalar literals carry a `.value`; list / inputObject nodes do not and are
+-- therefore reported as absent. A variable resolves only under
+-- `resolve_variables`, from the request's `variables` map first and then from the
+-- default the operation declares for it.
+local function literal_value(state, value_node)
+    if type(value_node) ~= "table" then
+        return nil
+    end
+
+    if value_node.kind == "variable" then
+        local name = value_node.name and value_node.name.value
+        if not name then
+            return nil
+        end
+
+        if state.variables then
+            local value = state.variables[name]
+            if value ~= nil then
+                return value
+            end
+        end
+
+        -- The client did not supply the variable, so the upstream executes with
+        -- the default the operation itself declares. Reading only the supplied
+        -- map would let `query Q($n: Int = 10000)` sent with no variables at all
+        -- cost the same as an absent quantifier -- the bypass `resolve_variables`
+        -- exists to close, reached through the document instead of the map.
+        if state.use_defaults then
+            local var_default = state.var_defaults and state.var_defaults[name]
+            return var_default and var_default.value
+        end
+
+        return nil
+    end
+
+    return value_node.value
+end
+
+
+local function argument_literal(state, node, name)
+    local args = node.arguments
+    if not args then
+        return nil
+    end
+
+    for _, arg in ipairs(args) do
+        if arg.name and arg.name.value == name then
+            return literal_value(state, arg.value)
+        end
+    end
+
+    return nil
+end
+
+
+-- Returns the numeric value of `name` for this node, or nil when the argument is
+-- absent or not usable in arithmetic. `first: "ten"` is client-controlled, so it
+-- must not reach the arithmetic: treating it as absent keeps the request on the
+-- fast path instead of turning a client-controlled value into a 500.
+local function resolve_argument(state, node, name, field_def)
+    local value = argument_literal(state, node, name)
+
+    -- By default a query that omits a paginating argument is free, because the
+    -- schema default is not consulted; `resolve_variables` opts into reading it.
+    if value == nil and state.use_defaults and field_def and field_def.args then
+        local arg_def = field_def.args[name]
+        value = arg_def and arg_def.default_value
+    end
+
+    return tonumber(value)
+end
+
+
+local function node_weights(state, node, deco, field_def)
+    if not deco then
+        return 1, 1, false
+    end
+
+    local node_add = deco.add_value or 1
+    local node_mul = deco.mul_value or 1
+    local has_quantifier = false
+
+    if deco.add_arguments then
+        for _, name in ipairs(deco.add_arguments) do
+            local value = resolve_argument(state, node, name, field_def)
+            if value then
+                node_add = node_add + value
+            end
+        end
+    end
+
+    if deco.mul_arguments then
+        for _, name in ipairs(deco.mul_arguments) do
+            local value = resolve_argument(state, node, name, field_def)
+            if value then
+                node_mul = node_mul * value
+                has_quantifier = true
+            end
+        end
+    end
+
+    return node_add, node_mul, has_quantifier
+end
+
+
+local each_field
+-- Calls fn(child, decoration, field_def, child_type, child_queue) for every field
+-- selected by `node`, where `sel_type` is the GraphQL type the selection set
+-- belongs to and `queue` the decoration paths still live at this point.
+--
+-- Fragments are transparent: they carry no cost of their own and they do not move
+-- the type cursor or the decoration paths. `... on Droid { name }` therefore
+-- matches `<parent type>.name` rather than `Droid.name`: the walk never sees the
+-- fragment node, so its `typeCondition` does not take effect.
+-- A fragment's `typeCondition` narrows the selection to a concrete type, so the
+-- fields inside it belong to that type and not to the abstract one the parent
+-- selected. Without moving the cursor, `... on Product { expensive }` under an
+-- interface looks `expensive` up on the interface, misses a `Product.expensive`
+-- weight and undercharges the query. Only move it when the schema actually knows
+-- the type, so an unknown condition degrades instead of losing the cursor.
+local function fragment_type(state, node, sel_type)
+    local condition = node.typeCondition
+    local name = condition and condition.name and condition.name.value
+    if name and state.types and state.types[name] then
+        return name
+    end
+
+    return sel_type
+end
+
+
+function each_field(state, node, sel_type, queue, fn)
+    local selection_set = node.selectionSet
+    if not selection_set or not selection_set.selections then
+        return
+    end
+
+    for _, sel in ipairs(selection_set.selections) do
+        local kind = sel.kind
+
+        -- Spreading a fragment twice legitimately costs twice, so an acyclic chain
+        -- of fragments that each spread the previous one twice expands
+        -- exponentially: a document under a kilobyte can otherwise burn seconds of
+        -- CPU in the access phase, before any limit has been applied. Bound the
+        -- expansion instead, and let the caller reject the request.
+        state.budget = state.budget - 1
+        if state.budget < 0 then
+            state.exhausted = true
+            return
+        end
+
+        if kind == "field" then
+            local name = field_name(sel)
+            local deco, field_def, child_type, child_queue
+            if name and sel_type then
+                local type_def = state.types and state.types[sel_type]
+                field_def = type_def and type_def.fields[name]
+                child_type = field_def and field_def.type
+
+                if state.deep then
+                    child_queue = advance(state.root, queue, child_type, name)
+                    deco = matched_decoration(child_queue)
+                else
+                    -- every path is `<type>.<field>`, so the walk has at most one
+                    -- live candidate and collapses to a single lookup
+                    deco = state.flat[sel_type .. "." .. name]
+                end
+            end
+            fn(sel, deco, field_def, child_type, child_queue)
+
+        elseif kind == "inlineFragment" then
+            each_field(state, sel, fragment_type(state, sel, sel_type), queue, fn)

Review Comment:
   Fixed in `d8804a5b`. Reproduced exactly as described: `Product.expensive` costs 102 alone and 3 once any type-level or three-segment rule elsewhere on the Service flips the index to the trie walk. Both fragment forms now reseed the candidate queue from the fragment's concrete type while keeping the inherited pinned candidates, so a deep path reaching past the fragment still applies. The regression asserts 102 under a flat index, under an unrelated three-segment rule and under an unrelated type-level rule, plus a case where a three-segment rule reaching past the fragment does apply -- so it fails if the reseed replaces the inherited candidates instead of adding to them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13840:
URL: https://github.com/apache/apisix/pull/13840#discussion_r3819304103


##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }
+}
+
+query {
+    __schema {
+        queryType { name }
+        mutationType { name }
+        types {
+            ...TypeAttr
+            fields {
+                name
+                args { name defaultValue type { ...WrappedTypeRef } }
+                type { ...WrappedTypeRef }
+            }
+        }
+    }
+}
+]]
+
+-- Per-worker, keyed by the owning service (or by the explicit endpoint when one
+-- is configured). Never invalidated: an upstream schema change only takes effect
+-- after a reload. Keying on the service rather than on the derived endpoint is
+-- what bounds the table: the endpoint embeds the
+-- request path, so a route matching many paths would otherwise add a permanent
+-- entry -- and trigger an upstream introspection -- for every distinct path.
+local schema_cache = {}
+-- cache_key -> {err = <string>, expire_at = <number>}
+local failure_cache = {}
+
+local _M = {}
+
+
+local function unwrap_type_name(type_ref)
+    -- NON_NULL / LIST wrappers have no name of their own; descend to the named type.
+    while type_ref do
+        if type_ref.name then
+            return type_ref.name
+        end
+        type_ref = type_ref.ofType
+    end
+
+    return nil
+end
+
+
+local function build_index(schema_data)
+    local introspected = schema_data and schema_data.__schema
+    if not introspected or type(introspected.types) ~= "table" then
+        return nil, "introspection response has no __schema.types"
+    end
+
+    local types = {}
+    for _, type_def in ipairs(introspected.types) do
+        if type_def.name and type_def.fields then
+            local fields = {}
+            for _, field in ipairs(type_def.fields) do
+                if field.name then
+                    local args
+                    if field.args then
+                        for _, arg in ipairs(field.args) do
+                            if arg.name and arg.defaultValue ~= nil then
+                                args = args or {}
+                                args[arg.name] = {default_value = arg.defaultValue}
+                            end
+                        end
+                    end
+
+                    fields[field.name] = {
+                        type = unwrap_type_name(field.type),
+                        args = args,
+                    }
+                end
+            end
+            types[type_def.name] = {fields = fields}
+        end
+    end
+
+    return {
+        query_type    = introspected.queryType and introspected.queryType.name or "Query",
+        mutation_type = introspected.mutationType and introspected.mutationType.name
+                        or "Mutation",
+        types         = types,
+    }
+end
+
+
+-- Picks the node the introspection request is sent to. APISIX runs the access
+-- phase before the balancer, so there is no resolved peer to reuse:
+-- the nodes are sorted and the first one is taken so repeated introspections of
+-- the same upstream are stable. Deployments where that is not good enough (service
+-- discovery, a separate introspection path) set `introspection_endpoint`.
+local function first_node(nodes, scheme)
+    local candidates = {}
+    local default_port = (scheme == "https" or scheme == "grpcs") and 443 or 80
+
+    if core.table.isarray(nodes) then
+        for _, node in ipairs(nodes) do
+            if node.host then
+                core.table.insert(candidates, node.host .. ":" .. (node.port or default_port))
+            end
+        end
+    else
+        for addr in pairs(nodes) do
+            core.table.insert(candidates, addr)
+        end
+    end
+
+    if #candidates == 0 then
+        return nil
+    end
+
+    tab_sort(candidates)
+    return candidates[1]
+end
+
+
+-- Returns the endpoint to introspect and the Host header to send with it, or
+-- nil plus an error message.
+local function resolve_endpoint(conf, ctx)
+    if conf.introspection_endpoint then
+        return core.utils.escape_uri_control_chars(conf.introspection_endpoint)
+    end
+
+    local route = ctx.matched_route and ctx.matched_route.value
+    if not route then
+        return nil, "no matched route to derive the introspection endpoint from"
+    end
+
+    -- The schema is cached per service, so prefer the service's own upstream:
+    -- merge_service_route lets a route override it, and two routes on one service
+    -- with different overrides would otherwise share whichever schema was fetched
+    -- first. Falls back to the route for a route-only upstream.
+    local up_conf
+    local service = ctx.service_id and service_fetch(ctx.service_id)
+    if service and service.value then
+        up_conf = service.value.upstream
+        if not up_conf and service.value.upstream_id then
+            up_conf = upstream.get_by_id(service.value.upstream_id)
+        end
+    end
+
+    if not up_conf then
+        up_conf = route.upstream
+        if not up_conf and route.upstream_id then
+            up_conf = upstream.get_by_id(route.upstream_id)
+        end
+    end
+
+    if not up_conf or not up_conf.nodes then
+        return nil, "the route has no upstream nodes, set introspection_endpoint"
+    end
+
+    local scheme = up_conf.scheme or "http"
+    local addr = first_node(up_conf.nodes, scheme)
+    if not addr then
+        return nil, "the route has no upstream nodes, set introspection_endpoint"
+    end
+
+    local path = ctx.var.upstream_uri
+    if not path or path == "" then
+        path = ctx.var.uri
+    end
+    local query_pos = str_find(path, "?", 1, true)
+    if query_pos then
+        path = path:sub(1, query_pos - 1)
+    end
+    -- $uri is already percent-decoded, so a request path carrying %0d%0a would
+    -- otherwise reach the request line of the introspection call verbatim. Same
+    -- treatment $upstream_uri gets in init.lua.
+    path = core.utils.escape_uri_control_chars(path)
+
+    -- Mirror what proxied traffic sends, so a virtual-hosted upstream answers the
+    -- introspection the same way it answers the query. `node` means "use the node
+    -- address", which is what request_uri does on its own. For `pass` the template
+    -- leaves $upstream_host at $http_host (cli/ngx_tpl.lua:810) -- the verbatim
+    -- header including any port -- so $host, which is lowercased and port-stripped,
+    -- would reach a different vhost than the proxied request does.
+    local host
+    if up_conf.pass_host == "rewrite" then
+        host = up_conf.upstream_host
+    elseif up_conf.pass_host ~= "node" then
+        host = ctx.var.http_host or ctx.var.host
+    end
+
+    if host and not core.utils.validate_header_value(host) then
+        return nil, "the upstream host is not a valid header value"
+    end
+
+    return scheme .. "://" .. addr .. path, nil, host
+end
+
+
+-- Nothing here comes from the request. The schema is cached per service, so an
+-- introspection that varied with the caller would mean the request which happens
+-- to warm a worker picks the schema every later request on it is costed against,
+-- and one caller's rejected credentials would cache a failure that answers 400 to
+-- everyone else for as long as it lives. Introspection is the operator's call to
+-- the upstream, made with the operator's credentials.
+local function build_headers(conf, host)
+    local headers = {["Content-Type"] = "application/json"}
+
+    -- request_uri would otherwise send the node address as the Host, which breaks
+    -- a virtual-hosted GraphQL upstream that proxied traffic reaches fine
+    if host then
+        headers["Host"] = host
+    end
+
+    -- applied last, so an operator who needs a different Host or Content-Type on
+    -- the introspection request can say so
+    for name, value in pairs(conf.introspection_headers or {}) do
+        headers[name] = value
+    end
+
+    return headers
+end
+
+
+local function fetch_schema(conf, endpoint, host)
+    local httpc, err = http.new()
+    if not httpc then
+        return nil, "failed to create http client: " .. err
+    end
+
+    -- The single-flight lock is held for the whole fetch, so without an explicit
+    -- timeout an unresponsive endpoint would park every other request on the lock
+    -- until the OpenResty default (60s) expired. Bounded, not configurable.
+    httpc:set_timeouts(INTROSPECTION_CONNECT_TIMEOUT, INTROSPECTION_SEND_TIMEOUT,
+                       INTROSPECTION_READ_TIMEOUT)
+
+    local res
+    res, err = httpc:request_uri(endpoint, {
+        method  = "POST",
+        body    = core.json.encode({query = INTROSPECTION_QUERY}),
+        headers = build_headers(conf, host),
+        -- The introspection target is the upstream this route already proxies to,
+        -- whose certificate the proxy path does not verify either.
+        ssl_verify = false,
+        keepalive  = false,
+    })
+
+    if not res then
+        return nil, "failed to request " .. endpoint .. ": " .. err
+    end
+
+    if res.status ~= 200 then
+        return nil, "unexpected status " .. res.status .. " from " .. endpoint
+    end
+
+    local body
+    body, err = core.json.decode(res.body, {null_as_nil = true})
+    if not body then
+        return nil, "failed to decode the introspection response: " .. (err or "not an object")
+    end
+
+    if not body.data then
+        return nil, "introspection response has no data field"
+    end
+
+    return build_index(body.data)
+end
+
+
+local function fetch_and_cache(conf, cache_key, endpoint, host)
+    local lock, err = resty_lock:new(LOCK_SHDICT_NAME)
+    if not lock then
+        core.log.warn("failed to create the introspection lock: ", err,
+                      ", fetching the schema without single-flight protection")
+        return fetch_schema(conf, endpoint, host)
+    end
+
+    local elapsed
+    elapsed, err = lock:lock("graphql-schema#" .. cache_key)
+    if not elapsed then
+        core.log.warn("failed to acquire the introspection lock: ", err,
+                      ", fetching the schema without single-flight protection")
+        return fetch_schema(conf, endpoint, host)
+    end
+
+    -- another request may have populated the cache while this one waited
+    local cached = schema_cache[cache_key]
+    if cached then
+        lock:unlock()
+        return cached
+    end
+
+    local schema, ferr = fetch_schema(conf, endpoint, host)
+    if schema then
+        schema_cache[cache_key] = schema
+        failure_cache[cache_key] = nil
+    else
+        failure_cache[cache_key] = {
+            err = ferr,
+            expire_at = ngx_now() + INTROSPECTION_FAILURE_TTL,
+        }
+    end
+    lock:unlock()
+
+    return schema, ferr
+end
+
+
+---
+-- Returns the schema index for the upstream this request is routed to.
+function _M.get(conf, ctx)
+    -- The derived endpoint embeds the request path, so it is not a safe cache key
+    -- (a route matching many paths would grow the table without bound). The schema
+    -- belongs to the upstream, and decorations only exist per service, so the
+    -- service is both the correct and the bounded key. An explicit endpoint is its
+    -- own key: it does not vary per request.
+    local cache_key = conf.introspection_endpoint
+                      or (ctx.service_id and tostring(ctx.service_id))

Review Comment:
   Fixed in `d8804a5b`. The Service now wins as the cache key and the explicit endpoint is only a fallback for a caller that has no Service. The regression makes both requests inside one test block, because Test::Nginx starts a fresh server per block and two separate blocks could never observe the leak; with the old key order the credential-less Service is answered 200 from the other's cached schema.



##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }

Review Comment:
   Fixed in `d8804a5b`. The query asks for seven `ofType` links now, as a standard introspection query does -- `[[Product!]!]!` alone is five. The regression asserts the depth the query can express and that a chain truncated to it still resolves to a name.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5352054762

   Reviewed head: `67b72e5774565fcf4c87255955b4595fff971681`
   
   ### [P1] `deep` index mode drops concrete-type decorations inside fragments
   
   `TEST 42` verifies that a fragment `typeCondition` moves `sel_type`, but its decoration set contains only `Product.expensive`. That path has depth 2, so `build_index` leaves `index.deep = false` and the test exercises only the flat `<type>.<field>` lookup.
   
   As soon as the same Service has any supported type-level or deeper path, `index.deep` becomes true and `each_field` stops using the flat map. Entering `... on Product` updates `sel_type` but passes the existing candidate `queue` through unchanged. For `expensive`, the deep branch then calls `advance(state.root, queue, child_type, name)`; it seeds from the field's return type (for example, `String`), not from the fragment's concrete selection type (`Product`). Consequently, `Product.expensive` is not matched.
   
   A minimal regression is a decoration index containing both:
   
   ```lua
   {field_path = "Product.expensive", add_value = 100},
   {field_path = "Query.node.id", add_value = 9},
   ```
   
   For:
   
   ```graphql
   query { node { ... on Product { expensive } } }
   ```
   
   the unrelated deep path switches the whole index to deep mode, and the query is charged as three plain nodes instead of applying the `Product.expensive` weight. The named-fragment form follows the same path.
   
   This allows the same field to evade its configured weight based on fragment syntax and the presence of another decoration, so `max_cost` and quota charging can undercount client-controlled queries. Please realign/reseed the candidate queue when a fragment changes the concrete type (while preserving still-live pinned candidates), and add inline- and named-fragment regressions with mixed depth-2 and deep/type-level decorations.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5325573561

   Reviewed head: `d4483b2495d2f072841cadd16546bfedaee88a57`
   
   I found three P1 merge blockers:
   
   ### [P1] Fragment DAGs can make query-cost computation expand exponentially
   
   `each_field` recursively expands every `fragmentSpread`. `state.visiting` prevents cycles, but it neither memoizes nor bounds acyclic repeated spreads. A compact acyclic chain in which each fragment spreads the previous fragment twice causes exponential work in the access phase before rate limiting is applied. Please memoize fragment cost using the necessary traversal state, or enforce a strict expanded-node/work limit and reject oversized expansion with a 4xx response. Add a regression proving that an acyclic repeated-fragment DAG has bounded processing cost.
   
   ### [P1] Fragment type conditions do not update the schema type cursor
   
   Both `inlineFragment` and `fragmentSpread` recurse with the parent `sel_type`; the fragment `typeCondition` is ignored. For an interface or union query, `... on Product { expensiveField }` therefore still looks up the field under the parent abstract type, so a `Product.expensiveField` decoration can fail to match and the query is undercharged. Please update the type cursor from `typeCondition.name.value` when entering either fragment form, validate compatibility, and add tests where the parent abstract type differs from the concrete decorated type.
   
   ### [P1] Default variable handling lets clients bypass argument-based cost
   
   `resolve_variables` defaults to `false`, and variable nodes are treated as missing arguments. A client can move the same fan-out value from `first: 10000` to `first: $n` and avoid the configured `add_arguments` or `mul_arguments`, causing `max_cost` and quota charging to underestimate the request. Please make variable resolution the safe default for the new cost strategies, or conservatively reject/charge unresolved decorated variables. Add literal-versus-variable equivalence tests for both cost strategies.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13840:
URL: https://github.com/apache/apisix/pull/13840#discussion_r3819003903


##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -203,23 +360,66 @@ function _M.access(conf, ctx)
         return 400, {message = "Invalid graphql request: empty graphql query"}
     end
 
-    local depth = 0
-    local memo = {}
-    local cycle = {found = false}
-    for _, op in ipairs(operations) do
-        local d = node_depth(op, fragments, {}, memo, cycle)
-        depth = max(depth, d)
+    -- A document with several operations only executes the one `operationName`
+    -- selects, so that is the one to charge for. Without it the request is not a
+    -- valid multi operation request at all; the whole document is then costed and
+    -- the most expensive operation charged, which cannot under charge whichever
+    -- one the upstream ends up running.
+    if operation_name and #operations > 1 then
+        for _, op in ipairs(operations) do
+            if op.name and op.name.value == operation_name then
+                operations = {op}
+                break
+            end
+        end
     end
 
-    if cycle.found then
-        core.log.error("invalid graphql request: fragment spreads form a cycle")
-        return 400, {message = "Invalid graphql request: fragment spreads must not form cycles"}
+    local raw_cost, client_msg
+    raw_cost, err, client_msg = raw_query_cost(conf, ctx, operations, fragments, variables)
+    if not raw_cost then
+        -- a malformed document reports its own message; anything else is the
+        -- introspection failing
+        core.log.error(client_msg and err
+                       or "failed to compute the graphql query cost: " .. err)
+        return 400, {message = client_msg
+                               or "Invalid graphql request: failed to introspect the "
+                                  .. "upstream graphql schema"}
     end
 
-    depth = max(depth, 1)
-    core.log.info("graphql query depth: ", depth)
+    -- The +0.01 floor makes a query whose nodes are all undecorated still cost 1.
+    -- "depth" is never 0 and has always charged exactly the depth, so it is left
+    -- alone: with the default score_factor of 1 the cost is unchanged.
+    if conf.cost_strategy ~= "depth" then
+        raw_cost = raw_cost + 0.01
+    end
+
+    -- ceil keeps the value an integer, which the Redis backend requires anyway
+    local cost = max(ceil(raw_cost * (conf.score_factor or 1)), 1)

Review Comment:
   The `+ 0.01` does not merely floor zero: because the result is immediately passed to `ceil`, every integral raw cost is incremented (for example, 51 becomes 52). The existing `max(..., 1)` already provides the minimum charge, so this currently distorts `score_factor`, quota usage, headers, and `max_cost` thresholds.



##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -146,6 +245,63 @@ local function node_depth(node, fragments, visited, memo, cycle)
 end
 
 
+-- Returns the depth, or nil plus the log line and the client message when the
+-- document is not valid GraphQL to begin with.
+local function query_depth(operations, fragments)
+    local depth = 0
+    local memo = {}
+    local cycle = {found = false}
+    for _, op in ipairs(operations) do
+        depth = max(depth, node_depth(op, fragments, {}, memo, cycle))
+    end
+
+    if cycle.found then
+        return nil, "invalid graphql request: fragment spreads form a cycle",
+               "Invalid graphql request: fragment spreads must not form cycles"
+    end
+
+    depth = max(depth, 1)
+    core.log.info("graphql node depth: ", depth)
+    return depth
+end
+
+
+-- Returns the raw cost of the query, or nil plus an error message.
+local function raw_query_cost(conf, ctx, operations, fragments, variables)
+    if conf.cost_strategy == "depth" then
+        return query_depth(operations, fragments)
+    end
+
+    -- Decorations are owned by the service; a route that is not bound to one has
+    -- no place to hang them, so the cost model simply does not apply there.
+    local service_decorations
+    if ctx.service_id then
+        service_decorations = decorations.get(ctx.service_id)
+    else
+        -- info, not warn: this is on the request path, and the effect is already
+        -- visible on every response through X-Graphql-Query-Cost
+        core.log.info("the route is not bound to a service, so it has no graphql ",
+                      "cost decorations; the query cost degenerates to the node count")
+    end
+
+    local schema_index
+    if service_decorations then
+        local err
+        schema_index, err = introspection.get(conf, ctx)
+        if not schema_index then
+            return nil, err
+        end
+    end
+
+    return gql_cost.query_cost(conf.cost_strategy, operations, fragments, {
+        decorations  = service_decorations,
+        schema       = schema_index,
+        variables    = conf.resolve_variables and variables or nil,
+        use_defaults = conf.resolve_variables,
+    })

Review Comment:
   This contradicts the stated fallback that a Service without decorations, or a Route without a Service, is charged its node count. With `node_quantifier`, a missing decoration makes every node produce zero and the final floor charges the whole document as 1 (as TEST 11 currently demonstrates). Fall back to the unweighted complexity walk when no decoration index exists.



##########
apisix/control/v1.lua:
##########
@@ -19,7 +19,8 @@ local core = require("apisix.core")
 local plugin = require("apisix.plugin")
 local get_routes = require("apisix.router").http_routes
 local get_stream_routes = require("apisix.router").stream_routes
-local get_services = require("apisix.http.service").services
+local get_service_mod = require("apisix.http.service")
+local get_services = get_service_mod.services

Review Comment:
   The services watcher now includes decoration values, but not every control-API reader filters them. `iter_and_find_healthcheck_info` still compares each raw `value.value.id` and returns immediately; a decoration whose id equals the requested Service id can therefore produce a false “no checker” before the actual Service is reached. Filter decoration keys in that lookup as well.



##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }
+}
+
+query {
+    __schema {
+        queryType { name }
+        mutationType { name }
+        types {
+            ...TypeAttr
+            fields {
+                name
+                args { name defaultValue type { ...WrappedTypeRef } }
+                type { ...WrappedTypeRef }
+            }
+        }
+    }
+}
+]]
+
+-- Per-worker, keyed by the owning service (or by the explicit endpoint when one
+-- is configured). Never invalidated: an upstream schema change only takes effect
+-- after a reload. Keying on the service rather than on the derived endpoint is
+-- what bounds the table: the endpoint embeds the
+-- request path, so a route matching many paths would otherwise add a permanent
+-- entry -- and trigger an upstream introspection -- for every distinct path.
+local schema_cache = {}
+-- cache_key -> {err = <string>, expire_at = <number>}
+local failure_cache = {}
+
+local _M = {}
+
+
+local function unwrap_type_name(type_ref)
+    -- NON_NULL / LIST wrappers have no name of their own; descend to the named type.
+    while type_ref do
+        if type_ref.name then
+            return type_ref.name
+        end
+        type_ref = type_ref.ofType
+    end
+
+    return nil
+end
+
+
+local function build_index(schema_data)
+    local introspected = schema_data and schema_data.__schema
+    if not introspected or type(introspected.types) ~= "table" then
+        return nil, "introspection response has no __schema.types"
+    end
+
+    local types = {}
+    for _, type_def in ipairs(introspected.types) do
+        if type_def.name and type_def.fields then
+            local fields = {}
+            for _, field in ipairs(type_def.fields) do
+                if field.name then
+                    local args
+                    if field.args then
+                        for _, arg in ipairs(field.args) do
+                            if arg.name and arg.defaultValue ~= nil then
+                                args = args or {}
+                                args[arg.name] = {default_value = arg.defaultValue}
+                            end
+                        end
+                    end
+
+                    fields[field.name] = {
+                        type = unwrap_type_name(field.type),
+                        args = args,
+                    }
+                end
+            end
+            types[type_def.name] = {fields = fields}
+        end
+    end
+
+    return {
+        query_type    = introspected.queryType and introspected.queryType.name or "Query",
+        mutation_type = introspected.mutationType and introspected.mutationType.name
+                        or "Mutation",
+        types         = types,
+    }
+end
+
+
+-- Picks the node the introspection request is sent to. APISIX runs the access
+-- phase before the balancer, so there is no resolved peer to reuse:
+-- the nodes are sorted and the first one is taken so repeated introspections of
+-- the same upstream are stable. Deployments where that is not good enough (service
+-- discovery, a separate introspection path) set `introspection_endpoint`.
+local function first_node(nodes, scheme)
+    local candidates = {}
+    local default_port = (scheme == "https" or scheme == "grpcs") and 443 or 80
+
+    if core.table.isarray(nodes) then
+        for _, node in ipairs(nodes) do
+            if node.host then
+                core.table.insert(candidates, node.host .. ":" .. (node.port or default_port))
+            end
+        end
+    else
+        for addr in pairs(nodes) do
+            core.table.insert(candidates, addr)
+        end
+    end
+
+    if #candidates == 0 then
+        return nil
+    end
+
+    tab_sort(candidates)
+    return candidates[1]
+end
+
+
+-- Returns the endpoint to introspect and the Host header to send with it, or
+-- nil plus an error message.
+local function resolve_endpoint(conf, ctx)
+    if conf.introspection_endpoint then
+        return core.utils.escape_uri_control_chars(conf.introspection_endpoint)
+    end
+
+    local route = ctx.matched_route and ctx.matched_route.value
+    if not route then
+        return nil, "no matched route to derive the introspection endpoint from"
+    end
+
+    -- The schema is cached per service, so prefer the service's own upstream:
+    -- merge_service_route lets a route override it, and two routes on one service
+    -- with different overrides would otherwise share whichever schema was fetched
+    -- first. Falls back to the route for a route-only upstream.
+    local up_conf
+    local service = ctx.service_id and service_fetch(ctx.service_id)
+    if service and service.value then
+        up_conf = service.value.upstream
+        if not up_conf and service.value.upstream_id then
+            up_conf = upstream.get_by_id(service.value.upstream_id)
+        end
+    end
+
+    if not up_conf then
+        up_conf = route.upstream
+        if not up_conf and route.upstream_id then
+            up_conf = upstream.get_by_id(route.upstream_id)
+        end
+    end
+
+    if not up_conf or not up_conf.nodes then
+        return nil, "the route has no upstream nodes, set introspection_endpoint"
+    end
+
+    local scheme = up_conf.scheme or "http"
+    local addr = first_node(up_conf.nodes, scheme)
+    if not addr then
+        return nil, "the route has no upstream nodes, set introspection_endpoint"
+    end
+
+    local path = ctx.var.upstream_uri
+    if not path or path == "" then
+        path = ctx.var.uri
+    end
+    local query_pos = str_find(path, "?", 1, true)
+    if query_pos then
+        path = path:sub(1, query_pos - 1)
+    end
+    -- $uri is already percent-decoded, so a request path carrying %0d%0a would
+    -- otherwise reach the request line of the introspection call verbatim. Same
+    -- treatment $upstream_uri gets in init.lua.
+    path = core.utils.escape_uri_control_chars(path)
+
+    -- Mirror what proxied traffic sends, so a virtual-hosted upstream answers the
+    -- introspection the same way it answers the query. `node` means "use the node
+    -- address", which is what request_uri does on its own. For `pass` the template
+    -- leaves $upstream_host at $http_host (cli/ngx_tpl.lua:810) -- the verbatim
+    -- header including any port -- so $host, which is lowercased and port-stripped,
+    -- would reach a different vhost than the proxied request does.
+    local host
+    if up_conf.pass_host == "rewrite" then
+        host = up_conf.upstream_host
+    elseif up_conf.pass_host ~= "node" then
+        host = ctx.var.http_host or ctx.var.host
+    end
+
+    if host and not core.utils.validate_header_value(host) then
+        return nil, "the upstream host is not a valid header value"
+    end
+
+    return scheme .. "://" .. addr .. path, nil, host
+end
+
+
+-- Nothing here comes from the request. The schema is cached per service, so an
+-- introspection that varied with the caller would mean the request which happens
+-- to warm a worker picks the schema every later request on it is costed against,
+-- and one caller's rejected credentials would cache a failure that answers 400 to
+-- everyone else for as long as it lives. Introspection is the operator's call to
+-- the upstream, made with the operator's credentials.
+local function build_headers(conf, host)
+    local headers = {["Content-Type"] = "application/json"}
+
+    -- request_uri would otherwise send the node address as the Host, which breaks
+    -- a virtual-hosted GraphQL upstream that proxied traffic reaches fine
+    if host then
+        headers["Host"] = host
+    end
+
+    -- applied last, so an operator who needs a different Host or Content-Type on
+    -- the introspection request can say so
+    for name, value in pairs(conf.introspection_headers or {}) do
+        headers[name] = value
+    end
+
+    return headers
+end
+
+
+local function fetch_schema(conf, endpoint, host)
+    local httpc, err = http.new()
+    if not httpc then
+        return nil, "failed to create http client: " .. err
+    end
+
+    -- The single-flight lock is held for the whole fetch, so without an explicit
+    -- timeout an unresponsive endpoint would park every other request on the lock
+    -- until the OpenResty default (60s) expired. Bounded, not configurable.
+    httpc:set_timeouts(INTROSPECTION_CONNECT_TIMEOUT, INTROSPECTION_SEND_TIMEOUT,
+                       INTROSPECTION_READ_TIMEOUT)
+
+    local res
+    res, err = httpc:request_uri(endpoint, {
+        method  = "POST",
+        body    = core.json.encode({query = INTROSPECTION_QUERY}),
+        headers = build_headers(conf, host),
+        -- The introspection target is the upstream this route already proxies to,
+        -- whose certificate the proxy path does not verify either.
+        ssl_verify = false,
+        keepalive  = false,
+    })
+
+    if not res then
+        return nil, "failed to request " .. endpoint .. ": " .. err
+    end
+
+    if res.status ~= 200 then
+        return nil, "unexpected status " .. res.status .. " from " .. endpoint
+    end
+
+    local body
+    body, err = core.json.decode(res.body, {null_as_nil = true})
+    if not body then
+        return nil, "failed to decode the introspection response: " .. (err or "not an object")
+    end
+
+    if not body.data then
+        return nil, "introspection response has no data field"
+    end
+
+    return build_index(body.data)
+end
+
+
+local function fetch_and_cache(conf, cache_key, endpoint, host)
+    local lock, err = resty_lock:new(LOCK_SHDICT_NAME)
+    if not lock then
+        core.log.warn("failed to create the introspection lock: ", err,
+                      ", fetching the schema without single-flight protection")
+        return fetch_schema(conf, endpoint, host)
+    end
+
+    local elapsed
+    elapsed, err = lock:lock("graphql-schema#" .. cache_key)
+    if not elapsed then
+        core.log.warn("failed to acquire the introspection lock: ", err,
+                      ", fetching the schema without single-flight protection")
+        return fetch_schema(conf, endpoint, host)
+    end
+
+    -- another request may have populated the cache while this one waited
+    local cached = schema_cache[cache_key]
+    if cached then
+        lock:unlock()
+        return cached
+    end
+
+    local schema, ferr = fetch_schema(conf, endpoint, host)
+    if schema then
+        schema_cache[cache_key] = schema
+        failure_cache[cache_key] = nil
+    else
+        failure_cache[cache_key] = {
+            err = ferr,
+            expire_at = ngx_now() + INTROSPECTION_FAILURE_TTL,
+        }
+    end
+    lock:unlock()
+
+    return schema, ferr
+end
+
+
+---
+-- Returns the schema index for the upstream this request is routed to.
+function _M.get(conf, ctx)
+    -- The derived endpoint embeds the request path, so it is not a safe cache key
+    -- (a route matching many paths would grow the table without bound). The schema
+    -- belongs to the upstream, and decorations only exist per service, so the
+    -- service is both the correct and the bounded key. An explicit endpoint is its
+    -- own key: it does not vary per request.
+    local cache_key = conf.introspection_endpoint
+                      or (ctx.service_id and tostring(ctx.service_id))

Review Comment:
   Using the explicit endpoint itself as the cache key allows two Services that share a URL to share a schema or failure entry, even when they configure different introspection credentials and receive different schemas. This violates the per-Service cache isolation described by the feature and lets whichever Service warms the worker determine the other's cost model.



##########
apisix/plugins/graphql-limit-count/cost.lua:
##########
@@ -0,0 +1,527 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- GraphQL query cost engine.
+--
+-- Implements the `complexity` and `node_quantifier` cost strategies on the AST
+-- produced by the `graphql` rock.
+--
+-- Cost decorations are keyed by a position in the schema graph -- a `<GraphQL
+-- type>`, or a `<GraphQL type>` followed by the chain of fields that reaches it
+-- -- so matching a query field against a decoration requires the upstream schema:
+-- the walker carries a type cursor that descends alongside the selection tree.
+-- Without a schema no decoration can match and every node falls back to its
+-- default weights.
+--
+local core   = require("apisix.core")
+
+local ipairs     = ipairs
+local type       = type
+local tonumber   = tonumber
+local math_max   = math.max
+local str_gmatch = string.gmatch
+local tab_sort   = table.sort
+
+local _M = {}
+
+-- the fields a decoration contributes; everything else on the value is metadata
+local WEIGHT_KEYS = {"add_value", "mul_value", "add_arguments", "mul_arguments"}
+
+local EMPTY_INDEX = {flat = {}, root = {children = {}}, deep = false}
+
+-- How many selections the walk may expand. A real query is in the hundreds; a
+-- document that spreads fragments into an exponential DAG reaches this in
+-- milliseconds and is rejected instead of costed.
+local EXPANSION_BUDGET = 100000
+
+
+local function field_name(node)
+    return node.name and node.name.value
+end
+
+
+-- A field_path is a chain of GraphQL name tokens naming a position in the schema
+-- graph. `Person` weights every field that returns a `Person`; `Person.name`
+-- weights the field `name` wherever it is selected on a `Person`;
+-- `Query.products.nodes.reviews` pins one specific chain of fields. All three live
+-- in the same trie, one node per token, so matching is a walk rather than a string
+-- compare.
+local function insert_path(root, deco)
+    local node = root
+    local depth = 0
+
+    for token in str_gmatch(deco.field_path, "[^.]+") do
+        depth = depth + 1
+        local children = node.children
+        if not children then
+            children = {}
+            node.children = children
+        end
+
+        local child = children[token]
+        if not child then
+            child = {}
+            children[token] = child
+        end
+        node = child
+    end
+
+    -- an empty path names nothing; decorating the trie root would weight every
+    -- field in the document
+    if depth == 0 then
+        return 0
+    end
+
+    node.decoration = deco
+    node.depth = depth
+    return depth
+end
+
+
+---
+-- Compiles a service's decorations into the index `query_cost` consumes. Built
+-- once per configuration version, not per request.
+-- @tparam table list decoration values, each carrying a `field_path`
+function _M.build_index(list)
+    local index = {flat = {}, root = {children = {}}, deep = false}
+
+    for _, deco in ipairs(list or {}) do
+        if type(deco.field_path) == "string" then
+            local depth = insert_path(index.root, deco)
+            if depth == 2 then
+                index.flat[deco.field_path] = deco
+            elseif depth ~= 0 then
+                -- a single token is a type level rule and a longer path is pinned
+                -- to a chain; neither can be answered by the `<type>.<field>`
+                -- lookup, so the whole index falls back to the trie walk
+                index.deep = true
+            end
+        end
+    end
+
+    return index
+end
+
+
+-- Advances the candidate paths for one field: the ones inherited from the parent
+-- move on by this field's name, and a fresh candidate is seeded from the root by
+-- this field's own type. Seeding by type is what lets a two segment path match at
+-- any depth, while carrying the parent's candidates forward keeps a longer path
+-- pinned to its chain.
+local function advance(root, queue, type_name, name)
+    local next_queue
+
+    if queue then
+        for _, candidate in ipairs(queue) do
+            local children = candidate.children
+            local child = children and children[name]
+            if child then
+                next_queue = next_queue or {}
+                next_queue[#next_queue + 1] = child
+            end
+        end
+    end
+
+    local seed = type_name and root.children[type_name]
+    if seed then
+        next_queue = next_queue or {}
+        next_queue[#next_queue + 1] = seed
+    end
+
+    return next_queue
+end
+
+
+-- Two paths can name the same field: `Product.reviews` and
+-- `Query.products.nodes.reviews` both match one node. They are merged key by key,
+-- least specific first, so the longer path wins wherever they disagree. Resolving
+-- by path length rather than by storage order keeps the cost a function of the
+-- configuration alone.
+local function matched_decoration(queue)
+    if not queue then
+        return nil
+    end
+
+    local first, overlapping
+    for _, candidate in ipairs(queue) do
+        if candidate.decoration then
+            if not first then
+                first = candidate
+            else
+                overlapping = overlapping or {first}
+                overlapping[#overlapping + 1] = candidate
+            end
+        end
+    end
+
+    if not first then
+        return nil
+    end
+
+    if not overlapping then
+        return first.decoration
+    end
+
+    tab_sort(overlapping, function (a, b)
+        return a.depth < b.depth
+    end)
+
+    local merged = {}
+    for _, candidate in ipairs(overlapping) do
+        for _, key in ipairs(WEIGHT_KEYS) do
+            local value = candidate.decoration[key]
+            if value ~= nil then
+                merged[key] = value
+            end
+        end
+    end
+
+    return merged
+end
+
+
+-- Resolves a GraphQL argument literal to a Lua value.
+-- Only scalar literals carry a `.value`; list / inputObject nodes do not and are
+-- therefore reported as absent. A variable resolves only under
+-- `resolve_variables`, from the request's `variables` map first and then from the
+-- default the operation declares for it.
+local function literal_value(state, value_node)
+    if type(value_node) ~= "table" then
+        return nil
+    end
+
+    if value_node.kind == "variable" then
+        local name = value_node.name and value_node.name.value
+        if not name then
+            return nil
+        end
+
+        if state.variables then
+            local value = state.variables[name]
+            if value ~= nil then
+                return value
+            end
+        end
+
+        -- The client did not supply the variable, so the upstream executes with
+        -- the default the operation itself declares. Reading only the supplied
+        -- map would let `query Q($n: Int = 10000)` sent with no variables at all
+        -- cost the same as an absent quantifier -- the bypass `resolve_variables`
+        -- exists to close, reached through the document instead of the map.
+        if state.use_defaults then
+            local var_default = state.var_defaults and state.var_defaults[name]
+            return var_default and var_default.value
+        end
+
+        return nil
+    end
+
+    return value_node.value
+end
+
+
+local function argument_literal(state, node, name)
+    local args = node.arguments
+    if not args then
+        return nil
+    end
+
+    for _, arg in ipairs(args) do
+        if arg.name and arg.name.value == name then
+            return literal_value(state, arg.value)
+        end
+    end
+
+    return nil
+end
+
+
+-- Returns the numeric value of `name` for this node, or nil when the argument is
+-- absent or not usable in arithmetic. `first: "ten"` is client-controlled, so it
+-- must not reach the arithmetic: treating it as absent keeps the request on the
+-- fast path instead of turning a client-controlled value into a 500.
+local function resolve_argument(state, node, name, field_def)
+    local value = argument_literal(state, node, name)
+
+    -- By default a query that omits a paginating argument is free, because the
+    -- schema default is not consulted; `resolve_variables` opts into reading it.
+    if value == nil and state.use_defaults and field_def and field_def.args then
+        local arg_def = field_def.args[name]
+        value = arg_def and arg_def.default_value
+    end
+
+    return tonumber(value)

Review Comment:
   Client-supplied quantifiers are allowed to be negative. A negative outer multiplier makes descendant costs negative, after which the final floor reduces an arbitrarily expensive nested query to cost 1. Quantifiers represent counts, so non-negative finite values must be enforced before arithmetic.



##########
apisix/plugins/graphql-limit-count/cost.lua:
##########
@@ -0,0 +1,527 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- GraphQL query cost engine.
+--
+-- Implements the `complexity` and `node_quantifier` cost strategies on the AST
+-- produced by the `graphql` rock.
+--
+-- Cost decorations are keyed by a position in the schema graph -- a `<GraphQL
+-- type>`, or a `<GraphQL type>` followed by the chain of fields that reaches it
+-- -- so matching a query field against a decoration requires the upstream schema:
+-- the walker carries a type cursor that descends alongside the selection tree.
+-- Without a schema no decoration can match and every node falls back to its
+-- default weights.
+--
+local core   = require("apisix.core")
+
+local ipairs     = ipairs
+local type       = type
+local tonumber   = tonumber
+local math_max   = math.max
+local str_gmatch = string.gmatch
+local tab_sort   = table.sort
+
+local _M = {}
+
+-- the fields a decoration contributes; everything else on the value is metadata
+local WEIGHT_KEYS = {"add_value", "mul_value", "add_arguments", "mul_arguments"}
+
+local EMPTY_INDEX = {flat = {}, root = {children = {}}, deep = false}
+
+-- How many selections the walk may expand. A real query is in the hundreds; a
+-- document that spreads fragments into an exponential DAG reaches this in
+-- milliseconds and is rejected instead of costed.
+local EXPANSION_BUDGET = 100000
+
+
+local function field_name(node)
+    return node.name and node.name.value
+end
+
+
+-- A field_path is a chain of GraphQL name tokens naming a position in the schema
+-- graph. `Person` weights every field that returns a `Person`; `Person.name`
+-- weights the field `name` wherever it is selected on a `Person`;
+-- `Query.products.nodes.reviews` pins one specific chain of fields. All three live
+-- in the same trie, one node per token, so matching is a walk rather than a string
+-- compare.
+local function insert_path(root, deco)
+    local node = root
+    local depth = 0
+
+    for token in str_gmatch(deco.field_path, "[^.]+") do
+        depth = depth + 1
+        local children = node.children
+        if not children then
+            children = {}
+            node.children = children
+        end
+
+        local child = children[token]
+        if not child then
+            child = {}
+            children[token] = child
+        end
+        node = child
+    end
+
+    -- an empty path names nothing; decorating the trie root would weight every
+    -- field in the document
+    if depth == 0 then
+        return 0
+    end
+
+    node.decoration = deco
+    node.depth = depth
+    return depth
+end
+
+
+---
+-- Compiles a service's decorations into the index `query_cost` consumes. Built
+-- once per configuration version, not per request.
+-- @tparam table list decoration values, each carrying a `field_path`
+function _M.build_index(list)
+    local index = {flat = {}, root = {children = {}}, deep = false}
+
+    for _, deco in ipairs(list or {}) do
+        if type(deco.field_path) == "string" then
+            local depth = insert_path(index.root, deco)
+            if depth == 2 then
+                index.flat[deco.field_path] = deco
+            elseif depth ~= 0 then
+                -- a single token is a type level rule and a longer path is pinned
+                -- to a chain; neither can be answered by the `<type>.<field>`
+                -- lookup, so the whole index falls back to the trie walk
+                index.deep = true
+            end
+        end
+    end
+
+    return index
+end
+
+
+-- Advances the candidate paths for one field: the ones inherited from the parent
+-- move on by this field's name, and a fresh candidate is seeded from the root by
+-- this field's own type. Seeding by type is what lets a two segment path match at
+-- any depth, while carrying the parent's candidates forward keeps a longer path
+-- pinned to its chain.
+local function advance(root, queue, type_name, name)
+    local next_queue
+
+    if queue then
+        for _, candidate in ipairs(queue) do
+            local children = candidate.children
+            local child = children and children[name]
+            if child then
+                next_queue = next_queue or {}
+                next_queue[#next_queue + 1] = child
+            end
+        end
+    end
+
+    local seed = type_name and root.children[type_name]
+    if seed then
+        next_queue = next_queue or {}
+        next_queue[#next_queue + 1] = seed
+    end
+
+    return next_queue
+end
+
+
+-- Two paths can name the same field: `Product.reviews` and
+-- `Query.products.nodes.reviews` both match one node. They are merged key by key,
+-- least specific first, so the longer path wins wherever they disagree. Resolving
+-- by path length rather than by storage order keeps the cost a function of the
+-- configuration alone.
+local function matched_decoration(queue)
+    if not queue then
+        return nil
+    end
+
+    local first, overlapping
+    for _, candidate in ipairs(queue) do
+        if candidate.decoration then
+            if not first then
+                first = candidate
+            else
+                overlapping = overlapping or {first}
+                overlapping[#overlapping + 1] = candidate
+            end
+        end
+    end
+
+    if not first then
+        return nil
+    end
+
+    if not overlapping then
+        return first.decoration
+    end
+
+    tab_sort(overlapping, function (a, b)
+        return a.depth < b.depth
+    end)
+
+    local merged = {}
+    for _, candidate in ipairs(overlapping) do
+        for _, key in ipairs(WEIGHT_KEYS) do
+            local value = candidate.decoration[key]
+            if value ~= nil then
+                merged[key] = value
+            end
+        end
+    end
+
+    return merged
+end
+
+
+-- Resolves a GraphQL argument literal to a Lua value.
+-- Only scalar literals carry a `.value`; list / inputObject nodes do not and are
+-- therefore reported as absent. A variable resolves only under
+-- `resolve_variables`, from the request's `variables` map first and then from the
+-- default the operation declares for it.
+local function literal_value(state, value_node)
+    if type(value_node) ~= "table" then
+        return nil
+    end
+
+    if value_node.kind == "variable" then
+        local name = value_node.name and value_node.name.value
+        if not name then
+            return nil
+        end
+
+        if state.variables then
+            local value = state.variables[name]
+            if value ~= nil then
+                return value
+            end
+        end
+
+        -- The client did not supply the variable, so the upstream executes with
+        -- the default the operation itself declares. Reading only the supplied
+        -- map would let `query Q($n: Int = 10000)` sent with no variables at all
+        -- cost the same as an absent quantifier -- the bypass `resolve_variables`
+        -- exists to close, reached through the document instead of the map.
+        if state.use_defaults then
+            local var_default = state.var_defaults and state.var_defaults[name]
+            return var_default and var_default.value
+        end
+
+        return nil
+    end
+
+    return value_node.value
+end
+
+
+local function argument_literal(state, node, name)
+    local args = node.arguments
+    if not args then
+        return nil
+    end
+
+    for _, arg in ipairs(args) do
+        if arg.name and arg.name.value == name then
+            return literal_value(state, arg.value)
+        end
+    end
+
+    return nil
+end
+
+
+-- Returns the numeric value of `name` for this node, or nil when the argument is
+-- absent or not usable in arithmetic. `first: "ten"` is client-controlled, so it
+-- must not reach the arithmetic: treating it as absent keeps the request on the
+-- fast path instead of turning a client-controlled value into a 500.
+local function resolve_argument(state, node, name, field_def)
+    local value = argument_literal(state, node, name)
+
+    -- By default a query that omits a paginating argument is free, because the
+    -- schema default is not consulted; `resolve_variables` opts into reading it.
+    if value == nil and state.use_defaults and field_def and field_def.args then
+        local arg_def = field_def.args[name]
+        value = arg_def and arg_def.default_value
+    end
+
+    return tonumber(value)
+end
+
+
+local function node_weights(state, node, deco, field_def)
+    if not deco then
+        return 1, 1, false
+    end
+
+    local node_add = deco.add_value or 1
+    local node_mul = deco.mul_value or 1
+    local has_quantifier = false
+
+    if deco.add_arguments then
+        for _, name in ipairs(deco.add_arguments) do
+            local value = resolve_argument(state, node, name, field_def)
+            if value then
+                node_add = node_add + value
+            end
+        end
+    end
+
+    if deco.mul_arguments then
+        for _, name in ipairs(deco.mul_arguments) do
+            local value = resolve_argument(state, node, name, field_def)
+            if value then
+                node_mul = node_mul * value
+                has_quantifier = true
+            end
+        end
+    end
+
+    return node_add, node_mul, has_quantifier
+end
+
+
+local each_field
+-- Calls fn(child, decoration, field_def, child_type, child_queue) for every field
+-- selected by `node`, where `sel_type` is the GraphQL type the selection set
+-- belongs to and `queue` the decoration paths still live at this point.
+--
+-- Fragments are transparent: they carry no cost of their own and they do not move
+-- the type cursor or the decoration paths. `... on Droid { name }` therefore
+-- matches `<parent type>.name` rather than `Droid.name`: the walk never sees the
+-- fragment node, so its `typeCondition` does not take effect.
+-- A fragment's `typeCondition` narrows the selection to a concrete type, so the
+-- fields inside it belong to that type and not to the abstract one the parent
+-- selected. Without moving the cursor, `... on Product { expensive }` under an
+-- interface looks `expensive` up on the interface, misses a `Product.expensive`
+-- weight and undercharges the query. Only move it when the schema actually knows
+-- the type, so an unknown condition degrades instead of losing the cursor.
+local function fragment_type(state, node, sel_type)
+    local condition = node.typeCondition
+    local name = condition and condition.name and condition.name.value
+    if name and state.types and state.types[name] then
+        return name
+    end
+
+    return sel_type
+end
+
+
+function each_field(state, node, sel_type, queue, fn)
+    local selection_set = node.selectionSet
+    if not selection_set or not selection_set.selections then
+        return
+    end
+
+    for _, sel in ipairs(selection_set.selections) do
+        local kind = sel.kind
+
+        -- Spreading a fragment twice legitimately costs twice, so an acyclic chain
+        -- of fragments that each spread the previous one twice expands
+        -- exponentially: a document under a kilobyte can otherwise burn seconds of
+        -- CPU in the access phase, before any limit has been applied. Bound the
+        -- expansion instead, and let the caller reject the request.
+        state.budget = state.budget - 1
+        if state.budget < 0 then
+            state.exhausted = true
+            return
+        end
+
+        if kind == "field" then
+            local name = field_name(sel)
+            local deco, field_def, child_type, child_queue
+            if name and sel_type then
+                local type_def = state.types and state.types[sel_type]
+                field_def = type_def and type_def.fields[name]
+                child_type = field_def and field_def.type
+
+                if state.deep then
+                    child_queue = advance(state.root, queue, child_type, name)
+                    deco = matched_decoration(child_queue)
+                else
+                    -- every path is `<type>.<field>`, so the walk has at most one
+                    -- live candidate and collapses to a single lookup
+                    deco = state.flat[sel_type .. "." .. name]
+                end
+            end
+            fn(sel, deco, field_def, child_type, child_queue)
+
+        elseif kind == "inlineFragment" then
+            each_field(state, sel, fragment_type(state, sel, sel_type), queue, fn)

Review Comment:
   When the index contains any type-level or deep rule, `state.deep` is enabled and shallow rules are matched through `queue`. Entering a concrete fragment changes `sel_type` but leaves `queue` seeded from the parent's abstract return type, so a rule such as `Product.expensive` stops matching if an unrelated deep rule also exists. Re-seed the queue from the known fragment condition while preserving the inherited deep-path candidates; the named-fragment branch has the same issue.



##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }
+}
+
+query {
+    __schema {
+        queryType { name }
+        mutationType { name }

Review Comment:
   The schema index omits `subscriptionType`, and `root_type` consequently treats every non-mutation operation as a query. A valid subscription is therefore walked under the Query type, so Subscription decorations never match and its cost is undercounted. Include the subscription root in introspection and select it for subscription operations.



##########
apisix/plugins/graphql-limit-count.lua:
##########
@@ -25,30 +28,115 @@ local pairs   = pairs
 local ipairs  = ipairs
 local pcall   = pcall
 local max     = math.max
+local ceil    = math.ceil
 local tonumber = tonumber
 
 local GRAPHQL_DEFAULT_MAX_SIZE = 1048576
+local QUERY_COST_HEADER = "X-Graphql-Query-Cost"
 
 local plugin_name = "graphql-limit-count"
+
+-- The plugin reuses the whole limit-count configuration surface and adds the cost
+-- model on top of it. limit_count.schema is shared with limit-count,
+-- limit-count-advanced and ai-rate-limiting, so it must not be mutated in place.
+local schema = core.table.deepcopy(limit_count.schema)
+
+schema.properties.cost_strategy = {
+    type = "string",
+    enum = {"depth", "complexity", "node_quantifier"},
+    -- "depth" is what this plugin has always done; keeping it as the default means
+    -- an existing configuration keeps its current cost after the upgrade.
+    default = "depth",
+}
+schema.properties.max_cost = {
+    type = "number",
+    minimum = 0,
+    default = 0,
+    description = "reject with 403 above this cost, 0 disables the check",
+}
+schema.properties.score_factor = {
+    type = "number",
+    exclusiveMinimum = 0,
+    default = 1,
+    description = "scaling applied to the raw cost before the quota is charged",
+}
+schema.properties.resolve_variables = {
+    type = "boolean",
+    -- On by default: with it off, moving `first: 10000` to `first: $n` makes the
+    -- same request cost a fraction of what the literal costs, which is a bypass
+    -- of max_cost and of the quota. Turn it off only to reproduce an engine that
+    -- ignores variables.
+    default = true,
+    description = "resolve GraphQL variables and schema argument defaults when " ..
+                  "computing the cost, instead of treating them as absent",
+}
+schema.properties.introspection_endpoint = {
+    type = "string",
+    pattern = "^https?://",
+    description = "explicit schema introspection endpoint, derived from the " ..
+                  "upstream when unset",
+}
+schema.properties.introspection_headers = {
+    type = "object",
+    patternProperties = {["^[^:]+$"] = {type = "string"}},
+    -- Deliberately not taken from the request. The schema is cached per service,
+    -- so an introspection whose result depends on the caller would let whichever
+    -- request warms a worker pick the schema every later request is costed
+    -- against, and would let one caller's bad credentials cache a failure that
+    -- rejects everyone else. Credentials for it belong to the operator.
+    description = "headers sent on the schema introspection request, for an " ..
+                  "upstream whose introspection needs credentials",
+}

Review Comment:
   `introspection_headers` is explicitly intended to hold upstream credentials, but it is not added to the inherited `encrypt_fields`, so tokens remain plaintext in etcd even when APISIX data encryption is enabled. Comparable credential-bearing header maps are encrypted in `apisix/plugins/loki-logger.lua:113` and `apisix/plugins/elasticsearch-logger.lua:113`; append this field to the schema's encryption list.



##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }

Review Comment:
   The introspection query follows only four `ofType` links. GraphQL permits deeper nested list/non-null wrappers, so a valid field such as a sufficiently nested list of `Product` is returned without the innermost type name; `unwrap_type_name` then yields nil and all decorations beneath that field are silently missed. Request enough wrapper depth (as standard introspection queries do) or otherwise reject an unresolved type instead of undercharging it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5349797067

   Thanks — I checked all four against the code before answering. Two are already handled, one is a real gap and is now fixed, one is real but the obvious fix does not close it.
   
   **1. Request-path introspection I/O — confirmed, deliberate, staying.**
   
   The fetch is lazy on purpose, and it is bounded rather than unbounded: it is skipped entirely when the service carries no decorations, so a deployment not using the cost model never makes the call; it is single-flighted per cache key with `resty.lock` under an explicit lock timeout, so an unresponsive endpoint parks one request rather than every request; the result is cached per worker per service, and a failure is cached for 10s so a dead endpoint costs at most one upstream request per worker per 10s.
   
   Prefetching at `init_worker` is not available to it: the plugin does not know at boot which services carry decorations — they arrive through the `/services` watcher — the derived endpoint depends on the route's upstream, and the upstream is frequently not reachable during boot. Moving it off the request path would mean a services-watcher-driven registry plus a refresher timer, fetching schemas for services that may never receive GraphQL traffic. That is a reasonable thing to want, but it is a separate change; I would rather not add a background fetcher to this PR. Tracking separately.
   
   **2. Plugin reload coverage — valid gap, fixed.**
   
   You are right that nothing asserted it. Rather than a `t/cli` lifecycle script, I added an in-tree case that primes both introspection caches through the shipped module's own upvalues (not a replica), calls `destroy()` the way `apisix/plugin.lua` does on reload, and asserts both are empty. I verified it is not vacuous: making `destroy()` a no-op reddens it.
   
   **3. Backend error classification — already guarded; the residual case is the operator's own choice.**
   
   `graphql-limit-count.lua` already distinguishes them:
   
   ```lua
   local code, msg = limit_count.rate_limit(conf, ctx, plugin_name, cost)
   if code == 500 and conf.rejected_code ~= 500 then
       return code, msg
   end
   ```
   
   A counter-backend failure returns a literal 500 from `limit-count`, a rejection returns `conf.rejected_code`. The guard is written this way rather than `code >= 500` precisely so the default `rejected_code` of 503 does not get returned where the `max_cost` rejection should answer 403.
   
   That leaves exactly one case: `rejected_code == 500`. There the two are indistinguishable by construction, because the operator chose the rejection code that collides with the internal one — the ambiguity exists for a plain rejection too, with or without `max_cost`. And when `max_cost` is exceeded, 403 is the accurate answer regardless of whether the counter also failed: the query really is over the limit, and that is a property of the query, not of the counter. The backend failure is not lost either — `limit-count/init.lua` logs it at error level before returning.
   
   An explicit outcome marker from `rate_limit` would be cleaner, but `rate_limit` lives in the shared `limit-count/init.lua` and is called by `limit-count` as well, so changing its contract is not a change this plugin can make on its own. Tracking separately.
   
   **4. Service recreation race — confirmed real, and reordering does not close it.**
   
   The window is as you describe: `service_absent` → `base_put` → `reclaim_stale_decorations`, and a decoration `PUT` landing between the last two is deleted after returning 201.
   
   I checked the obvious fix — reclaiming before `base_put` — and it does not work. It only moves the window: `service_absent` says absent, another client creates the service *and* a decoration, then our reclaim deletes it. The decoration handler 404s when the parent service is missing, so the window is empty at that instant, but the check-then-act gap is still there.
   
   The correct fix is the one you point at: delete only the keys that predate the new service, i.e. those whose `mod_revision` is below the create revision `base_put` gets back from etcd. `core.etcd.rmdir` is an unconditional range delete, so this needs a list followed by a per-key compare-and-delete, which is a different shape from the current one-call reclaim.
   
   Worth noting how narrow it is in practice: the reclaim only runs on a **create** of a service id that was absent a moment earlier, so the racing client has to be writing a decoration to a service id that is being created concurrently by someone else. The failure mode is a lost decoration, not a wrong cost — the cost model degrades to the node count.
   
   Happy to implement the revision-conditioned reclaim in this PR if you would rather not carry it; my inclination is to track it separately so this PR does not also change the etcd write shape. Let me know which you prefer.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13840:
URL: https://github.com/apache/apisix/pull/13840#discussion_r3819307379


##########
apisix/control/v1.lua:
##########
@@ -19,7 +19,8 @@ local core = require("apisix.core")
 local plugin = require("apisix.plugin")
 local get_routes = require("apisix.router").http_routes
 local get_stream_routes = require("apisix.router").stream_routes
-local get_services = require("apisix.http.service").services
+local get_service_mod = require("apisix.http.service")
+local get_services = get_service_mod.services

Review Comment:
   Not reachable, so I have left the lookup alone. A decoration's runtime `value.id` is not its own id: `config_etcd` overwrites it with the key relative to the prefix, so it reads `aaa-owner/graphql_cost_decorations/hc-svc` -- verified by instrumenting the loop. A Service id cannot contain a slash (`id_schema` is `^[a-zA-Z0-9-_.]+$`), so no decoration can ever match one here.
   
   I did add the filter first, then reverted it: a guard against something that cannot happen, carrying a comment saying it can, is worse than none. There is a regression pinning the invariant instead, since it rests on how `config_etcd` derives the id rather than on anything local to this function.



##########
apisix/plugins/graphql-limit-count/introspection.lua:
##########
@@ -0,0 +1,409 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+--
+-- Upstream GraphQL schema introspection.
+--
+-- Cost decorations are addressed by GraphQL type name, while a query AST only
+-- carries field names, so the schema is required to tell `Person.name` from
+-- `Vehicle.name`. The schema is fetched lazily on the first request that needs it
+-- and then kept for the lifetime of the worker; there is no knob for the TTL, so a
+-- schema change on a live upstream needs a reload.
+--
+local core        = require("apisix.core")
+local http        = require("resty.http")
+local resty_lock  = require("resty.lock")
+local upstream    = require("apisix.upstream")
+local service_fetch = require("apisix.http.service").get
+
+local ipairs   = ipairs
+local pairs    = pairs
+local type     = type
+local str_find = string.find
+local tab_sort = table.sort
+local tostring = tostring
+local ngx_now  = ngx.now
+
+local LOCK_SHDICT_NAME = "lrucache-lock"
+
+-- milliseconds; see the comment in fetch_schema
+local INTROSPECTION_CONNECT_TIMEOUT = 2000
+local INTROSPECTION_SEND_TIMEOUT    = 2000
+local INTROSPECTION_READ_TIMEOUT    = 5000
+-- seconds a failed introspection is remembered, so an upstream that answers
+-- nothing does not get one request per client request
+local INTROSPECTION_FAILURE_TTL     = 10
+
+-- Trimmed to what the cost engine consumes: the root type names (`field_path`'s
+-- first segment is matched against them), each type's fields and their result
+-- types, and the argument default values used by `resolve_variables`.
+local INTROSPECTION_QUERY = [[
+fragment TypeAttr on __Type {
+    kind
+    name
+}
+
+fragment WrappedTypeRef on __Type {
+    ...TypeAttr
+    ofType { ...TypeAttr
+      ofType { ...TypeAttr
+        ofType { ...TypeAttr
+          ofType { ...TypeAttr } } } }
+}
+
+query {
+    __schema {
+        queryType { name }
+        mutationType { name }

Review Comment:
   Not reachable with the current parser. The `graphql` 0.0.2 grammar has no `subscription` production, so such a document fails `parse` and the plugin answers 400 before the cost engine is entered -- the parse gaps are listed in the description. Adding `subscriptionType` to the index and selecting it in `root_type` would be dead code until the parser gains it, so I would rather add both together.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(graphql-limit-count): rate limit by GraphQL query cost [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #13840:
URL: https://github.com/apache/apisix/pull/13840#issuecomment-5326458839

   Thanks — all three reproduce, and all three are fixed in `16d8075`. I reproduced each against the engine before changing anything, so the numbers below are measured rather than argued.
   
   ### [P1] Fragment DAGs — confirmed, now bounded
   
   Reproduced with a chain where each fragment spreads the previous one twice:
   
   | n | document | time | cost |
   |---|---|---|---|
   | 12 | 448 B | 0.004 s | 4098 |
   | 16 | 592 B | 0.021 s | 65538 |
   | 20 | 736 B | 0.254 s | 1048578 |
   
   Doubling per level, from a document well under a kilobyte, all spent in the access phase before any limit applies.
   
   I went with the work limit rather than memoisation, deliberately: the cost of a subtree depends on the type cursor **and** on the live `field_path` candidates, so a memo key would have to include both, and for `node_quantifier` the inherited multiplier as well. That is a lot of subtlety to get right for a bound that a counter gives unconditionally. `each_field` now decrements an expansion budget per selection and aborts once it is spent; `query_cost` returns nil plus a client message and the access phase answers 400.
   
   After: n=16/18/20 are all rejected in ~0.01 s, flat instead of doubling. Regression is `TEST 41`, which asserts both halves — a 10-level DAG still costs normally (1026), a 24-level one is rejected and the whole thing runs in under a second.
   
   ### [P1] Fragment type conditions — confirmed, cursor now moves
   
   With `Query.node` returning an interface and the weight on the concrete type:
   
   ```
   Product.expensive add_value=100
   query { node { ... on Product { expensive } } }   ->  3   (weight missed)
   ```
   
   Both fragment forms now move the cursor from `typeCondition`, and the same query costs 102. A condition naming a type the schema does not have leaves the cursor where it was, so an unknown or ill-formed condition degrades instead of losing the cursor entirely. `TEST 42` covers the inline form, the named form, and the unknown-type case.
   
   ### [P1] Variable handling — confirmed, safe default
   
   ```
   first: 10000   -> 20002
   first: $n      ->     4     (n = 10000)
   ```
   
   `resolve_variables` now defaults to `true`. Turning it off is still available for reproducing an engine that ignores variables, but that is no longer what an operator gets by not thinking about it. `TEST 43` is the literal-versus-variable equivalence check for both strategies; `TEST 9` and `TEST 12`–`14` previously asserted the old default and are re-aimed rather than deleted, so both sides of the switch stay covered.
   
   ### Note on the default change
   
   This is a behaviour change for anyone who had already sized a quota against the old default: the same traffic now costs more, because the fan-out that was previously invisible is charged. It is called out in the docs and I think it is the right default — the alternative is shipping a bypass that any client can use — but it is worth a reviewer's eye rather than being buried in a diff.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org