unitysvc-core 0.3.7__tar.gz → 0.3.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {unitysvc_core-0.3.7/src/unitysvc_core.egg-info → unitysvc_core-0.3.9}/PKG-INFO +9 -7
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/README.md +8 -6
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/pyproject.toml +2 -2
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/channel.py +17 -5
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/offering_v1.json +2 -2
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9/src/unitysvc_core.egg-info}/PKG-INFO +9 -7
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core.egg-info/SOURCES.txt +0 -2
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_request_rules.py +46 -62
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_validate_service.py +10 -7
- unitysvc_core-0.3.7/src/unitysvc_core/rules.py +0 -301
- unitysvc_core-0.3.7/src/unitysvc_core/rules_conformance.json +0 -91
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/LICENSE +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/setup.cfg +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/__init__.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/grants.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/__init__.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/base.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/documents.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/listing_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/listing_v1.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/offering_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/offering_v1.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/pricing.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/promotion_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/provider_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/provider_v1.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/service.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/service_group_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/models/validators.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/parameter_usage.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/py.typed +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/base.json +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/listing_v1.json +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/promotion_v1.json +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/provider_v1.json +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/schema/service_group_v1.json +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/utils.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core/validator.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core.egg-info/dependency_links.txt +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core.egg-info/requires.txt +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/src/unitysvc_core.egg-info/top_level.txt +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_access_interface_routing.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_canonical_interfaces.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_channel.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_effective_price.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_external_apps.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_grants.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_hidden_path_skip.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_listing_audience.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_offering_data.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_parameter_usage.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_pricing.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_provider_account_rate_limits.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_service_group.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_service_status.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_utils.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_validator.py +0 -0
- {unitysvc_core-0.3.7 → unitysvc_core-0.3.9}/tests/test_validators.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: unitysvc-core
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.9
|
|
4
4
|
Summary: Shared data models and validation helpers for UnitySVC packages
|
|
5
5
|
Author-email: Bo Peng <bo.peng@unitysvc.com>
|
|
6
6
|
Maintainer-email: Bo Peng <bo.peng@unitysvc.com>
|
|
@@ -275,19 +275,21 @@ Unknown keys are refused. Retired keys are refused with what to do instead (`RET
|
|
|
275
275
|
|
|
276
276
|
### Request rules
|
|
277
277
|
|
|
278
|
-
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
278
|
+
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947, unitysvc/apisix-gateways#94). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
279
279
|
|
|
280
280
|
```json
|
|
281
281
|
"request_rules": [
|
|
282
|
-
{"name": "github-ping", "when": "header[
|
|
283
|
-
{"name": "own-echo", "when": "body.user_id ==
|
|
284
|
-
{"
|
|
282
|
+
{"name": "github-ping", "when": "header[\"x-github-event\"] == \"ping\"", "respond": {"status": 200}},
|
|
283
|
+
{"name": "own-echo", "when": "body.user_id == \"${ customer_secrets.DISCORD_WEBHOOK_ID ?? }\"", "respond": {"status": 204}},
|
|
284
|
+
{"name": "filter", "when": "body.count >= {{ params.MIN_COUNT }}", "respond": {"status": 204}}
|
|
285
285
|
]
|
|
286
286
|
```
|
|
287
287
|
|
|
288
|
-
|
|
288
|
+
`when` is a Lua expression that the gateway compiles and evaluates against the request. Its environment (the request's `body`, `header`, `query` and `method`, the approved functions), its template references (`${ customer_secrets.NAME ?? }`, `${ secrets.NAME }`, `{{ params.NAME }}`, `{{ enrollment.code }}`, bound as values and never spliced into the source; a secret may only be compared whole with `==` / `~=`) and its limits are the gateway's, documented in apisix-gateways' README. Core does not parse it: it checks only that it is non-empty and at most 1024 bytes. Lua syntax errors and invalid references are reported by the gateway (the channel answers 503) and by a gateway-run test (`services run-tests`), not by `specs validate`.
|
|
289
289
|
|
|
290
|
-
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
290
|
+
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
291
|
+
|
|
292
|
+
Versions: 0.3.7 accepted a two-operator grammar; 0.3.8 refused template references and added `customer_secrets`; 0.3.9 follows the gateway as merged (template references in `when`, no `customer_secrets`). No published service used 0.3.7 or 0.3.8 rules.
|
|
291
293
|
|
|
292
294
|
### External apps and product links
|
|
293
295
|
|
|
@@ -240,19 +240,21 @@ Unknown keys are refused. Retired keys are refused with what to do instead (`RET
|
|
|
240
240
|
|
|
241
241
|
### Request rules
|
|
242
242
|
|
|
243
|
-
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
243
|
+
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947, unitysvc/apisix-gateways#94). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
244
244
|
|
|
245
245
|
```json
|
|
246
246
|
"request_rules": [
|
|
247
|
-
{"name": "github-ping", "when": "header[
|
|
248
|
-
{"name": "own-echo", "when": "body.user_id ==
|
|
249
|
-
{"
|
|
247
|
+
{"name": "github-ping", "when": "header[\"x-github-event\"] == \"ping\"", "respond": {"status": 200}},
|
|
248
|
+
{"name": "own-echo", "when": "body.user_id == \"${ customer_secrets.DISCORD_WEBHOOK_ID ?? }\"", "respond": {"status": 204}},
|
|
249
|
+
{"name": "filter", "when": "body.count >= {{ params.MIN_COUNT }}", "respond": {"status": 204}}
|
|
250
250
|
]
|
|
251
251
|
```
|
|
252
252
|
|
|
253
|
-
|
|
253
|
+
`when` is a Lua expression that the gateway compiles and evaluates against the request. Its environment (the request's `body`, `header`, `query` and `method`, the approved functions), its template references (`${ customer_secrets.NAME ?? }`, `${ secrets.NAME }`, `{{ params.NAME }}`, `{{ enrollment.code }}`, bound as values and never spliced into the source; a secret may only be compared whole with `==` / `~=`) and its limits are the gateway's, documented in apisix-gateways' README. Core does not parse it: it checks only that it is non-empty and at most 1024 bytes. Lua syntax errors and invalid references are reported by the gateway (the channel answers 503) and by a gateway-run test (`services run-tests`), not by `specs validate`.
|
|
254
254
|
|
|
255
|
-
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
255
|
+
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
256
|
+
|
|
257
|
+
Versions: 0.3.7 accepted a two-operator grammar; 0.3.8 refused template references and added `customer_secrets`; 0.3.9 follows the gateway as merged (template references in `when`, no `customer_secrets`). No published service used 0.3.7 or 0.3.8 rules.
|
|
256
258
|
|
|
257
259
|
### External apps and product links
|
|
258
260
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "unitysvc-core"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.9"
|
|
8
8
|
description = "Shared data models and validation helpers for UnitySVC packages"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [{ name = "Bo Peng", email = "bo.peng@unitysvc.com" }]
|
|
@@ -80,7 +80,7 @@ package = true
|
|
|
80
80
|
include-package-data = true
|
|
81
81
|
|
|
82
82
|
[tool.setuptools.package-data]
|
|
83
|
-
unitysvc_core = ["schema/*.json", "py.typed"
|
|
83
|
+
unitysvc_core = ["schema/*.json", "py.typed"]
|
|
84
84
|
|
|
85
85
|
[tool.pytest.ini_options]
|
|
86
86
|
testpaths = ["tests"]
|
|
@@ -46,7 +46,6 @@ from pydantic import (
|
|
|
46
46
|
from pydantic.json_schema import JsonSchemaValue
|
|
47
47
|
from pydantic_core import CoreSchema
|
|
48
48
|
|
|
49
|
-
from ..rules import MAX_RULES, parse_condition
|
|
50
49
|
from .validators import validate_channel_name
|
|
51
50
|
|
|
52
51
|
# A string field that is a value or a template rendering to one.
|
|
@@ -106,6 +105,8 @@ _REFUSED_RULE_HEADERS = frozenset(
|
|
|
106
105
|
_HEADER_NAME_RE = re.compile(r"^[!#$%&'*+.^_`|~0-9A-Za-z-]+$")
|
|
107
106
|
_CONTENT_TYPE_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]*/[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]*([ \t]*;[ -~]*)?$")
|
|
108
107
|
_MAX_RULE_BODY_BYTES = 8192
|
|
108
|
+
MAX_RULES = 32
|
|
109
|
+
MAX_CONDITION_BYTES = 1024
|
|
109
110
|
_MAX_RULE_HEADERS = 16
|
|
110
111
|
|
|
111
112
|
|
|
@@ -155,7 +156,13 @@ class RuleResponse(BaseModel):
|
|
|
155
156
|
class RequestRule(BaseModel):
|
|
156
157
|
"""A condition the gateway checks before dispatch, and the response it answers with when it holds.
|
|
157
158
|
|
|
158
|
-
|
|
159
|
+
``when`` is a Lua expression the gateway compiles and evaluates against the
|
|
160
|
+
request (unitysvc/apisix-gateways#94, #96); its language, the values it can
|
|
161
|
+
read and its limits are the gateway's, and core does not parse it. Template
|
|
162
|
+
references (``${ customer_secrets.NAME ?? }``, ``${ secrets.NAME }``,
|
|
163
|
+
``{{ params.NAME }}``, ``{{ enrollment.code }}``) are bound by the gateway as
|
|
164
|
+
invocation arguments, never spliced into the source; a secret may only be
|
|
165
|
+
compared whole (``==`` / ``~=``).
|
|
159
166
|
"""
|
|
160
167
|
|
|
161
168
|
model_config = ConfigDict(extra="forbid")
|
|
@@ -167,15 +174,20 @@ class RequestRule(BaseModel):
|
|
|
167
174
|
)
|
|
168
175
|
when: str | None = Field(
|
|
169
176
|
default=None,
|
|
170
|
-
description=
|
|
177
|
+
description='A Lua expression evaluated by the gateway, e.g. header["x-github-event"] == "ping"; '
|
|
178
|
+
"absent means every request",
|
|
171
179
|
)
|
|
172
180
|
respond: RuleResponse
|
|
173
181
|
|
|
174
182
|
@field_validator("when")
|
|
175
183
|
@classmethod
|
|
176
184
|
def _check_condition(cls, value: str | None) -> str | None:
|
|
177
|
-
if value is
|
|
178
|
-
|
|
185
|
+
if value is None:
|
|
186
|
+
return value
|
|
187
|
+
if not value.strip():
|
|
188
|
+
raise ValueError("a condition must not be empty; omit `when` to answer every request")
|
|
189
|
+
if len(value.encode()) > MAX_CONDITION_BYTES:
|
|
190
|
+
raise ValueError(f"a condition is longer than {MAX_CONDITION_BYTES} bytes (UTF-8)")
|
|
179
191
|
return value
|
|
180
192
|
|
|
181
193
|
|
|
@@ -2361,7 +2361,7 @@
|
|
|
2361
2361
|
},
|
|
2362
2362
|
"RequestRule": {
|
|
2363
2363
|
"additionalProperties": false,
|
|
2364
|
-
"description": "A condition the gateway checks before dispatch, and the response it answers with when it holds.\n\
|
|
2364
|
+
"description": "A condition the gateway checks before dispatch, and the response it answers with when it holds.\n\n``when`` is a Lua expression the gateway compiles and evaluates against the\nrequest (unitysvc/apisix-gateways#94, #96); its language, the values it can\nread and its limits are the gateway's, and core does not parse it. Template\nreferences (``${ customer_secrets.NAME ?? }``, ``${ secrets.NAME }``,\n``{{ params.NAME }}``, ``{{ enrollment.code }}``) are bound by the gateway as\ninvocation arguments, never spliced into the source; a secret may only be\ncompared whole (``==`` / ``~=``).",
|
|
2365
2365
|
"properties": {
|
|
2366
2366
|
"name": {
|
|
2367
2367
|
"anyOf": [
|
|
@@ -2390,7 +2390,7 @@
|
|
|
2390
2390
|
}
|
|
2391
2391
|
],
|
|
2392
2392
|
"default": null,
|
|
2393
|
-
"description": "
|
|
2393
|
+
"description": "A Lua expression evaluated by the gateway, e.g. header[\"x-github-event\"] == \"ping\"; absent means every request",
|
|
2394
2394
|
"title": "When"
|
|
2395
2395
|
}
|
|
2396
2396
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: unitysvc-core
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.9
|
|
4
4
|
Summary: Shared data models and validation helpers for UnitySVC packages
|
|
5
5
|
Author-email: Bo Peng <bo.peng@unitysvc.com>
|
|
6
6
|
Maintainer-email: Bo Peng <bo.peng@unitysvc.com>
|
|
@@ -275,19 +275,21 @@ Unknown keys are refused. Retired keys are refused with what to do instead (`RET
|
|
|
275
275
|
|
|
276
276
|
### Request rules
|
|
277
277
|
|
|
278
|
-
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
278
|
+
An `http` channel may list up to 32 `request_rules` (unitysvc/unitysvc#2947, unitysvc/apisix-gateways#94). The gateway checks them in order after authentication, channel selection and rate limiting and before anything is dispatched. The first rule whose `when` holds answers with its static `respond`, and the request goes no further; when none holds, the request continues unchanged. A rule without `when` answers every request.
|
|
279
279
|
|
|
280
280
|
```json
|
|
281
281
|
"request_rules": [
|
|
282
|
-
{"name": "github-ping", "when": "header[
|
|
283
|
-
{"name": "own-echo", "when": "body.user_id ==
|
|
284
|
-
{"
|
|
282
|
+
{"name": "github-ping", "when": "header[\"x-github-event\"] == \"ping\"", "respond": {"status": 200}},
|
|
283
|
+
{"name": "own-echo", "when": "body.user_id == \"${ customer_secrets.DISCORD_WEBHOOK_ID ?? }\"", "respond": {"status": 204}},
|
|
284
|
+
{"name": "filter", "when": "body.count >= {{ params.MIN_COUNT }}", "respond": {"status": 204}}
|
|
285
285
|
]
|
|
286
286
|
```
|
|
287
287
|
|
|
288
|
-
|
|
288
|
+
`when` is a Lua expression that the gateway compiles and evaluates against the request. Its environment (the request's `body`, `header`, `query` and `method`, the approved functions), its template references (`${ customer_secrets.NAME ?? }`, `${ secrets.NAME }`, `{{ params.NAME }}`, `{{ enrollment.code }}`, bound as values and never spliced into the source; a secret may only be compared whole with `==` / `~=`) and its limits are the gateway's, documented in apisix-gateways' README. Core does not parse it: it checks only that it is non-empty and at most 1024 bytes. Lua syntax errors and invalid references are reported by the gateway (the channel answers 503) and by a gateway-run test (`services run-tests`), not by `specs validate`.
|
|
289
289
|
|
|
290
|
-
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
290
|
+
`respond` takes a `status` (200–599), an optional `body` (an object or array is sent as JSON; a string needs `content_type`), and optional `headers` (not the gateway-managed or hop-by-hop ones).
|
|
291
|
+
|
|
292
|
+
Versions: 0.3.7 accepted a two-operator grammar; 0.3.8 refused template references and added `customer_secrets`; 0.3.9 follows the gateway as merged (template references in `when`, no `customer_secrets`). No published service used 0.3.7 or 0.3.8 rules.
|
|
291
293
|
|
|
292
294
|
### External apps and product links
|
|
293
295
|
|
|
@@ -5,8 +5,6 @@ src/unitysvc_core/__init__.py
|
|
|
5
5
|
src/unitysvc_core/grants.py
|
|
6
6
|
src/unitysvc_core/parameter_usage.py
|
|
7
7
|
src/unitysvc_core/py.typed
|
|
8
|
-
src/unitysvc_core/rules.py
|
|
9
|
-
src/unitysvc_core/rules_conformance.json
|
|
10
8
|
src/unitysvc_core/utils.py
|
|
11
9
|
src/unitysvc_core/validator.py
|
|
12
10
|
src/unitysvc_core.egg-info/PKG-INFO
|
|
@@ -1,24 +1,14 @@
|
|
|
1
|
-
"""Request rules: the
|
|
1
|
+
"""Request rules: the channel field and what core checks of it (unitysvc/unitysvc#2947).
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Conditions are Lua expressions compiled by the gateway (apisix-gateways#94);
|
|
4
|
+
core does not parse them.
|
|
5
|
+
"""
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
7
8
|
from pydantic import ValidationError
|
|
8
9
|
|
|
9
10
|
from unitysvc_core.models import HttpChannel, RequestRule, UpstreamAccessConfig
|
|
10
|
-
from unitysvc_core.
|
|
11
|
-
MAX_CONDITION_LENGTH,
|
|
12
|
-
MAX_RULES,
|
|
13
|
-
Condition,
|
|
14
|
-
Ref,
|
|
15
|
-
RuleSyntaxError,
|
|
16
|
-
Slot,
|
|
17
|
-
evaluate,
|
|
18
|
-
parse_condition,
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
_CASES = json.loads(resources.files("unitysvc_core").joinpath("rules_conformance.json").read_text())["cases"]
|
|
11
|
+
from unitysvc_core.models.channel import MAX_CONDITION_BYTES, MAX_RULES
|
|
22
12
|
|
|
23
13
|
|
|
24
14
|
def _channel(config: dict):
|
|
@@ -31,55 +21,55 @@ def _rules_channel(*rules: dict) -> HttpChannel:
|
|
|
31
21
|
return channel
|
|
32
22
|
|
|
33
23
|
|
|
34
|
-
class
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
names = [case["name"] for case in _CASES]
|
|
39
|
-
assert len(names) == len(set(names))
|
|
40
|
-
|
|
41
|
-
@pytest.mark.parametrize("case", _CASES, ids=[case["name"] for case in _CASES])
|
|
42
|
-
def test_case(self, case: dict) -> None:
|
|
43
|
-
if "error" in case:
|
|
44
|
-
with pytest.raises(RuleSyntaxError) as excinfo:
|
|
45
|
-
parse_condition(case["when"])
|
|
46
|
-
assert case["error"] in str(excinfo.value)
|
|
47
|
-
else:
|
|
48
|
-
condition = parse_condition(case["when"])
|
|
49
|
-
assert evaluate(condition, case.get("request", {}), case.get("slots")) is case["match"]
|
|
50
|
-
|
|
24
|
+
class TestCondition:
|
|
25
|
+
@staticmethod
|
|
26
|
+
def _rule(**fields: object) -> RequestRule:
|
|
27
|
+
return RequestRule.model_validate({"respond": {"status": 204}, **fields})
|
|
51
28
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
|
|
29
|
+
@pytest.mark.parametrize(
|
|
30
|
+
"when",
|
|
31
|
+
[
|
|
32
|
+
'header["x-github-event"] == "ping"',
|
|
33
|
+
'body.type == "ping" or string.find(body.text or "", params.MARKER, 1, true) ~= nil',
|
|
34
|
+
"not body.author or body.author.bot == true",
|
|
35
|
+
"(body.n or 0) > 3 and method == 'POST'",
|
|
36
|
+
"this is not even Lua", # the gateway reports syntax, core does not
|
|
37
|
+
],
|
|
38
|
+
)
|
|
39
|
+
def test_any_lua_text_is_accepted_unparsed(self, when: str) -> None:
|
|
40
|
+
assert self._rule(when=when).when == when
|
|
58
41
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
param = parse_condition("'{{ params.marker }}' in body.name").literal
|
|
64
|
-
assert param == Slot("params", "marker") and not param.is_secret
|
|
42
|
+
@pytest.mark.parametrize("when", ["", " ", "\n\t"])
|
|
43
|
+
def test_an_empty_condition_is_refused(self, when: str) -> None:
|
|
44
|
+
with pytest.raises(ValidationError, match="must not be empty"):
|
|
45
|
+
self._rule(when=when)
|
|
65
46
|
|
|
66
|
-
def
|
|
67
|
-
|
|
68
|
-
|
|
47
|
+
def test_length_is_measured_in_utf8_bytes(self) -> None:
|
|
48
|
+
assert self._rule(when="x" * MAX_CONDITION_BYTES).when
|
|
49
|
+
with pytest.raises(ValidationError, match="longer than"):
|
|
50
|
+
self._rule(when="测" * (MAX_CONDITION_BYTES // 3 + 1))
|
|
69
51
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
52
|
+
@pytest.mark.parametrize(
|
|
53
|
+
"when",
|
|
54
|
+
[
|
|
55
|
+
"body.id == '${ customer_secrets.HOOK ?? }'",
|
|
56
|
+
'header["x-token"] ~= "${ secrets.TOKEN }"',
|
|
57
|
+
"body.count >= {{ params.MIN_COUNT }} and not {{ params.DISABLED }}",
|
|
58
|
+
'query["code"] == "{{ enrollment.code }}"',
|
|
59
|
+
],
|
|
60
|
+
)
|
|
61
|
+
def test_template_references_are_left_to_the_gateway(self, when: str) -> None:
|
|
62
|
+
assert self._rule(when=when).when == when
|
|
73
63
|
|
|
74
|
-
def
|
|
75
|
-
with pytest.raises(
|
|
76
|
-
|
|
64
|
+
def test_customer_secrets_is_not_a_rule_field(self) -> None:
|
|
65
|
+
with pytest.raises(ValidationError, match="customer_secrets"):
|
|
66
|
+
self._rule(when="true", customer_secrets=["HOOK_ID"])
|
|
77
67
|
|
|
78
68
|
|
|
79
69
|
class TestChannelField:
|
|
80
70
|
def test_rules_are_a_channel_field(self) -> None:
|
|
81
71
|
channel = _rules_channel(
|
|
82
|
-
{"name": "github-ping", "when":
|
|
72
|
+
{"name": "github-ping", "when": 'header["x-github-event"] == "ping"', "respond": {"status": 200}},
|
|
83
73
|
{"respond": {"status": 503, "body": {"error": "paused"}}},
|
|
84
74
|
)
|
|
85
75
|
assert channel.request_rules is not None
|
|
@@ -91,7 +81,7 @@ class TestChannelField:
|
|
|
91
81
|
def test_round_trip(self) -> None:
|
|
92
82
|
rule = {
|
|
93
83
|
"name": "echo",
|
|
94
|
-
"when":
|
|
84
|
+
"when": 'body.user_id == "${ customer_secrets.DISCORD_WEBHOOK_ID ?? }"',
|
|
95
85
|
"respond": {"status": 204},
|
|
96
86
|
}
|
|
97
87
|
dumped = _rules_channel(rule).model_dump(exclude_none=True)
|
|
@@ -99,13 +89,7 @@ class TestChannelField:
|
|
|
99
89
|
|
|
100
90
|
def test_bad_condition_names_its_rule(self) -> None:
|
|
101
91
|
with pytest.raises(ValidationError, match=r"request_rules\.0\.when"):
|
|
102
|
-
_rules_channel({"when": "
|
|
103
|
-
|
|
104
|
-
def test_secret_under_in_is_refused(self) -> None:
|
|
105
|
-
with pytest.raises(ValidationError, match="may only be compared with '=='"):
|
|
106
|
-
_rules_channel(
|
|
107
|
-
{"when": "body.user_id in '${ customer_secrets.DISCORD_WEBHOOK_URL ?? }'", "respond": {"status": 204}}
|
|
108
|
-
)
|
|
92
|
+
_rules_channel({"when": " ", "respond": {"status": 204}})
|
|
109
93
|
|
|
110
94
|
@pytest.mark.parametrize(
|
|
111
95
|
"config",
|
|
@@ -116,16 +116,21 @@ def test_services_validate_in_parallel_with_one_validator(validator: DataValidat
|
|
|
116
116
|
|
|
117
117
|
|
|
118
118
|
def test_a_request_rule_condition_is_checked(validator: DataValidator) -> None:
|
|
119
|
-
"""``specs validate``
|
|
119
|
+
"""``specs validate`` accepts a Lua condition, template references included,
|
|
120
|
+
unparsed, and reports an empty one with the rule it is in
|
|
121
|
+
(unitysvc/unitysvc#2947, apisix-gateways#96)."""
|
|
120
122
|
files = _files(SERVICES[0])
|
|
121
123
|
offering = files["offering"]
|
|
122
124
|
channel = next(iter(offering["upstream_access_config"].values()))
|
|
123
|
-
channel["request_rules"] = [
|
|
125
|
+
channel["request_rules"] = [
|
|
126
|
+
{"when": 'header["x-github-event"] == "ping" or (body.n or 0) > 3', "respond": {"status": 200}},
|
|
127
|
+
{"when": 'body.user_id == "${ customer_secrets.HOOK_ID ?? }"', "respond": {"status": 204}},
|
|
128
|
+
]
|
|
124
129
|
assert validator.validate_service(offering=offering)["offering"] == []
|
|
125
130
|
|
|
126
|
-
channel["request_rules"] = [{"when": "
|
|
131
|
+
channel["request_rules"] = [{"when": " ", "respond": {"status": 204}}]
|
|
127
132
|
errors = validator.validate_service(offering=offering)["offering"]
|
|
128
|
-
assert any("request_rules" in e and "
|
|
133
|
+
assert any("request_rules" in e and "must not be empty" in e for e in errors)
|
|
129
134
|
|
|
130
135
|
|
|
131
136
|
@pytest.mark.parametrize(
|
|
@@ -133,9 +138,7 @@ def test_a_request_rule_condition_is_checked(validator: DataValidator) -> None:
|
|
|
133
138
|
[
|
|
134
139
|
{"respond": {"status": 200, "headers": {"Content-Length\n": "100"}}},
|
|
135
140
|
{"respond": {"status": 200, "body": "ok", "content_type": "text/plain\r\n; charset=utf-8"}},
|
|
136
|
-
{"when": "
|
|
137
|
-
{"when": "method == 'GET'\u00a0", "respond": {"status": 200}},
|
|
138
|
-
{"when": "body.id == '${ customer_secrets.HOOK ?? }\n'", "respond": {"status": 204}},
|
|
141
|
+
{"when": " ", "respond": {"status": 204}},
|
|
139
142
|
],
|
|
140
143
|
)
|
|
141
144
|
def test_request_rule_boundary_errors_reach_service_validation(validator: DataValidator, rule: dict) -> None:
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
"""Request rules: conditions a channel's gateway answers itself (unitysvc/unitysvc#2947).
|
|
2
|
-
|
|
3
|
-
A channel's ``request_rules`` lists rules the gateway checks, in order, after
|
|
4
|
-
authentication, channel selection and rate limiting and before anything is
|
|
5
|
-
dispatched. The first rule whose condition holds answers the request with its
|
|
6
|
-
static response; when none holds the request continues unchanged::
|
|
7
|
-
|
|
8
|
-
"request_rules": [
|
|
9
|
-
{"name": "github-ping", "when": "header['x-github-event'] == 'ping'",
|
|
10
|
-
"respond": {"status": 200}},
|
|
11
|
-
{"when": "body.user_id == '${ customer_secrets.DISCORD_WEBHOOK_ID ?? }'",
|
|
12
|
-
"respond": {"status": 204}}
|
|
13
|
-
]
|
|
14
|
-
|
|
15
|
-
A condition is one string comparison between a request value and a literal::
|
|
16
|
-
|
|
17
|
-
when := ref "==" literal | ref "in" literal | literal "in" ref
|
|
18
|
-
ref := root ( "." ident | "[" string "]" )*
|
|
19
|
-
root := "body" | "header" | "query" | "method"
|
|
20
|
-
literal := a quoted string ('...' or "...", escapes \\\\ \\' \\")
|
|
21
|
-
|
|
22
|
-
``ref in 'lit'`` holds when the request value is a non-empty substring of the
|
|
23
|
-
literal, ``'lit' in ref`` when the literal is a non-empty substring of the
|
|
24
|
-
request value. A request value that is not a string never matches.
|
|
25
|
-
|
|
26
|
-
A literal whose whole content is one reference is a **slot**:
|
|
27
|
-
``'${ customer_secrets.X }'``, ``'${ customer_secrets.X ?? default }'``,
|
|
28
|
-
``'${ secrets.X }'``, ``'{{ params.X }}'`` or ``'{{ enrollment.code }}'``. The
|
|
29
|
-
gateway fills a slot with the resolved value when it evaluates the condition;
|
|
30
|
-
the value is never spliced into the condition's text. A slot that is unset or
|
|
31
|
-
resolves to an empty string never matches. A secret slot may only be compared
|
|
32
|
-
with ``==``: in ``ref in '${ secret }'`` the caller controls ``ref`` and would
|
|
33
|
-
learn the secret one character at a time.
|
|
34
|
-
|
|
35
|
-
The gateway evaluates conditions (apisix-gateways ``lib/rules.lua``); this
|
|
36
|
-
module validates them at upload. :func:`evaluate` exists to run the shared
|
|
37
|
-
conformance cases (``rules_conformance.json``) that keep the two in step.
|
|
38
|
-
"""
|
|
39
|
-
|
|
40
|
-
from __future__ import annotations
|
|
41
|
-
|
|
42
|
-
import hmac
|
|
43
|
-
import re
|
|
44
|
-
from collections.abc import Mapping
|
|
45
|
-
from dataclasses import dataclass
|
|
46
|
-
from typing import Any, Literal
|
|
47
|
-
|
|
48
|
-
ROOTS = ("body", "header", "query", "method")
|
|
49
|
-
MAX_RULES = 32
|
|
50
|
-
MAX_CONDITION_LENGTH = 1024
|
|
51
|
-
MAX_LITERAL_LENGTH = 1024
|
|
52
|
-
MAX_REF_DEPTH = 16
|
|
53
|
-
|
|
54
|
-
_SECRET_NAMESPACES = ("secrets", "customer_secrets")
|
|
55
|
-
# Match Lua's %s, including when stripping a suffix with no next token.
|
|
56
|
-
_WHITESPACE = " \t\n\r\f\v"
|
|
57
|
-
_SECRET_SLOT_RE = re.compile(
|
|
58
|
-
r"^\$\{\s*(secrets|customer_secrets)\.([A-Za-z_][A-Za-z0-9_]*)\s*(?:\?\?\s*(.*?))?\s*\}$", re.DOTALL | re.ASCII
|
|
59
|
-
)
|
|
60
|
-
_PARAM_SLOT_RE = re.compile(r"^\{\{\s*(params\.[A-Za-z_][A-Za-z0-9_]*|enrollment\.code)\s*\}\}$", re.ASCII)
|
|
61
|
-
_IDENT_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
|
|
62
|
-
_TOKEN_RE = re.compile(r"\s*(?:(==)|(\[)|(\])|(\.)|('|\")|([A-Za-z_][A-Za-z0-9_]*))", re.ASCII)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
class RuleSyntaxError(ValueError):
|
|
66
|
-
"""A request-rule condition does not parse, or breaks a rule of the language."""
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
@dataclass(frozen=True)
|
|
70
|
-
class Ref:
|
|
71
|
-
"""A request value: a root and the keys looked up under it."""
|
|
72
|
-
|
|
73
|
-
root: str
|
|
74
|
-
keys: tuple[str, ...] = ()
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
@dataclass(frozen=True)
|
|
78
|
-
class Slot:
|
|
79
|
-
"""A literal that is one reference, filled when the gateway evaluates the condition."""
|
|
80
|
-
|
|
81
|
-
namespace: str # secrets, customer_secrets, params or enrollment
|
|
82
|
-
name: str
|
|
83
|
-
default: str | None = None # the ``?? default`` of a secret reference
|
|
84
|
-
|
|
85
|
-
@property
|
|
86
|
-
def key(self) -> str:
|
|
87
|
-
"""The reference without braces, e.g. ``customer_secrets.X``."""
|
|
88
|
-
return f"{self.namespace}.{self.name}"
|
|
89
|
-
|
|
90
|
-
@property
|
|
91
|
-
def is_secret(self) -> bool:
|
|
92
|
-
return self.namespace in _SECRET_NAMESPACES
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
@dataclass(frozen=True)
|
|
96
|
-
class Condition:
|
|
97
|
-
"""A parsed condition: ``ref == literal``, ``ref in literal`` or ``literal in ref``."""
|
|
98
|
-
|
|
99
|
-
op: Literal["==", "in"]
|
|
100
|
-
ref: Ref
|
|
101
|
-
literal: str | Slot
|
|
102
|
-
literal_first: bool = False # True for ``literal in ref``
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def _literal(content: str) -> str | Slot:
|
|
106
|
-
"""A literal's value, or the slot it names. A reference mixed with other text is an error."""
|
|
107
|
-
if match := _SECRET_SLOT_RE.fullmatch(content):
|
|
108
|
-
return Slot(match.group(1), match.group(2), match.group(3))
|
|
109
|
-
if match := _PARAM_SLOT_RE.fullmatch(content):
|
|
110
|
-
namespace, name = match.group(1).split(".", 1)
|
|
111
|
-
return Slot(namespace, name)
|
|
112
|
-
if "${" in content or "{{" in content:
|
|
113
|
-
raise RuleSyntaxError(
|
|
114
|
-
f"'{content}' is not a valid reference literal: a reference must be the whole literal, one of "
|
|
115
|
-
"'${ customer_secrets.NAME }', '${ customer_secrets.NAME ?? default }', '${ secrets.NAME }', "
|
|
116
|
-
"'{{ params.NAME }}' or '{{ enrollment.code }}'"
|
|
117
|
-
)
|
|
118
|
-
if len(content.encode()) > MAX_LITERAL_LENGTH:
|
|
119
|
-
raise RuleSyntaxError(f"a literal is longer than {MAX_LITERAL_LENGTH} bytes")
|
|
120
|
-
return content
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
class _Parser:
|
|
124
|
-
def __init__(self, text: str) -> None:
|
|
125
|
-
self.text = text
|
|
126
|
-
self.pos = 0
|
|
127
|
-
|
|
128
|
-
def fail(self, message: str) -> RuleSyntaxError:
|
|
129
|
-
return RuleSyntaxError(f"{message} at position {self.pos} in {self.text!r}")
|
|
130
|
-
|
|
131
|
-
def peek(self) -> tuple[str, str] | None:
|
|
132
|
-
"""The next token as (kind, text) without consuming it; None at the end."""
|
|
133
|
-
match = _TOKEN_RE.match(self.text, self.pos)
|
|
134
|
-
if not match:
|
|
135
|
-
if self.text[self.pos :].strip(_WHITESPACE):
|
|
136
|
-
raise self.fail("unexpected character")
|
|
137
|
-
return None
|
|
138
|
-
kinds = ("op", "lbracket", "rbracket", "dot", "quote", "ident")
|
|
139
|
-
index = next(i for i, group in enumerate(match.groups()) if group is not None)
|
|
140
|
-
return kinds[index], match.group(index + 1)
|
|
141
|
-
|
|
142
|
-
def take(self) -> tuple[str, str] | None:
|
|
143
|
-
token = self.peek()
|
|
144
|
-
if token is not None:
|
|
145
|
-
match = _TOKEN_RE.match(self.text, self.pos)
|
|
146
|
-
assert match is not None
|
|
147
|
-
self.pos = match.end()
|
|
148
|
-
return token
|
|
149
|
-
|
|
150
|
-
def string(self) -> str:
|
|
151
|
-
"""Read a quoted string; the opening quote is the next token."""
|
|
152
|
-
token = self.take()
|
|
153
|
-
assert token is not None and token[0] == "quote"
|
|
154
|
-
quote, out = token[1], list[str]()
|
|
155
|
-
while self.pos < len(self.text):
|
|
156
|
-
char = self.text[self.pos]
|
|
157
|
-
self.pos += 1
|
|
158
|
-
if char == quote:
|
|
159
|
-
return "".join(out)
|
|
160
|
-
if char == "\\":
|
|
161
|
-
if self.pos >= len(self.text) or self.text[self.pos] not in "\\'\"":
|
|
162
|
-
raise self.fail("unsupported escape (only \\\\, \\' and \\\")")
|
|
163
|
-
char = self.text[self.pos]
|
|
164
|
-
self.pos += 1
|
|
165
|
-
out.append(char)
|
|
166
|
-
raise self.fail("unterminated string")
|
|
167
|
-
|
|
168
|
-
def ref(self) -> Ref:
|
|
169
|
-
token = self.take()
|
|
170
|
-
assert token is not None and token[0] == "ident"
|
|
171
|
-
root, keys = token[1], []
|
|
172
|
-
if root not in ROOTS:
|
|
173
|
-
raise self.fail(f"unknown root '{root}' (expected one of {', '.join(ROOTS)})")
|
|
174
|
-
while (token := self.peek()) is not None and token[0] in ("dot", "lbracket"):
|
|
175
|
-
self.take()
|
|
176
|
-
if token[0] == "dot":
|
|
177
|
-
ident = self.take()
|
|
178
|
-
if ident is None or ident[0] != "ident":
|
|
179
|
-
raise self.fail("expected a name after '.'")
|
|
180
|
-
keys.append(ident[1])
|
|
181
|
-
else:
|
|
182
|
-
if self.text[self.pos :].lstrip(_WHITESPACE)[:1] not in ("'", '"'):
|
|
183
|
-
raise self.fail("expected a quoted key inside [ ]")
|
|
184
|
-
keys.append(self.string())
|
|
185
|
-
if (self.take() or ("", ""))[0] != "rbracket":
|
|
186
|
-
raise self.fail("expected ']'")
|
|
187
|
-
if len(keys) > MAX_REF_DEPTH:
|
|
188
|
-
raise self.fail(f"a reference is deeper than {MAX_REF_DEPTH} keys")
|
|
189
|
-
if root == "method" and keys:
|
|
190
|
-
raise self.fail("'method' takes no keys")
|
|
191
|
-
if root in ("header", "query") and len(keys) != 1:
|
|
192
|
-
raise self.fail(f"'{root}' takes exactly one key, e.g. {root}['name']")
|
|
193
|
-
if root == "header":
|
|
194
|
-
keys = [keys[0].lower()]
|
|
195
|
-
return Ref(root, tuple(keys))
|
|
196
|
-
|
|
197
|
-
def operand(self) -> Ref | str | Slot:
|
|
198
|
-
token = self.peek()
|
|
199
|
-
if token is None:
|
|
200
|
-
raise self.fail("expected a request value or a quoted literal")
|
|
201
|
-
if token[0] == "quote":
|
|
202
|
-
return _literal(self.string())
|
|
203
|
-
if token[0] == "ident":
|
|
204
|
-
if token[1] == "in":
|
|
205
|
-
raise self.fail("expected a request value or a quoted literal")
|
|
206
|
-
return self.ref()
|
|
207
|
-
raise self.fail("expected a request value or a quoted literal")
|
|
208
|
-
|
|
209
|
-
def operator(self) -> Literal["==", "in"]:
|
|
210
|
-
token = self.take()
|
|
211
|
-
if token == ("op", "=="):
|
|
212
|
-
return "=="
|
|
213
|
-
if token == ("ident", "in"):
|
|
214
|
-
return "in"
|
|
215
|
-
raise self.fail("expected '==' or 'in'")
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
def parse_condition(text: str) -> Condition:
|
|
219
|
-
"""Parse and check one condition.
|
|
220
|
-
|
|
221
|
-
Raises:
|
|
222
|
-
RuleSyntaxError: anything outside the language, a secret slot under
|
|
223
|
-
``in``, or a condition longer than :data:`MAX_CONDITION_LENGTH` bytes.
|
|
224
|
-
"""
|
|
225
|
-
if not isinstance(text, str) or not text.strip(_WHITESPACE):
|
|
226
|
-
raise RuleSyntaxError("a condition must be a non-empty string")
|
|
227
|
-
if len(text.encode()) > MAX_CONDITION_LENGTH:
|
|
228
|
-
raise RuleSyntaxError(f"a condition is longer than {MAX_CONDITION_LENGTH} bytes (UTF-8)")
|
|
229
|
-
parser = _Parser(text)
|
|
230
|
-
left = parser.operand()
|
|
231
|
-
op = parser.operator()
|
|
232
|
-
right = parser.operand()
|
|
233
|
-
if parser.peek() is not None:
|
|
234
|
-
raise parser.fail("unexpected text after the condition")
|
|
235
|
-
|
|
236
|
-
if isinstance(left, Ref) and not isinstance(right, Ref):
|
|
237
|
-
condition = Condition(op, left, right)
|
|
238
|
-
elif not isinstance(left, Ref) and isinstance(right, Ref):
|
|
239
|
-
if op == "==":
|
|
240
|
-
raise RuleSyntaxError(f"write the request value first: '{text}' compares a literal == a request value")
|
|
241
|
-
condition = Condition(op, right, left, literal_first=True)
|
|
242
|
-
else:
|
|
243
|
-
raise RuleSyntaxError(f"'{text}' must compare one request value with one quoted literal")
|
|
244
|
-
|
|
245
|
-
if isinstance(condition.literal, Slot) and condition.literal.is_secret and op != "==":
|
|
246
|
-
raise RuleSyntaxError(
|
|
247
|
-
f"'{text}': a secret may only be compared with '==' (a substring test would reveal it "
|
|
248
|
-
"one character at a time)"
|
|
249
|
-
)
|
|
250
|
-
return condition
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
# ---------------------------------------------------------------------------
|
|
254
|
-
# Evaluation, for the conformance cases. The gateway is the real evaluator.
|
|
255
|
-
# ---------------------------------------------------------------------------
|
|
256
|
-
|
|
257
|
-
_MISSING = object()
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
def _request_value(ref: Ref, request: Mapping[str, Any]) -> Any:
|
|
261
|
-
if ref.root == "method":
|
|
262
|
-
return request.get("method", _MISSING)
|
|
263
|
-
if ref.root in ("header", "query"):
|
|
264
|
-
table = request.get("headers" if ref.root == "header" else "query") or {}
|
|
265
|
-
if ref.root == "header":
|
|
266
|
-
table = {str(name).lower(): value for name, value in table.items()}
|
|
267
|
-
return table.get(ref.keys[0], _MISSING)
|
|
268
|
-
value = request.get("body", _MISSING)
|
|
269
|
-
for key in ref.keys:
|
|
270
|
-
if not isinstance(value, dict) or key not in value:
|
|
271
|
-
return _MISSING
|
|
272
|
-
value = value[key]
|
|
273
|
-
return value
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
def _literal_value(literal: str | Slot, slots: Mapping[str, str]) -> Any:
|
|
277
|
-
if isinstance(literal, str):
|
|
278
|
-
return literal
|
|
279
|
-
value = slots.get(literal.key)
|
|
280
|
-
if value is None:
|
|
281
|
-
value = literal.default
|
|
282
|
-
return value if isinstance(value, str) and value != "" else _MISSING
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
def evaluate(condition: Condition, request: Mapping[str, Any], slots: Mapping[str, str] | None = None) -> bool:
|
|
286
|
-
"""Whether ``condition`` holds for ``request``.
|
|
287
|
-
|
|
288
|
-
``request`` has ``body`` (the decoded JSON body; absent when the body is
|
|
289
|
-
not JSON), ``headers``, ``query`` and ``method``. ``slots`` maps a slot's
|
|
290
|
-
:attr:`Slot.key` to its resolved value; an absent key uses the slot's
|
|
291
|
-
``?? default``.
|
|
292
|
-
"""
|
|
293
|
-
value = _request_value(condition.ref, request)
|
|
294
|
-
literal = _literal_value(condition.literal, slots or {})
|
|
295
|
-
if not isinstance(value, str) or literal is _MISSING:
|
|
296
|
-
return False
|
|
297
|
-
if condition.op == "==":
|
|
298
|
-
return hmac.compare_digest(value.encode(), literal.encode())
|
|
299
|
-
if condition.literal_first:
|
|
300
|
-
return literal != "" and literal in value
|
|
301
|
-
return value != "" and value in literal
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"description": "Request-rule condition cases shared by unitysvc-core (unitysvc_core.rules) and apisix-gateways (lib/rules.lua), unitysvc/unitysvc#2947. A case with 'error' must fail to parse, its message containing that text. Any other case must parse and evaluate to 'match' against 'request' (body absent = not JSON) with 'slots' (resolved values by reference; an absent reference uses its ?? default). Copy this file unchanged between the two repos.",
|
|
3
|
-
"version": 1,
|
|
4
|
-
"cases": [
|
|
5
|
-
{"name": "body field equals", "when": "body.type == 'ping'", "request": {"body": {"type": "ping"}}, "match": true},
|
|
6
|
-
{"name": "body field differs", "when": "body.type == 'ping'", "request": {"body": {"type": "pong"}}, "match": false},
|
|
7
|
-
{"name": "nested body field", "when": "body.author.id == '42'", "request": {"body": {"author": {"id": "42"}}}, "match": true},
|
|
8
|
-
{"name": "bracket key in body", "when": "body['user-id'] == 'u'", "request": {"body": {"user-id": "u"}}, "match": true},
|
|
9
|
-
{"name": "bracket and dot are the same lookup", "when": "body['author'].name == 'a'", "request": {"body": {"author": {"name": "a"}}}, "match": true},
|
|
10
|
-
{"name": "missing body field", "when": "body.type == 'ping'", "request": {"body": {}}, "match": false},
|
|
11
|
-
{"name": "missing intermediate object", "when": "body.author.id == '1'", "request": {"body": {"author": "x"}}, "match": false},
|
|
12
|
-
{"name": "body not json", "when": "body.type == 'ping'", "request": {}, "match": false},
|
|
13
|
-
{"name": "body is an array", "when": "body.type == 'ping'", "request": {"body": [{"type": "ping"}]}, "match": false},
|
|
14
|
-
{"name": "number never equals a string", "when": "body.id == '123'", "request": {"body": {"id": 123}}, "match": false},
|
|
15
|
-
{"name": "boolean never equals a string", "when": "body.bot == 'true'", "request": {"body": {"bot": true}}, "match": false},
|
|
16
|
-
{"name": "null never equals a string", "when": "body.x == 'null'", "request": {"body": {"x": null}}, "match": false},
|
|
17
|
-
{"name": "object never matches", "when": "'a' in body.x", "request": {"body": {"x": {"a": "a"}}}, "match": false},
|
|
18
|
-
{"name": "empty string equals empty literal", "when": "body.x == ''", "request": {"body": {"x": ""}}, "match": true},
|
|
19
|
-
{"name": "equality is case sensitive", "when": "body.type == 'Ping'", "request": {"body": {"type": "ping"}}, "match": false},
|
|
20
|
-
{"name": "unicode equality", "when": "body.name == 'Bo Peng - 测试'", "request": {"body": {"name": "Bo Peng - 测试"}}, "match": true},
|
|
21
|
-
|
|
22
|
-
{"name": "header lookup", "when": "header['x-github-event'] == 'ping'", "request": {"headers": {"x-github-event": "ping"}}, "match": true},
|
|
23
|
-
{"name": "header names are case insensitive", "when": "header['X-GitHub-Event'] == 'ping'", "request": {"headers": {"X-Github-Event": "ping"}}, "match": true},
|
|
24
|
-
{"name": "header values are case sensitive", "when": "header['x-github-event'] == 'ping'", "request": {"headers": {"x-github-event": "PING"}}, "match": false},
|
|
25
|
-
{"name": "missing header", "when": "header['x-github-event'] == 'ping'", "request": {"headers": {}}, "match": false},
|
|
26
|
-
{"name": "query lookup", "when": "query['hub.mode'] == 'subscribe'", "request": {"query": {"hub.mode": "subscribe"}}, "match": true},
|
|
27
|
-
{"name": "missing query argument", "when": "query['hub.mode'] == 'subscribe'", "request": {"query": {}}, "match": false},
|
|
28
|
-
{"name": "method", "when": "method == 'POST'", "request": {"method": "POST"}, "match": true},
|
|
29
|
-
{"name": "method differs", "when": "method == 'POST'", "request": {"method": "GET"}, "match": false},
|
|
30
|
-
|
|
31
|
-
{"name": "ref in literal", "when": "body.type in 'message.created message.updated'", "request": {"body": {"type": "message.created"}}, "match": true},
|
|
32
|
-
{"name": "ref not in literal", "when": "body.type in 'message.created'", "request": {"body": {"type": "channel.updated"}}, "match": false},
|
|
33
|
-
{"name": "empty ref is never in a literal", "when": "body.type in 'abc'", "request": {"body": {"type": ""}}, "match": false},
|
|
34
|
-
{"name": "literal in ref", "when": "'[bot]' in body.sender.login", "request": {"body": {"sender": {"login": "dependabot[bot]"}}}, "match": true},
|
|
35
|
-
{"name": "literal not in ref", "when": "'[bot]' in body.sender.login", "request": {"body": {"sender": {"login": "octocat"}}}, "match": false},
|
|
36
|
-
{"name": "empty literal is never in a ref", "when": "'' in body.x", "request": {"body": {"x": "anything"}}, "match": false},
|
|
37
|
-
{"name": "literal in missing ref", "when": "'a' in body.x", "request": {"body": {}}, "match": false},
|
|
38
|
-
{"name": "double-quoted literal", "when": "body.x == \"it's\"", "request": {"body": {"x": "it's"}}, "match": true},
|
|
39
|
-
{"name": "escaped quote", "when": "body.x == 'it\\'s'", "request": {"body": {"x": "it's"}}, "match": true},
|
|
40
|
-
{"name": "escaped backslash", "when": "body.x == 'a\\\\b'", "request": {"body": {"x": "a\\b"}}, "match": true},
|
|
41
|
-
{"name": "whitespace is insignificant", "when": " body . type == 'ping' ", "request": {"body": {"type": "ping"}}, "match": true},
|
|
42
|
-
|
|
43
|
-
{"name": "customer secret slot", "when": "body.user_id == '${ customer_secrets.DISCORD_WEBHOOK_ID }'", "request": {"body": {"user_id": "123"}}, "slots": {"customer_secrets.DISCORD_WEBHOOK_ID": "123"}, "match": true},
|
|
44
|
-
{"name": "customer secret slot differs", "when": "body.user_id == '${ customer_secrets.DISCORD_WEBHOOK_ID }'", "request": {"body": {"user_id": "124"}}, "slots": {"customer_secrets.DISCORD_WEBHOOK_ID": "123"}, "match": false},
|
|
45
|
-
{"name": "unset secret never matches", "when": "body.user_id == '${ customer_secrets.DISCORD_WEBHOOK_ID ?? }'", "request": {"body": {"user_id": ""}}, "match": false},
|
|
46
|
-
{"name": "empty secret never matches", "when": "body.user_id == '${ customer_secrets.X }'", "request": {"body": {"user_id": ""}}, "slots": {"customer_secrets.X": ""}, "match": false},
|
|
47
|
-
{"name": "secret default applies when unset", "when": "body.v == '${ customer_secrets.X ?? fallback }'", "request": {"body": {"v": "fallback"}}, "match": true},
|
|
48
|
-
{"name": "secret value wins over default", "when": "body.v == '${ customer_secrets.X ?? fallback }'", "request": {"body": {"v": "fallback"}}, "slots": {"customer_secrets.X": "set"}, "match": false},
|
|
49
|
-
{"name": "secret containing a quote is a value, not code", "when": "body.v == '${ customer_secrets.X }'", "request": {"body": {"v": "x' or 'a' == 'a"}}, "slots": {"customer_secrets.X": "x' or 'a' == 'a"}, "match": true},
|
|
50
|
-
{"name": "seller secret slot", "when": "header['x-token'] == '${ secrets.TOKEN }'", "request": {"headers": {"x-token": "t"}}, "slots": {"secrets.TOKEN": "t"}, "match": true},
|
|
51
|
-
{"name": "param slot with in", "when": "'{{ params.marker }}' in body.name", "request": {"body": {"name": "Bo - marker1"}}, "slots": {"params.marker": "marker1"}, "match": true},
|
|
52
|
-
{"name": "ref in param slot", "when": "body.type in '{{ params.types }}'", "request": {"body": {"type": "a"}}, "slots": {"params.types": "a b"}, "match": true},
|
|
53
|
-
{"name": "unset param never matches", "when": "'{{ params.marker }}' in body.name", "request": {"body": {"name": "x"}}, "match": false},
|
|
54
|
-
{"name": "enrollment code slot", "when": "query['code'] == '{{ enrollment.code }}'", "request": {"query": {"code": "ABC"}}, "slots": {"enrollment.code": "ABC"}, "match": true},
|
|
55
|
-
|
|
56
|
-
{"name": "secret under in is refused", "when": "body.user_id in '${ customer_secrets.DISCORD_WEBHOOK_URL ?? }'", "error": "may only be compared with '=='"},
|
|
57
|
-
{"name": "secret on the left of in is refused", "when": "'${ secrets.X }' in body.v", "error": "may only be compared with '=='"},
|
|
58
|
-
{"name": "reference mixed with text is refused", "when": "body.v == 'id-${ customer_secrets.X }'", "error": "must be the whole literal"},
|
|
59
|
-
{"name": "jinja indirection is refused", "when": "body.v == '${ customer_secrets.{{ params.n }} }'", "error": "must be the whole literal"},
|
|
60
|
-
{"name": "jinja expression is refused", "when": "body.v == '{{ params.a | upper }}'", "error": "must be the whole literal"},
|
|
61
|
-
{"name": "literal first with == is refused", "when": "'ping' == body.type", "error": "write the request value first"},
|
|
62
|
-
{"name": "two refs are refused", "when": "body.a == body.b", "error": "one request value with one quoted literal"},
|
|
63
|
-
{"name": "two literals are refused", "when": "'a' == 'a'", "error": "one request value with one quoted literal"},
|
|
64
|
-
{"name": "unknown root", "when": "cookie.a == 'b'", "error": "unknown root"},
|
|
65
|
-
{"name": "unknown operator", "when": "body.a != 'b'", "error": "unexpected character"},
|
|
66
|
-
{"name": "and is not supported", "when": "body.a == 'b' and body.c == 'd'", "error": "unexpected text after the condition"},
|
|
67
|
-
{"name": "number literal is refused", "when": "body.a == 1", "error": "unexpected character"},
|
|
68
|
-
{"name": "bare word literal is refused", "when": "body.a == ping", "error": "unknown root"},
|
|
69
|
-
{"name": "unterminated string", "when": "body.a == 'b", "error": "unterminated string"},
|
|
70
|
-
{"name": "unsupported escape", "when": "body.a == 'b\\n'", "error": "unsupported escape"},
|
|
71
|
-
{"name": "method takes no keys", "when": "method.x == 'GET'", "error": "'method' takes no keys"},
|
|
72
|
-
{"name": "header needs one key", "when": "header == 'x'", "error": "'header' takes exactly one key"},
|
|
73
|
-
{"name": "header takes no nested keys", "when": "header['a'].b == 'x'", "error": "'header' takes exactly one key"},
|
|
74
|
-
{"name": "unquoted bracket key", "when": "body[0] == 'x'", "error": "expected a quoted key"},
|
|
75
|
-
{"name": "missing operand", "when": "body.a ==", "error": "expected a request value or a quoted literal"},
|
|
76
|
-
{"name": "missing operator", "when": "body.a 'b'", "error": "expected '==' or 'in'"},
|
|
77
|
-
{"name": "length is measured in UTF-8 bytes", "when": "body.a == '测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测测'", "error": "longer than"},
|
|
78
|
-
{"name": "ASCII whitespace is accepted", "when": "\tmethod \r\n== \u000b'GET'\f", "request": {"method": "GET"}, "match": true},
|
|
79
|
-
{"name": "Unicode whitespace in a literal is preserved", "when": "body.value == 'a\u00a0b'", "request": {"body": {"value": "a\u00a0b"}}, "match": true},
|
|
80
|
-
{"name": "leading Unicode whitespace is refused", "when": "\u2003method == 'GET'", "error": "unexpected character"},
|
|
81
|
-
{"name": "Unicode whitespace between tokens is refused", "when": "method\u00a0== 'GET'", "error": "unexpected character"},
|
|
82
|
-
{"name": "Unicode whitespace before a literal is refused", "when": "method ==\u2003'GET'", "error": "unexpected character"},
|
|
83
|
-
{"name": "trailing Unicode whitespace is refused", "when": "method == 'GET'\u00a0", "error": "unexpected character"},
|
|
84
|
-
{"name": "Unicode whitespace in a secret slot is refused", "when": "body.id == '${\u00a0customer_secrets.HOOK ?? }'", "error": "must be the whole literal"},
|
|
85
|
-
{"name": "Unicode whitespace in a param slot is refused", "when": "body.id == '{{\u00a0params.VAR }}'", "error": "must be the whole literal"},
|
|
86
|
-
{"name": "a newline after a secret reference is refused", "when": "body.id == '${ customer_secrets.HOOK ?? }\n'", "error": "must be the whole literal"},
|
|
87
|
-
{"name": "a newline after a param reference is refused", "when": "body.id == '{{ params.VAR }}\n'", "error": "must be the whole literal"},
|
|
88
|
-
{"name": "a newline after an enrollment reference is refused", "when": "body.id == '{{ enrollment.code }}\n'", "error": "must be the whole literal"},
|
|
89
|
-
{"name": "empty condition", "when": " ", "error": "non-empty string"}
|
|
90
|
-
]
|
|
91
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|