@fuzdev/fuz_app 0.163.0 → 0.165.0
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.
- package/README.md +2 -1
- package/dist/actions/action_event_data.d.ts +1 -1
- package/dist/actions/action_rpc.d.ts.map +1 -1
- package/dist/actions/action_rpc.js +4 -0
- package/dist/actions/action_spec.d.ts +3 -3
- package/dist/actions/action_spec.d.ts.map +1 -1
- package/dist/auth/CLAUDE.md +1 -1
- package/dist/auth/account_route_schema.d.ts +3 -27
- package/dist/auth/account_route_schema.d.ts.map +1 -1
- package/dist/auth/account_route_schema.js +6 -20
- package/dist/auth/admin_action_specs.d.ts +56 -56
- package/dist/auth/audit_log_route_schema.d.ts +1 -1
- package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_route_schema.js +4 -5
- package/dist/auth/audit_log_schema.d.ts +18 -18
- package/dist/auth/bootstrap_route_schema.d.ts +1 -30
- package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
- package/dist/auth/bootstrap_route_schema.js +3 -8
- package/dist/auth/daemon_token.d.ts +4 -4
- package/dist/auth/daemon_token.d.ts.map +1 -1
- package/dist/auth/daemon_token.js +3 -3
- package/dist/auth/daemon_token_middleware.d.ts +1 -1
- package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.js +5 -5
- package/dist/auth/middleware.js +4 -4
- package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
- package/dist/auth/signup_route_schema.d.ts.map +1 -1
- package/dist/auth/signup_route_schema.js +3 -7
- package/dist/db/CLAUDE.md +2 -1
- package/dist/db/file_fact_url.d.ts +11 -6
- package/dist/db/file_fact_url.d.ts.map +1 -1
- package/dist/db/file_fact_url.js +5 -5
- package/dist/db/schema_ready.d.ts +0 -5
- package/dist/db/schema_ready.d.ts.map +1 -1
- package/dist/db/schema_ready.js +0 -5
- package/dist/http/CLAUDE.md +105 -71
- package/dist/http/auth_shape.d.ts +48 -29
- package/dist/http/auth_shape.d.ts.map +1 -1
- package/dist/http/auth_shape.js +12 -85
- package/dist/http/body_limit.d.ts +12 -0
- package/dist/http/body_limit.d.ts.map +1 -1
- package/dist/http/body_limit.js +15 -0
- package/dist/http/body_limit_schema.d.ts +20 -0
- package/dist/http/body_limit_schema.d.ts.map +1 -0
- package/dist/http/body_limit_schema.js +20 -0
- package/dist/http/common_routes.d.ts +1 -1
- package/dist/http/common_routes.d.ts.map +1 -1
- package/dist/http/common_routes.js +11 -14
- package/dist/http/db_routes.d.ts.map +1 -1
- package/dist/http/db_routes.js +59 -69
- package/dist/http/dev_route_error_check.d.ts +70 -0
- package/dist/http/dev_route_error_check.d.ts.map +1 -0
- package/dist/http/dev_route_error_check.js +119 -0
- package/dist/http/error_schemas.d.ts +365 -189
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/error_schemas.js +256 -149
- package/dist/http/health_schema.d.ts +10 -6
- package/dist/http/health_schema.d.ts.map +1 -1
- package/dist/http/health_schema.js +3 -3
- package/dist/http/middleware_spec.d.ts +8 -3
- package/dist/http/middleware_spec.d.ts.map +1 -1
- package/dist/http/origin.d.ts +12 -43
- package/dist/http/origin.d.ts.map +1 -1
- package/dist/http/origin.js +10 -6
- package/dist/http/route_error_check.d.ts +179 -0
- package/dist/http/route_error_check.d.ts.map +1 -0
- package/dist/http/route_error_check.js +283 -0
- package/dist/http/route_errors.d.ts +80 -0
- package/dist/http/route_errors.d.ts.map +1 -0
- package/dist/http/route_errors.js +83 -0
- package/dist/http/route_spec.d.ts +33 -17
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/http/route_spec.js +48 -62
- package/dist/http/schema_helpers.d.ts +15 -34
- package/dist/http/schema_helpers.d.ts.map +1 -1
- package/dist/http/schema_helpers.js +11 -47
- package/dist/http/surface.d.ts +19 -12
- package/dist/http/surface.d.ts.map +1 -1
- package/dist/http/surface.js +25 -38
- package/dist/http/surface_query.d.ts +11 -2
- package/dist/http/surface_query.d.ts.map +1 -1
- package/dist/http/surface_query.js +7 -1
- package/dist/primitive_schemas_fuz.d.ts +17 -3
- package/dist/primitive_schemas_fuz.d.ts.map +1 -1
- package/dist/primitive_schemas_fuz.js +13 -3
- package/dist/rate_limiter.d.ts +13 -0
- package/dist/rate_limiter.d.ts.map +1 -1
- package/dist/rate_limiter.js +20 -2
- package/dist/schema_issues.d.ts +29 -11
- package/dist/schema_issues.d.ts.map +1 -1
- package/dist/schema_issues.js +11 -10
- package/dist/schema_meta.d.ts +8 -1
- package/dist/schema_meta.d.ts.map +1 -1
- package/dist/schema_meta.js +2 -1
- package/dist/server/app_server.d.ts +3 -1
- package/dist/server/app_server.d.ts.map +1 -1
- package/dist/server/app_server.js +24 -10
- package/dist/server/serve_fact_route.d.ts.map +1 -1
- package/dist/server/serve_fact_route.js +8 -31
- package/dist/server/surface_route.d.ts.map +1 -1
- package/dist/server/surface_route.js +5 -6
- package/dist/server/x_accel.d.ts +12 -7
- package/dist/server/x_accel.d.ts.map +1 -1
- package/dist/server/x_accel.js +7 -9
- package/dist/testing/CLAUDE.md +105 -61
- package/dist/testing/admin_integration.js +2 -2
- package/dist/testing/adversarial_404.d.ts +4 -18
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +20 -52
- package/dist/testing/adversarial_headers.d.ts +3 -4
- package/dist/testing/adversarial_headers.d.ts.map +1 -1
- package/dist/testing/adversarial_headers.js +8 -6
- package/dist/testing/adversarial_input.d.ts.map +1 -1
- package/dist/testing/adversarial_input.js +20 -19
- package/dist/testing/assertions.d.ts +46 -14
- package/dist/testing/assertions.d.ts.map +1 -1
- package/dist/testing/assertions.js +73 -16
- package/dist/testing/attack_surface.d.ts +17 -35
- package/dist/testing/attack_surface.d.ts.map +1 -1
- package/dist/testing/attack_surface.js +36 -95
- package/dist/testing/auth_apps.d.ts.map +1 -1
- package/dist/testing/auth_apps.js +2 -1
- package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
- package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
- package/dist/testing/cross_backend/action_manifest.js +11 -11
- package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
- package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_crud.js +2 -1
- package/dist/testing/cross_backend/conformance_table.js +1 -1
- package/dist/testing/cross_backend/fact_serving.js +3 -3
- package/dist/testing/cross_backend/in_process_setup.d.ts +7 -3
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +11 -4
- package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
- package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
- package/dist/testing/cross_backend/origin_conformance.js +21 -21
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
- package/dist/testing/cross_backend/setup.d.ts +13 -0
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +24 -7
- package/dist/testing/cross_backend/spine_suites.d.ts +3 -2
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +5 -2
- package/dist/testing/cross_backend/standard.d.ts +31 -0
- package/dist/testing/cross_backend/standard.d.ts.map +1 -1
- package/dist/testing/cross_backend/standard.js +9 -3
- package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
- package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_origin_check.js +16 -13
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/data_exposure.d.ts +7 -0
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +8 -7
- package/dist/testing/error_coverage.d.ts +53 -61
- package/dist/testing/error_coverage.d.ts.map +1 -1
- package/dist/testing/error_coverage.js +86 -119
- package/dist/testing/integration.d.ts +28 -1
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +54 -29
- package/dist/testing/integration_helpers.d.ts +37 -9
- package/dist/testing/integration_helpers.d.ts.map +1 -1
- package/dist/testing/integration_helpers.js +75 -40
- package/dist/testing/middleware.d.ts +3 -5
- package/dist/testing/middleware.d.ts.map +1 -1
- package/dist/testing/middleware.js +5 -4
- package/dist/testing/middleware_gate_probes.d.ts +90 -0
- package/dist/testing/middleware_gate_probes.d.ts.map +1 -0
- package/dist/testing/middleware_gate_probes.js +179 -0
- package/dist/testing/rate_limiting.d.ts +1 -1
- package/dist/testing/rate_limiting.d.ts.map +1 -1
- package/dist/testing/rate_limiting.js +5 -4
- package/dist/testing/round_trip.d.ts +7 -0
- package/dist/testing/round_trip.d.ts.map +1 -1
- package/dist/testing/round_trip.js +8 -5
- package/dist/testing/schema_generators.d.ts +40 -11
- package/dist/testing/schema_generators.d.ts.map +1 -1
- package/dist/testing/schema_generators.js +71 -17
- package/dist/testing/stubs.d.ts +10 -1
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +15 -5
- package/dist/testing/surface_invariants.d.ts +36 -115
- package/dist/testing/surface_invariants.d.ts.map +1 -1
- package/dist/testing/surface_invariants.js +106 -269
- package/dist/testing/transports/raw_http.d.ts +10 -2
- package/dist/testing/transports/raw_http.d.ts.map +1 -1
- package/dist/testing/transports/raw_http.js +10 -4
- package/dist/ui/CLAUDE.md +4 -2
- package/dist/ui/auth_state.svelte.d.ts.map +1 -1
- package/dist/ui/auth_state.svelte.js +2 -8
- package/dist/ui/backend_probe.svelte.js +3 -3
- package/dist/ui/table_state.svelte.d.ts.map +1 -1
- package/dist/ui/table_state.svelte.js +1 -8
- package/dist/ui/ui_fetch.d.ts +12 -2
- package/dist/ui/ui_fetch.d.ts.map +1 -1
- package/dist/ui/ui_fetch.js +22 -4
- package/package.json +1 -1
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import './assert_dev_env.js';
|
|
2
2
|
import type { RouteSpec, RouteMethod } from '../http/route_spec.js';
|
|
3
|
+
import { type RouteErrorMap } from '../http/route_errors.js';
|
|
3
4
|
import { type RouteAuth } from '../http/auth_shape.js';
|
|
4
5
|
import type { TestAccount } from './app_server.js';
|
|
5
6
|
/**
|
|
6
|
-
* Find
|
|
7
|
+
* Find the route spec a request reaches: the first in list (mount) order
|
|
8
|
+
* whose method and path template match it, `HEAD` read as `GET` — the order
|
|
9
|
+
* Hono dispatches by, which the DEV error check reads through Hono's own
|
|
10
|
+
* router (`create_route_lookup`). This lookup is hono-free
|
|
11
|
+
* (`find_mounted_route`), pinned against Hono by the route-lookup tests. The
|
|
12
|
+
* path is read as Hono reads it: its pathname, percent-decoded.
|
|
7
13
|
*
|
|
8
|
-
*
|
|
14
|
+
* A path template passed as the path (`/api/things/:id`, a segment that is
|
|
15
|
+
* `*` or starts with `:`) finds its own spec first, outranking mount order.
|
|
9
16
|
*
|
|
10
|
-
* @param
|
|
17
|
+
* @param specs - the route specs, in mount order
|
|
18
|
+
* @param method - the request method
|
|
19
|
+
* @param path - request path, concrete or a route's own template
|
|
11
20
|
*/
|
|
12
21
|
export declare const find_route_spec: (specs: Array<RouteSpec>, method: string, path: string) => RouteSpec | undefined;
|
|
13
22
|
/**
|
|
@@ -33,14 +42,33 @@ export declare const find_auth_route: (specs: Array<RouteSpec>, suffix: RestAuth
|
|
|
33
42
|
/**
|
|
34
43
|
* Validate a response body against the route spec's declared schemas.
|
|
35
44
|
*
|
|
36
|
-
* For 2xx responses, validates against `spec.output`.
|
|
37
|
-
*
|
|
45
|
+
* For 2xx responses, validates against `spec.output`. For an error response
|
|
46
|
+
* (400 and up), checks it against the route's error map
|
|
47
|
+
* (`resolve_route_error_map`) — the one in `route_error_maps`, an
|
|
48
|
+
* `AppSurfaceSpec`'s, built with the app's middleware, or without one the
|
|
49
|
+
* route's own — through `assert_error_response_declared`, by the rule the DEV
|
|
50
|
+
* error check runs (`check_route_error`): the status must be
|
|
51
|
+
* declared and the body must parse against it. A 500 `internal_error` and a
|
|
52
|
+
* 503 `service_unavailable` are always accepted, and on a `jsonrpc` route (the
|
|
53
|
+
* RPC endpoint's) a JSON-RPC error envelope is not a REST error body; on any
|
|
54
|
+
* other route it is checked like any error body.
|
|
38
55
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
56
|
+
* The route is the one `find_route_spec` finds, the request's as Hono
|
|
57
|
+
* dispatches it. A failure reads as the DEV check logs it
|
|
58
|
+
* (`format_route_error_check`), then `: ` and the issues or the body as JSON.
|
|
59
|
+
*
|
|
60
|
+
* Only a JSON response, by its `Content-Type` (`is_json_response`, the DEV
|
|
61
|
+
* check's gate), has a body to check. A non-JSON response passes only where
|
|
62
|
+
* no schema applies: a 2xx on a route with a null output, or an error status
|
|
63
|
+
* the map doesn't declare.
|
|
64
|
+
*
|
|
65
|
+
* @param route_error_maps - each route's error map, keyed `METHOD path` (`AppSurfaceSpec.route_error_maps`)
|
|
66
|
+
* @throws Error if no route spec matches `method` + `path`, if the body fails
|
|
67
|
+
* its output schema, if a JSON error is at a status the route's map lacks or
|
|
68
|
+
* off its schema, or if the response is non-JSON despite a declared schema
|
|
69
|
+
* for its status.
|
|
42
70
|
*/
|
|
43
|
-
export declare const assert_response_matches_spec: (route_specs: Array<RouteSpec>, method: string, path: string, response: Response) => Promise<void>;
|
|
71
|
+
export declare const assert_response_matches_spec: (route_specs: Array<RouteSpec>, method: string, path: string, response: Response, route_error_maps?: ReadonlyMap<string, RouteErrorMap> | null) => Promise<void>;
|
|
44
72
|
/**
|
|
45
73
|
* Assert that an error response contains no leaky field values.
|
|
46
74
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integration_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/integration_helpers.ts"],"names":[],"m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F;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,GAAI,MAAM,SAAS,KAAG,aAKrD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,GAChC,WAAW,aAAa,EACxB,QAAQ,oBAAoB,EAC5B,gBAAgB,WAAW,EAC3B,eAAe,WAAW,KACxB,MAAM,CAAC,MAAM,EAAE,MAAM,CAavB,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,GAC7B,MAAM,SAAS,EACf,QAAQ,oBAAoB,EAC5B,gBAAgB,WAAW,EAC3B,eAAe,WAAW,KACxB,MAAM,CAAC,MAAM,EAAE,MAAM,CACoE,CAAC;AAE7F;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,6BAA6B,GACzC,WAAW,aAAa,EACxB,cAAc,OAAO,KACnB,OAAiD,CAAC;AAErD;;;;;;;;;GASG;AACH,eAAO,MAAM,0BAA0B,GAAI,QAAQ,MAAM,EAAE,QAAQ,OAAO,KAAG,OACQ,CAAC"}
|
|
@@ -5,34 +5,53 @@ import './assert_dev_env.js';
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
import { assert } from 'vitest';
|
|
8
|
-
import { is_null_schema
|
|
8
|
+
import { is_null_schema } from '../http/schema_helpers.js';
|
|
9
|
+
import { get_route_error_map } from '../http/route_errors.js';
|
|
9
10
|
import { is_public_auth } from '../http/auth_shape.js';
|
|
10
11
|
import { ERROR_AUTHENTICATION_REQUIRED } from '../http/error_schemas.js';
|
|
11
12
|
import { ROLE_ADMIN } from '../auth/role_schema.js';
|
|
12
13
|
import { create_test_request_headers } from './test_origin.js';
|
|
13
14
|
import { parse_any } from '../schema_issues.js';
|
|
15
|
+
import { decode_route_path, find_mounted_route, is_json_response, to_mounted_routes } from '../http/route_error_check.js';
|
|
16
|
+
import { assert_error_response_declared } from './assertions.js';
|
|
17
|
+
/** Each spec list's compiled routes, so a list is compiled once. */
|
|
18
|
+
const mounted_routes_cache = new WeakMap();
|
|
14
19
|
/**
|
|
15
|
-
*
|
|
20
|
+
* A request path as Hono reads it: the URL's pathname (a query string
|
|
21
|
+
* dropped, `.` and `..` segments resolved), percent-decoded as
|
|
22
|
+
* `decode_route_path` decodes it.
|
|
23
|
+
*/
|
|
24
|
+
const to_hono_path = (path) => decode_route_path(new URL(path, 'http://localhost').pathname);
|
|
25
|
+
/**
|
|
26
|
+
* Find the route spec a request reaches: the first in list (mount) order
|
|
27
|
+
* whose method and path template match it, `HEAD` read as `GET` — the order
|
|
28
|
+
* Hono dispatches by, which the DEV error check reads through Hono's own
|
|
29
|
+
* router (`create_route_lookup`). This lookup is hono-free
|
|
30
|
+
* (`find_mounted_route`), pinned against Hono by the route-lookup tests. The
|
|
31
|
+
* path is read as Hono reads it: its pathname, percent-decoded.
|
|
16
32
|
*
|
|
17
|
-
*
|
|
33
|
+
* A path template passed as the path (`/api/things/:id`, a segment that is
|
|
34
|
+
* `*` or starts with `:`) finds its own spec first, outranking mount order.
|
|
18
35
|
*
|
|
19
|
-
* @param
|
|
36
|
+
* @param specs - the route specs, in mount order
|
|
37
|
+
* @param method - the request method
|
|
38
|
+
* @param path - request path, concrete or a route's own template
|
|
20
39
|
*/
|
|
21
40
|
export const find_route_spec = (specs, method, path) => {
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
// a template names its own spec; only a whole param or wildcard segment
|
|
42
|
+
// makes one, as a concrete path can hold a `:` mid-segment
|
|
43
|
+
if (path.split('/').some((segment) => segment === '*' || segment.startsWith(':'))) {
|
|
44
|
+
const read_method = method === 'HEAD' ? 'GET' : method;
|
|
45
|
+
const own = specs.find((s) => s.method === read_method && s.path === path);
|
|
46
|
+
if (own)
|
|
47
|
+
return own;
|
|
48
|
+
}
|
|
49
|
+
let routes = mounted_routes_cache.get(specs);
|
|
50
|
+
if (!routes) {
|
|
51
|
+
routes = to_mounted_routes(specs, (spec) => spec);
|
|
52
|
+
mounted_routes_cache.set(specs, routes);
|
|
53
|
+
}
|
|
54
|
+
return find_mounted_route(routes, method, to_hono_path(path));
|
|
36
55
|
};
|
|
37
56
|
/**
|
|
38
57
|
* REST auth route suffixes on the account/bootstrap surface — the only
|
|
@@ -68,24 +87,50 @@ export const find_auth_route = (specs, suffix, method) => {
|
|
|
68
87
|
/**
|
|
69
88
|
* Validate a response body against the route spec's declared schemas.
|
|
70
89
|
*
|
|
71
|
-
* For 2xx responses, validates against `spec.output`.
|
|
72
|
-
*
|
|
90
|
+
* For 2xx responses, validates against `spec.output`. For an error response
|
|
91
|
+
* (400 and up), checks it against the route's error map
|
|
92
|
+
* (`resolve_route_error_map`) — the one in `route_error_maps`, an
|
|
93
|
+
* `AppSurfaceSpec`'s, built with the app's middleware, or without one the
|
|
94
|
+
* route's own — through `assert_error_response_declared`, by the rule the DEV
|
|
95
|
+
* error check runs (`check_route_error`): the status must be
|
|
96
|
+
* declared and the body must parse against it. A 500 `internal_error` and a
|
|
97
|
+
* 503 `service_unavailable` are always accepted, and on a `jsonrpc` route (the
|
|
98
|
+
* RPC endpoint's) a JSON-RPC error envelope is not a REST error body; on any
|
|
99
|
+
* other route it is checked like any error body.
|
|
100
|
+
*
|
|
101
|
+
* The route is the one `find_route_spec` finds, the request's as Hono
|
|
102
|
+
* dispatches it. A failure reads as the DEV check logs it
|
|
103
|
+
* (`format_route_error_check`), then `: ` and the issues or the body as JSON.
|
|
73
104
|
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
105
|
+
* Only a JSON response, by its `Content-Type` (`is_json_response`, the DEV
|
|
106
|
+
* check's gate), has a body to check. A non-JSON response passes only where
|
|
107
|
+
* no schema applies: a 2xx on a route with a null output, or an error status
|
|
108
|
+
* the map doesn't declare.
|
|
109
|
+
*
|
|
110
|
+
* @param route_error_maps - each route's error map, keyed `METHOD path` (`AppSurfaceSpec.route_error_maps`)
|
|
111
|
+
* @throws Error if no route spec matches `method` + `path`, if the body fails
|
|
112
|
+
* its output schema, if a JSON error is at a status the route's map lacks or
|
|
113
|
+
* off its schema, or if the response is non-JSON despite a declared schema
|
|
114
|
+
* for its status.
|
|
77
115
|
*/
|
|
78
|
-
export const assert_response_matches_spec = async (route_specs, method, path, response) => {
|
|
116
|
+
export const assert_response_matches_spec = async (route_specs, method, path, response, route_error_maps) => {
|
|
79
117
|
const spec = find_route_spec(route_specs, method, path);
|
|
80
118
|
if (!spec) {
|
|
81
119
|
throw new Error(`No route spec found for ${method} ${path}`);
|
|
82
120
|
}
|
|
83
|
-
|
|
121
|
+
// the DEV error check's gate: only a JSON response has a body to check
|
|
84
122
|
let body;
|
|
85
|
-
|
|
86
|
-
|
|
123
|
+
let parsed = false;
|
|
124
|
+
if (is_json_response(response)) {
|
|
125
|
+
try {
|
|
126
|
+
body = await response.clone().json();
|
|
127
|
+
parsed = true;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// a body claiming JSON that isn't — read as non-JSON below
|
|
131
|
+
}
|
|
87
132
|
}
|
|
88
|
-
|
|
133
|
+
if (!parsed) {
|
|
89
134
|
// Non-JSON response — only acceptable when no schema applies.
|
|
90
135
|
// If the spec declares an output or error schema for this status,
|
|
91
136
|
// getting non-JSON is a real bug.
|
|
@@ -93,8 +138,7 @@ export const assert_response_matches_spec = async (route_specs, method, path, re
|
|
|
93
138
|
throw new Error(`${method} ${path} (${response.status}) returned non-JSON but has output schema`);
|
|
94
139
|
}
|
|
95
140
|
if (!response.ok) {
|
|
96
|
-
|
|
97
|
-
if (merged?.[response.status]) {
|
|
141
|
+
if (get_route_error_map(route_error_maps, spec).schemas[response.status]) {
|
|
98
142
|
throw new Error(`${method} ${path} (${response.status}) returned non-JSON but has error schema for status ${response.status}`);
|
|
99
143
|
}
|
|
100
144
|
}
|
|
@@ -107,16 +151,7 @@ export const assert_response_matches_spec = async (route_specs, method, path, re
|
|
|
107
151
|
}
|
|
108
152
|
}
|
|
109
153
|
else {
|
|
110
|
-
|
|
111
|
-
if (merged) {
|
|
112
|
-
const status_schema = merged[response.status];
|
|
113
|
-
if (status_schema) {
|
|
114
|
-
const result = parse_any(status_schema, body);
|
|
115
|
-
if (!result.ok) {
|
|
116
|
-
throw new Error(`Error schema mismatch for ${method} ${path} (${response.status}): ${JSON.stringify(result.issues)}`);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
154
|
+
await assert_error_response_declared(get_route_error_map(route_error_maps, spec), spec.jsonrpc ?? false, response, `${method} ${path}`);
|
|
120
155
|
}
|
|
121
156
|
};
|
|
122
157
|
/** Fields in error responses that indicate information leakage. */
|
|
@@ -15,9 +15,9 @@ import './assert_dev_env.js';
|
|
|
15
15
|
*/
|
|
16
16
|
import { vi } from 'vitest';
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import type { z } from 'zod';
|
|
19
18
|
import { type RequestContext } from '../auth/request_context.js';
|
|
20
19
|
import type { TokenScope } from '../auth/token_scope.js';
|
|
20
|
+
import type { RestErrorDef } from '../http/error_schemas.js';
|
|
21
21
|
/** Mock configuration for bearer auth middleware test setup. */
|
|
22
22
|
export interface BearerAuthTestOptions {
|
|
23
23
|
/** Test description. */
|
|
@@ -36,10 +36,8 @@ export interface BearerAuthTestOptions {
|
|
|
36
36
|
mock_role_grants_result?: unknown;
|
|
37
37
|
/** Expected HTTP status, or `'next'` if the middleware should call `next()`. */
|
|
38
38
|
expected_status: number | 'next';
|
|
39
|
-
/**
|
|
40
|
-
expected_error?:
|
|
41
|
-
/** Zod schema to validate error response body against. Defaults to `ApiError` when `expected_error` is set. */
|
|
42
|
-
expected_error_schema?: z.ZodType;
|
|
39
|
+
/** The error definition the JSON body must be — its code, and a body its schema accepts. */
|
|
40
|
+
expected_error?: RestErrorDef;
|
|
43
41
|
}
|
|
44
42
|
/** A full test case for the table-driven bearer auth runner. */
|
|
45
43
|
export interface BearerAuthTestCase extends BearerAuthTestOptions {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAE7B;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,EAAE,EAA0B,MAAM,QAAQ,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAE7B;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,EAAE,EAA0B,MAAM,QAAQ,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAoB5B,OAAO,EAAuB,KAAK,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAOtF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAoB7D,gEAAgE;AAChE,MAAM,WAAW,qBAAqB;IACrC,wBAAwB;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,uBAAuB;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,oEAAoE;IACpE,WAAW,CAAC,EAAE,cAAc,CAAC;IAC7B,iDAAiD;IACjD,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,4CAA4C;IAC5C,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,0CAA0C;IAC1C,4BAA4B,CAAC,EAAE,OAAO,CAAC;IACvC,+DAA+D;IAC/D,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,gFAAgF;IAChF,eAAe,EAAE,MAA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,IAAI,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAoErC,CAAC;AAIF;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,GACtC,YAAY,MAAM,EAClB,OAAO,KAAK,CAAC,kBAAkB,CAAC,KAC9B,IAuGF,CAAC;AAIF,yEAAyE;AACzE,eAAO,MAAM,oBAAoB,cAAc,CAAC;AAEhD,sDAAsD;AACtD,MAAM,WAAW,0BAA0B;IAC1C,iDAAiD;IACjD,eAAe,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAChC,oFAAoF;IACpF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC;CACpD;AAED,yDAAyD;AACzD,MAAM,WAAW,sBAAsB;IACtC,GAAG,EAAE,IAAI,CAAC;IACV,aAAa,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,eAAe,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IAC1C,qBAAqB,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IAChD,2BAA2B,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IACtD,0BAA0B,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;CACrD;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gCAAgC,GAC5C,UAAU,0BAA0B,KAClC,sBA4DF,CAAC"}
|
|
@@ -28,7 +28,7 @@ import { REQUEST_CONTEXT_KEY } from '../auth/request_context.js';
|
|
|
28
28
|
import { RESOLVED_AUTH_KEY, TEST_CONTEXT_PRESET, TEST_CONTEXT_PRESET_KEY } from '../hono_context.js';
|
|
29
29
|
import { get_resolved_auth } from '../auth/resolved_auth.js';
|
|
30
30
|
import { create_test_resolved_auth } from './resolved_auth.js';
|
|
31
|
-
import {
|
|
31
|
+
import { parse_any } from '../schema_issues.js';
|
|
32
32
|
// Mock the query modules so test cases can control return values.
|
|
33
33
|
// vi.mock() is hoisted by vitest, so these run before any imports resolve.
|
|
34
34
|
vi.mock('../auth/api_token_queries.js', () => ({
|
|
@@ -175,9 +175,10 @@ export const describe_bearer_auth_cases = (suite_name, cases) => {
|
|
|
175
175
|
else {
|
|
176
176
|
assert.strictEqual(res.status, tc.expected_status);
|
|
177
177
|
if (tc.expected_error) {
|
|
178
|
-
assert.strictEqual(body.error, tc.expected_error);
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
assert.strictEqual(body.error, tc.expected_error.code);
|
|
179
|
+
assert.strictEqual(res.status, tc.expected_error.status);
|
|
180
|
+
const parsed = parse_any(tc.expected_error.schema, body);
|
|
181
|
+
assert.ok(parsed.ok, `error body fails its schema: ${JSON.stringify(body)}`);
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
184
|
if (tc.validate_expectation === 'not_called') {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import './assert_dev_env.js';
|
|
2
|
+
import type { RouteMethod, RouteSpec } from '../http/route_spec.js';
|
|
3
|
+
import { type RouteErrorMap } from '../http/route_errors.js';
|
|
4
|
+
import { type RestErrorDef } from '../http/error_schemas.js';
|
|
5
|
+
import type { ErrorCoverageCollector } from './error_coverage.js';
|
|
6
|
+
/** The `Origin` the origin probe sends — no allowlist names it. */
|
|
7
|
+
export declare const GATE_PROBE_FOREIGN_ORIGIN = "https://gate-probe.invalid";
|
|
8
|
+
/** The `Host` the host probe sends — neither loopback nor named by an allowed origin. */
|
|
9
|
+
export declare const GATE_PROBE_FOREIGN_HOST = "gate-probe.invalid";
|
|
10
|
+
/** The `Content-Length` the unreadable-length probe sends. */
|
|
11
|
+
export declare const GATE_PROBE_UNREADABLE_LENGTH = "not-a-length";
|
|
12
|
+
/** One probe: a request head a route's declared gate must refuse with `expected`. */
|
|
13
|
+
export interface MiddlewareGateProbe {
|
|
14
|
+
readonly method: RouteMethod;
|
|
15
|
+
/** The route's path, a template with no params among the routes probed. */
|
|
16
|
+
readonly path: string;
|
|
17
|
+
/** The refusal the route's error map declares for the gate. */
|
|
18
|
+
readonly expected: RestErrorDef;
|
|
19
|
+
/** Request headers beyond `Host` and `Content-Length`. */
|
|
20
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
21
|
+
/** The `Host` to send, `null` for the backend's own. */
|
|
22
|
+
readonly host: string | null;
|
|
23
|
+
/** The `Content-Length` to declare; no body byte is sent. */
|
|
24
|
+
readonly content_length: number | string;
|
|
25
|
+
}
|
|
26
|
+
/** Options for `plan_middleware_gate_probes`. */
|
|
27
|
+
export interface MiddlewareGateProbePlanOptions {
|
|
28
|
+
/** The routes to probe, each a path with no params. */
|
|
29
|
+
readonly route_specs: ReadonlyArray<RouteSpec>;
|
|
30
|
+
/** Each route's error map, middleware included (`AppSurfaceSpec.route_error_maps`). */
|
|
31
|
+
readonly route_error_maps: ReadonlyMap<string, RouteErrorMap> | null;
|
|
32
|
+
/**
|
|
33
|
+
* The backend's base URL, or `null` in-process. Without a socket the
|
|
34
|
+
* unreadable-length probe is planned too.
|
|
35
|
+
*/
|
|
36
|
+
readonly base_url: string | null;
|
|
37
|
+
/**
|
|
38
|
+
* The request-body cap on every route but `rpc_path`, in bytes.
|
|
39
|
+
*
|
|
40
|
+
* @default SPINE_BODY_LIMIT_DEFAULT_BYTES
|
|
41
|
+
*/
|
|
42
|
+
readonly max_body_bytes?: number;
|
|
43
|
+
/** The RPC endpoint's path, whose cap is `rpc_max_body_bytes`. */
|
|
44
|
+
readonly rpc_path: string;
|
|
45
|
+
/**
|
|
46
|
+
* The request-body cap on `rpc_path`, in bytes.
|
|
47
|
+
*
|
|
48
|
+
* @default max_body_bytes
|
|
49
|
+
*/
|
|
50
|
+
readonly rpc_max_body_bytes?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Plan the gate probes of each route: one per gate its error map declares,
|
|
54
|
+
* in route order.
|
|
55
|
+
*
|
|
56
|
+
* @param options - the routes, their maps, the socket and the caps
|
|
57
|
+
* @returns the probes, none for a gate a route's map doesn't declare
|
|
58
|
+
* @throws Error when a route's path has a param or a wildcard segment, which a
|
|
59
|
+
* probe would send as the literal template
|
|
60
|
+
*/
|
|
61
|
+
export declare const plan_middleware_gate_probes: (options: MiddlewareGateProbePlanOptions) => Array<MiddlewareGateProbe>;
|
|
62
|
+
/** A probe's name: the route and the refusal it expects. */
|
|
63
|
+
export declare const format_middleware_gate_probe: (probe: MiddlewareGateProbe) => string;
|
|
64
|
+
/** The in-process transport a probe is sent through. */
|
|
65
|
+
export type MiddlewareGateProbeTransport = (url: string, init: RequestInit) => Promise<Response>;
|
|
66
|
+
/**
|
|
67
|
+
* Send a probe: over the socket when there is one, else through `transport`.
|
|
68
|
+
*
|
|
69
|
+
* @param probe - the probe
|
|
70
|
+
* @param base_url - the backend's base URL, or `null` in-process
|
|
71
|
+
* @param transport - the in-process transport, read only without a `base_url`
|
|
72
|
+
* @returns the response, its body JSON when the server sent JSON
|
|
73
|
+
* @throws Error when an unreadable length is to go over a socket, or no
|
|
74
|
+
* transport is given in-process
|
|
75
|
+
*/
|
|
76
|
+
export declare const send_middleware_gate_probe: (probe: MiddlewareGateProbe, base_url: string | null, transport: MiddlewareGateProbeTransport | null) => Promise<Response>;
|
|
77
|
+
/**
|
|
78
|
+
* Assert a probe was refused as its route declares, then record the refusal
|
|
79
|
+
* in `collector`, which checks the body against the route's map.
|
|
80
|
+
*
|
|
81
|
+
* @param probe - the probe sent
|
|
82
|
+
* @param response - its response
|
|
83
|
+
* @param collector - the error coverage to record the refusal in
|
|
84
|
+
* @param route_specs - the routes the collector resolves the path against
|
|
85
|
+
* @mutates collector - records the probe's route, status and code
|
|
86
|
+
* @throws AssertionError when the response's status or `error` code isn't the
|
|
87
|
+
* declared refusal, or its body is off the route's map
|
|
88
|
+
*/
|
|
89
|
+
export declare const assert_middleware_gate_refusal: (probe: MiddlewareGateProbe, response: Response, collector: ErrorCoverageCollector, route_specs: Array<RouteSpec>) => Promise<void>;
|
|
90
|
+
//# sourceMappingURL=middleware_gate_probes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware_gate_probes.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/middleware_gate_probes.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAkC7B,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAuB,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAElF,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAKlE,mEAAmE;AACnE,eAAO,MAAM,yBAAyB,+BAA+B,CAAC;AAEtE,yFAAyF;AACzF,eAAO,MAAM,uBAAuB,uBAAuB,CAAC;AAE5D,8DAA8D;AAC9D,eAAO,MAAM,4BAA4B,iBAAiB,CAAC;AAE3D,qFAAqF;AACrF,MAAM,WAAW,mBAAmB;IACnC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,2EAA2E;IAC3E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnD,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,6DAA6D;IAC7D,QAAQ,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAAC;CACzC;AAED,iDAAiD;AACjD,MAAM,WAAW,8BAA8B;IAC9C,uDAAuD;IACvD,QAAQ,CAAC,WAAW,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), a `Host` the server doesn't answer to
|
|
5
|
+
* (403 `forbidden_host`, the Rust spine's host gate), a declared
|
|
6
|
+
* `Content-Length` one byte over the route's cap (413 `payload_too_large`),
|
|
7
|
+
* and a `Content-Length` that isn't a length (400 `invalid_content_length`).
|
|
8
|
+
*
|
|
9
|
+
* The map drives which probes a route gets: a gate the route's map doesn't
|
|
10
|
+
* declare — a carved-out path, a backend without the layer, a disabled limit —
|
|
11
|
+
* is not probed. Every probe is a request head with no body and no
|
|
12
|
+
* credential, so the gate answers it before auth, rate limiting or body
|
|
13
|
+
* parsing, and it leaves no state behind.
|
|
14
|
+
*
|
|
15
|
+
* Over a socket (`base_url`) each probe is written raw
|
|
16
|
+
* (`raw_http_send_head`): `fetch` replaces a caller's `Host` and
|
|
17
|
+
* `Content-Length` with its own. In-process the head goes through the
|
|
18
|
+
* fixture's transport, whose `Request` keeps both headers as given. The
|
|
19
|
+
* unreadable-length probe runs in-process only: over a socket the runtime's
|
|
20
|
+
* HTTP parser (Node's, hyper) refuses such a length with a bodiless 400 of its
|
|
21
|
+
* own before the application sees it, and `fetch` can't send one. In-process
|
|
22
|
+
* the length reaches the body limit unparsed, and the 400 is the limit's own —
|
|
23
|
+
* the answer a Deno or Bun server gives when a request repeats an equal
|
|
24
|
+
* `Content-Length`, which those runtimes hand over joined into one value.
|
|
25
|
+
*
|
|
26
|
+
* `describe_standard_integration_tests` runs one per declared gate of each
|
|
27
|
+
* route it counts and records each answer in its error coverage.
|
|
28
|
+
*
|
|
29
|
+
* @module
|
|
30
|
+
*/
|
|
31
|
+
import { assert } from 'vitest';
|
|
32
|
+
import { get_route_error_map } from '../http/route_errors.js';
|
|
33
|
+
import { split_route_template } from '../http/route_error_check.js';
|
|
34
|
+
import { REST_FORBIDDEN_HOST, REST_FORBIDDEN_ORIGIN, REST_INVALID_CONTENT_LENGTH, REST_PAYLOAD_TOO_LARGE } from '../http/error_schemas.js';
|
|
35
|
+
import { to_route_content_type } from './schema_generators.js';
|
|
36
|
+
import { raw_http_send_head } from './transports/raw_http.js';
|
|
37
|
+
import { SPINE_BODY_LIMIT_DEFAULT_BYTES } from './cross_backend/spine_surface_constants.js';
|
|
38
|
+
/** The `Origin` the origin probe sends — no allowlist names it. */
|
|
39
|
+
export const GATE_PROBE_FOREIGN_ORIGIN = 'https://gate-probe.invalid';
|
|
40
|
+
/** The `Host` the host probe sends — neither loopback nor named by an allowed origin. */
|
|
41
|
+
export const GATE_PROBE_FOREIGN_HOST = 'gate-probe.invalid';
|
|
42
|
+
/** The `Content-Length` the unreadable-length probe sends. */
|
|
43
|
+
export const GATE_PROBE_UNREADABLE_LENGTH = 'not-a-length';
|
|
44
|
+
/** Whether `defs` declares `def`'s status and code. */
|
|
45
|
+
const declares = (defs, def) => defs.some((d) => d.status === def.status && d.code === def.code);
|
|
46
|
+
/**
|
|
47
|
+
* Plan the gate probes of each route: one per gate its error map declares,
|
|
48
|
+
* in route order.
|
|
49
|
+
*
|
|
50
|
+
* @param options - the routes, their maps, the socket and the caps
|
|
51
|
+
* @returns the probes, none for a gate a route's map doesn't declare
|
|
52
|
+
* @throws Error when a route's path has a param or a wildcard segment, which a
|
|
53
|
+
* probe would send as the literal template
|
|
54
|
+
*/
|
|
55
|
+
export const plan_middleware_gate_probes = (options) => {
|
|
56
|
+
const max_body_bytes = options.max_body_bytes ?? SPINE_BODY_LIMIT_DEFAULT_BYTES;
|
|
57
|
+
const rpc_max_body_bytes = options.rpc_max_body_bytes ?? max_body_bytes;
|
|
58
|
+
const probes = [];
|
|
59
|
+
for (const spec of options.route_specs) {
|
|
60
|
+
const { defs } = get_route_error_map(options.route_error_maps, spec);
|
|
61
|
+
const { method, path } = spec;
|
|
62
|
+
if (split_route_template(path).some((segment) => segment.startsWith(':') || segment === '*')) {
|
|
63
|
+
throw new Error(`plan_middleware_gate_probes: ${method} ${path} has a param or wildcard segment, ` +
|
|
64
|
+
'which a probe would send as the literal template');
|
|
65
|
+
}
|
|
66
|
+
// a `POST` refuses anything but its content type with 415, so each probe
|
|
67
|
+
// sends it and the gate is the only refusal left
|
|
68
|
+
const headers = method === 'POST' ? { 'content-type': to_route_content_type(spec) } : {};
|
|
69
|
+
const cap = path === options.rpc_path ? rpc_max_body_bytes : max_body_bytes;
|
|
70
|
+
if (declares(defs, REST_FORBIDDEN_ORIGIN)) {
|
|
71
|
+
probes.push({
|
|
72
|
+
method,
|
|
73
|
+
path,
|
|
74
|
+
expected: REST_FORBIDDEN_ORIGIN,
|
|
75
|
+
headers: { ...headers, origin: GATE_PROBE_FOREIGN_ORIGIN },
|
|
76
|
+
host: null,
|
|
77
|
+
content_length: 0
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (declares(defs, REST_FORBIDDEN_HOST)) {
|
|
81
|
+
probes.push({
|
|
82
|
+
method,
|
|
83
|
+
path,
|
|
84
|
+
expected: REST_FORBIDDEN_HOST,
|
|
85
|
+
headers,
|
|
86
|
+
host: GATE_PROBE_FOREIGN_HOST,
|
|
87
|
+
content_length: 0
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (declares(defs, REST_PAYLOAD_TOO_LARGE)) {
|
|
91
|
+
probes.push({
|
|
92
|
+
method,
|
|
93
|
+
path,
|
|
94
|
+
expected: REST_PAYLOAD_TOO_LARGE,
|
|
95
|
+
headers,
|
|
96
|
+
host: null,
|
|
97
|
+
content_length: cap + 1
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (options.base_url === null && declares(defs, REST_INVALID_CONTENT_LENGTH)) {
|
|
101
|
+
probes.push({
|
|
102
|
+
method,
|
|
103
|
+
path,
|
|
104
|
+
expected: REST_INVALID_CONTENT_LENGTH,
|
|
105
|
+
headers,
|
|
106
|
+
host: null,
|
|
107
|
+
content_length: GATE_PROBE_UNREADABLE_LENGTH
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return probes;
|
|
112
|
+
};
|
|
113
|
+
/** A probe's name: the route and the refusal it expects. */
|
|
114
|
+
export const format_middleware_gate_probe = (probe) => `${probe.method} ${probe.path} → ${probe.expected.status} ${probe.expected.code}`;
|
|
115
|
+
/**
|
|
116
|
+
* Send a probe: over the socket when there is one, else through `transport`.
|
|
117
|
+
*
|
|
118
|
+
* @param probe - the probe
|
|
119
|
+
* @param base_url - the backend's base URL, or `null` in-process
|
|
120
|
+
* @param transport - the in-process transport, read only without a `base_url`
|
|
121
|
+
* @returns the response, its body JSON when the server sent JSON
|
|
122
|
+
* @throws Error when an unreadable length is to go over a socket, or no
|
|
123
|
+
* transport is given in-process
|
|
124
|
+
*/
|
|
125
|
+
export const send_middleware_gate_probe = async (probe, base_url, transport) => {
|
|
126
|
+
if (base_url !== null) {
|
|
127
|
+
if (typeof probe.content_length !== 'number') {
|
|
128
|
+
throw new Error(`${format_middleware_gate_probe(probe)}: an unreadable length can't be sent over a socket`);
|
|
129
|
+
}
|
|
130
|
+
const raw = await raw_http_send_head(base_url, {
|
|
131
|
+
method: probe.method,
|
|
132
|
+
path: probe.path,
|
|
133
|
+
headers: probe.headers,
|
|
134
|
+
content_length: probe.content_length,
|
|
135
|
+
...(probe.host !== null && { host: probe.host })
|
|
136
|
+
});
|
|
137
|
+
const content_type = raw.headers['content-type'];
|
|
138
|
+
return new Response(raw.body === '' ? null : raw.body, {
|
|
139
|
+
status: raw.status,
|
|
140
|
+
headers: content_type === undefined ? {} : { 'content-type': content_type }
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (!transport) {
|
|
144
|
+
throw new Error(`${format_middleware_gate_probe(probe)}: no transport to send it in-process`);
|
|
145
|
+
}
|
|
146
|
+
return transport(probe.path, {
|
|
147
|
+
method: probe.method,
|
|
148
|
+
headers: {
|
|
149
|
+
...probe.headers,
|
|
150
|
+
...(probe.host !== null && { host: probe.host }),
|
|
151
|
+
'content-length': String(probe.content_length)
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Assert a probe was refused as its route declares, then record the refusal
|
|
157
|
+
* in `collector`, which checks the body against the route's map.
|
|
158
|
+
*
|
|
159
|
+
* @param probe - the probe sent
|
|
160
|
+
* @param response - its response
|
|
161
|
+
* @param collector - the error coverage to record the refusal in
|
|
162
|
+
* @param route_specs - the routes the collector resolves the path against
|
|
163
|
+
* @mutates collector - records the probe's route, status and code
|
|
164
|
+
* @throws AssertionError when the response's status or `error` code isn't the
|
|
165
|
+
* declared refusal, or its body is off the route's map
|
|
166
|
+
*/
|
|
167
|
+
export const assert_middleware_gate_refusal = async (probe, response, collector, route_specs) => {
|
|
168
|
+
const label = format_middleware_gate_probe(probe);
|
|
169
|
+
let body;
|
|
170
|
+
try {
|
|
171
|
+
body = await response.clone().json();
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// not JSON — the status or code check below names it
|
|
175
|
+
}
|
|
176
|
+
assert.strictEqual(response.status, probe.expected.status, `${label}: answered ${response.status} ${JSON.stringify(body)}`);
|
|
177
|
+
assert.strictEqual(body?.error, probe.expected.code, `${label}: answered ${JSON.stringify(body)}`);
|
|
178
|
+
await collector.assert_and_record(route_specs, probe.method, probe.path, response);
|
|
179
|
+
};
|
|
@@ -41,7 +41,7 @@ export interface RateLimitingTestOptions {
|
|
|
41
41
|
*
|
|
42
42
|
* Creates 2 test groups:
|
|
43
43
|
* 1. IP rate limiting on login — fires `max_attempts + 1` login requests,
|
|
44
|
-
* verifies the last returns 429 with a valid `
|
|
44
|
+
* verifies the last returns 429 with a valid `REST_RATE_LIMIT_EXCEEDED` body.
|
|
45
45
|
* 2. Per-account rate limiting on login — fires `max_attempts + 1` login
|
|
46
46
|
* requests with the same username, verifies the last returns 429.
|
|
47
47
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate_limiting.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/rate_limiting.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"rate_limiting.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/rate_limiting.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AA2B7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAIvD,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAIN,KAAK,SAAS,EACd,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAIhE;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACvC,4CAA4C;IAC5C,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,wDAAwD;IACxD,kBAAkB,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,KAAK,CAAC,SAAS,CAAC,CAAC;IAChE,iDAAiD;IACjD,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;OAGG;IACH,YAAY,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAChC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,uBAAuB,CAAC;CACxC;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,4BAA4B,GAAI,SAAS,uBAAuB,KAAG,IA2J/E,CAAC"}
|
|
@@ -21,8 +21,9 @@ import './assert_dev_env.js';
|
|
|
21
21
|
* @module
|
|
22
22
|
*/
|
|
23
23
|
import { describe, test, assert } from 'vitest';
|
|
24
|
+
import { parse_or_throw } from '@fuzdev/fuz_schema/schema.ts';
|
|
24
25
|
import { RateLimiter } from '../rate_limiter.js';
|
|
25
|
-
import {
|
|
26
|
+
import { REST_RATE_LIMIT_EXCEEDED } from '../http/error_schemas.js';
|
|
26
27
|
import { AUTH_MIGRATION_NS } from '../auth/migrations.js';
|
|
27
28
|
import { create_test_app } from './app_server.js';
|
|
28
29
|
import { create_test_request_headers } from './test_origin.js';
|
|
@@ -34,7 +35,7 @@ import { run_migrations } from '../db/migrate.js';
|
|
|
34
35
|
*
|
|
35
36
|
* Creates 2 test groups:
|
|
36
37
|
* 1. IP rate limiting on login — fires `max_attempts + 1` login requests,
|
|
37
|
-
* verifies the last returns 429 with a valid `
|
|
38
|
+
* verifies the last returns 429 with a valid `REST_RATE_LIMIT_EXCEEDED` body.
|
|
38
39
|
* 2. Per-account rate limiting on login — fires `max_attempts + 1` login
|
|
39
40
|
* requests with the same username, verifies the last returns 429.
|
|
40
41
|
*
|
|
@@ -94,7 +95,7 @@ export const describe_rate_limiting_tests = (options) => {
|
|
|
94
95
|
});
|
|
95
96
|
assert.strictEqual(blocked_res.status, 429);
|
|
96
97
|
const body = await blocked_res.json();
|
|
97
|
-
|
|
98
|
+
parse_or_throw(REST_RATE_LIMIT_EXCEEDED.schema, body);
|
|
98
99
|
assert.ok(typeof body.retry_after === 'number' && body.retry_after > 0, 'Expected positive retry_after');
|
|
99
100
|
assert_rate_limit_retry_after_header(blocked_res, body);
|
|
100
101
|
}
|
|
@@ -143,7 +144,7 @@ export const describe_rate_limiting_tests = (options) => {
|
|
|
143
144
|
});
|
|
144
145
|
assert.strictEqual(blocked_res.status, 429);
|
|
145
146
|
const body = await blocked_res.json();
|
|
146
|
-
|
|
147
|
+
parse_or_throw(REST_RATE_LIMIT_EXCEEDED.schema, body);
|
|
147
148
|
assert.ok(typeof body.retry_after === 'number' && body.retry_after > 0, 'Expected positive retry_after');
|
|
148
149
|
assert_rate_limit_retry_after_header(blocked_res, body);
|
|
149
150
|
// A different username should NOT be rate limited
|