@segmentstream/ontology 0.126.0 → 0.128.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/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.128.0
4
+
5
+ A webhook whose provider lets the caller choose the signing secret can be signed and registered
6
+ from code: `signature: { scheme, generated: true, … }` takes the place of `secret`, and
7
+ `webhookSigningSecret(trigger)`, from `@segmentstream/ontology`, returns the secret to register.
8
+ SegmentStream derives it from the workspace's webhook secret and the webhook's name, so it needs
9
+ no storing and survives deploys; the ontology service checks deliveries with it. Like a token URL,
10
+ it is for the provider only.
11
+
12
+ ## 0.127.0
13
+
14
+ A `find` MCP tool's `where` takes `{ "$me": [] }`, as a role scope writes `me`, for the caller's
15
+ user ID. Compared with a to-one relationship, bare or with `eq`, `ne` or `in`, it reads the
16
+ relationship's `<relationship>_id` key, so `{ "reporter": { "$me": [] } }` needs no `user:read`;
17
+ anywhere else it is the ID itself, as in `{ "created_by": { "$me": [] } }`. A path from it stays a
18
+ role scope's and is refused. The feedback guidance now finds the member's own items with it in one
19
+ call instead of reading their ID from `me` first, in the ontology's MCP guidance and the agent's
20
+ platform instructions.
21
+
3
22
  ## 0.126.0
4
23
 
5
24
  A webhook trigger checks any provider's HMAC signature: `signature: { scheme: "hmac", secret,
@@ -118,7 +118,7 @@ export const AppFeedback = entity("app_feedback", {
118
118
  acknowledge: action({
119
119
  description: "Record that the member has been told the builders' response to their own feedback, passing " +
120
120
  "the responded_at of the response you reported. When they ask about their feedback, find " +
121
- "their own items (reporter is their user id from me), tell them what was done, then call " +
121
+ 'their own items (where {"reporter": {"$me": []}}), tell them what was done, then call ' +
122
122
  "this. " +
123
123
  "acknowledged is false when there is no such response, as when the builders answered again " +
124
124
  "meanwhile: read the item again and report the newer response.",
package/dist/index.d.ts CHANGED
@@ -31,7 +31,7 @@ export { isMe, isMeMarker, MAX_ME_HOPS, me, ME_KEY, MeMarker, mePathOf, meToWire
31
31
  export type { MeReference } from "./me.js";
32
32
  export { check, defineOntology } from "./ontology.js";
33
33
  export { webhookDeliveryJsonSchema } from "./analyze.js";
34
- export { webhookToken, webhookTokenMatches, webhookUrl, WebhookUrlUnavailable, WEBHOOK_ORIGIN_VARIABLE, WEBHOOK_TOKEN_CONTEXT, } from "./webhook-token.js";
34
+ export { webhookSigningKey, webhookSigningSecret, webhookToken, webhookTokenMatches, webhookUrl, WebhookUrlUnavailable, WEBHOOK_ORIGIN_VARIABLE, WEBHOOK_SIGNING_CONTEXT, WEBHOOK_TOKEN_CONTEXT, } from "./webhook-token.js";
35
35
  export type { Ontology } from "./ontology.js";
36
36
  export { AGENT_TOOL_VERBS, agentToolPermission, agentToolsOf, DEFAULT_MEMBER_PERMISSIONS, effectiveScope, generatedActionPermissions, grants, isAllowed, parsePermission, permissionsOf, principalScopes, READ_VERB, readScope, rolesGranting, scopesOf, USER_IDENTITY_VERBS, USER_MEMBER_VERBS, WILDCARD, WILDCARD_ENTITY_VERBS, WORKSPACE_SOURCE_PERMISSION, } from "./permissions.js";
37
37
  export type { AgentToolVerb, ParsedPermission, PermissionHolder, RolePermissions, ScopedPermission, ScopeFilter, ScopeValue, ScopeWhere, UserMemberVerb, } from "./permissions.js";
package/dist/index.js CHANGED
@@ -18,7 +18,7 @@ export { SYSTEM_PROPERTIES } from "./names.js";
18
18
  export { isMe, isMeMarker, MAX_ME_HOPS, me, ME_KEY, MeMarker, mePathOf, meToWire, MeTypeId, } from "./me.js";
19
19
  export { check, defineOntology } from "./ontology.js";
20
20
  export { webhookDeliveryJsonSchema } from "./analyze.js";
21
- export { webhookToken, webhookTokenMatches, webhookUrl, WebhookUrlUnavailable, WEBHOOK_ORIGIN_VARIABLE, WEBHOOK_TOKEN_CONTEXT, } from "./webhook-token.js";
21
+ export { webhookSigningKey, webhookSigningSecret, webhookToken, webhookTokenMatches, webhookUrl, WebhookUrlUnavailable, WEBHOOK_ORIGIN_VARIABLE, WEBHOOK_SIGNING_CONTEXT, WEBHOOK_TOKEN_CONTEXT, } from "./webhook-token.js";
22
22
  export { AGENT_TOOL_VERBS, agentToolPermission, agentToolsOf, DEFAULT_MEMBER_PERMISSIONS, effectiveScope, generatedActionPermissions, grants, isAllowed, parsePermission, permissionsOf, principalScopes, READ_VERB, readScope, rolesGranting, scopesOf, USER_IDENTITY_VERBS, USER_MEMBER_VERBS, WILDCARD, WILDCARD_ENTITY_VERBS, WORKSPACE_SOURCE_PERMISSION, } from "./permissions.js";
23
23
  export { p, PROPERTY_TYPES, propertyDefinition } from "./property.js";
24
24
  export { ON_DELETE, rel } from "./relationship.js";
@@ -95,7 +95,9 @@ import { type ResolvedGuest, type ResolvedSignInOptions, type SignInPolicy } fro
95
95
  * agent. Older readers ignore it, register none and let none be taken over.
96
96
  * Within version 15, a webhook trigger's `signature` may have scheme `hmac` and carry the provider's
97
97
  * format, written only when declared. Older readers present the scheme and ignore the format; an
98
- * older publication check refuses the scheme.
98
+ * older publication check refuses the scheme. A signature may carry `generated: true` instead of
99
+ * `secret`: the platform derives the webhook's secret. Older readers find no secret name; an older
100
+ * publication check refuses it.
99
101
  */
100
102
  export declare const MANIFEST_VERSION = 15;
101
103
  /** The phrase used for a direction the author did not phrase. */
@@ -479,7 +481,10 @@ export declare const WebhookTriggerManifest: Schema.Struct<{
479
481
  readonly description: Schema.String;
480
482
  readonly signature: Schema.optionalKey<Schema.Struct<{
481
483
  readonly scheme: Schema.String;
482
- readonly secret: Schema.String;
484
+ /** The workspace secret's name; absent when the secret is `generated`. */
485
+ readonly secret: Schema.optionalKey<Schema.String>;
486
+ /** The platform derives the secret for the webhook; workspace code registers it. */
487
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
483
488
  readonly algorithm: Schema.optionalKey<Schema.String>;
484
489
  readonly encoding: Schema.optionalKey<Schema.String>;
485
490
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -547,7 +552,10 @@ export declare const WorkflowTriggerManifest: Schema.Union<readonly [Schema.Stru
547
552
  readonly description: Schema.String;
548
553
  readonly signature: Schema.optionalKey<Schema.Struct<{
549
554
  readonly scheme: Schema.String;
550
- readonly secret: Schema.String;
555
+ /** The workspace secret's name; absent when the secret is `generated`. */
556
+ readonly secret: Schema.optionalKey<Schema.String>;
557
+ /** The platform derives the secret for the webhook; workspace code registers it. */
558
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
551
559
  readonly algorithm: Schema.optionalKey<Schema.String>;
552
560
  readonly encoding: Schema.optionalKey<Schema.String>;
553
561
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -622,7 +630,10 @@ export declare const WorkflowManifest: Schema.Struct<{
622
630
  readonly description: Schema.String;
623
631
  readonly signature: Schema.optionalKey<Schema.Struct<{
624
632
  readonly scheme: Schema.String;
625
- readonly secret: Schema.String;
633
+ /** The workspace secret's name; absent when the secret is `generated`. */
634
+ readonly secret: Schema.optionalKey<Schema.String>;
635
+ /** The platform derives the secret for the webhook; workspace code registers it. */
636
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
626
637
  readonly algorithm: Schema.optionalKey<Schema.String>;
627
638
  readonly encoding: Schema.optionalKey<Schema.String>;
628
639
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -1326,7 +1337,10 @@ export declare const OntologyManifestWire: Schema.encodeKeys<Schema.Struct<{
1326
1337
  readonly description: Schema.String;
1327
1338
  readonly signature: Schema.optionalKey<Schema.Struct<{
1328
1339
  readonly scheme: Schema.String;
1329
- readonly secret: Schema.String;
1340
+ /** The workspace secret's name; absent when the secret is `generated`. */
1341
+ readonly secret: Schema.optionalKey<Schema.String>;
1342
+ /** The platform derives the secret for the webhook; workspace code registers it. */
1343
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
1330
1344
  readonly algorithm: Schema.optionalKey<Schema.String>;
1331
1345
  readonly encoding: Schema.optionalKey<Schema.String>;
1332
1346
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -1603,7 +1617,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1603
1617
  readonly headers?: readonly string[] | undefined;
1604
1618
  readonly signature?: {
1605
1619
  readonly scheme: string;
1606
- readonly secret: string;
1607
1620
  readonly message?: readonly (string | {
1608
1621
  readonly body: true;
1609
1622
  } | {
@@ -1621,6 +1634,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1621
1634
  readonly tolerance?: number | undefined;
1622
1635
  readonly header?: string | undefined;
1623
1636
  } | undefined;
1637
+ readonly generated?: true | undefined;
1624
1638
  readonly algorithm?: string | undefined;
1625
1639
  readonly encoding?: string | undefined;
1626
1640
  readonly signatures?: {
@@ -1632,6 +1646,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1632
1646
  } | undefined;
1633
1647
  readonly prefix?: string | undefined;
1634
1648
  } | undefined;
1649
+ readonly secret?: string | undefined;
1635
1650
  } | undefined;
1636
1651
  readonly verification?: {
1637
1652
  readonly scheme: string;
@@ -1951,7 +1966,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1951
1966
  readonly headers?: readonly string[] | undefined;
1952
1967
  readonly signature?: {
1953
1968
  readonly scheme: string;
1954
- readonly secret: string;
1955
1969
  readonly message?: readonly (string | {
1956
1970
  readonly body: true;
1957
1971
  } | {
@@ -1969,6 +1983,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1969
1983
  readonly tolerance?: number | undefined;
1970
1984
  readonly header?: string | undefined;
1971
1985
  } | undefined;
1986
+ readonly generated?: true | undefined;
1972
1987
  readonly algorithm?: string | undefined;
1973
1988
  readonly encoding?: string | undefined;
1974
1989
  readonly signatures?: {
@@ -1980,6 +1995,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1980
1995
  } | undefined;
1981
1996
  readonly prefix?: string | undefined;
1982
1997
  } | undefined;
1998
+ readonly secret?: string | undefined;
1983
1999
  } | undefined;
1984
2000
  readonly verification?: {
1985
2001
  readonly scheme: string;
@@ -2301,7 +2317,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2301
2317
  readonly headers?: readonly string[] | undefined;
2302
2318
  readonly signature?: {
2303
2319
  readonly scheme: string;
2304
- readonly secret: string;
2305
2320
  readonly message?: readonly (string | {
2306
2321
  readonly body: true;
2307
2322
  } | {
@@ -2319,6 +2334,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2319
2334
  readonly tolerance?: number | undefined;
2320
2335
  readonly header?: string | undefined;
2321
2336
  } | undefined;
2337
+ readonly generated?: true | undefined;
2322
2338
  readonly algorithm?: string | undefined;
2323
2339
  readonly encoding?: string | undefined;
2324
2340
  readonly signatures?: {
@@ -2330,6 +2346,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2330
2346
  } | undefined;
2331
2347
  readonly prefix?: string | undefined;
2332
2348
  } | undefined;
2349
+ readonly secret?: string | undefined;
2333
2350
  } | undefined;
2334
2351
  readonly verification?: {
2335
2352
  readonly scheme: string;
@@ -2649,7 +2666,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2649
2666
  readonly headers?: readonly string[] | undefined;
2650
2667
  readonly signature?: {
2651
2668
  readonly scheme: string;
2652
- readonly secret: string;
2653
2669
  readonly message?: readonly (string | {
2654
2670
  readonly body: true;
2655
2671
  } | {
@@ -2667,6 +2683,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2667
2683
  readonly tolerance?: number | undefined;
2668
2684
  readonly header?: string | undefined;
2669
2685
  } | undefined;
2686
+ readonly generated?: true | undefined;
2670
2687
  readonly algorithm?: string | undefined;
2671
2688
  readonly encoding?: string | undefined;
2672
2689
  readonly signatures?: {
@@ -2678,6 +2695,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2678
2695
  } | undefined;
2679
2696
  readonly prefix?: string | undefined;
2680
2697
  } | undefined;
2698
+ readonly secret?: string | undefined;
2681
2699
  } | undefined;
2682
2700
  readonly verification?: {
2683
2701
  readonly scheme: string;
@@ -3195,7 +3213,10 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3195
3213
  readonly description: Schema.String;
3196
3214
  readonly signature: Schema.optionalKey<Schema.Struct<{
3197
3215
  readonly scheme: Schema.String;
3198
- readonly secret: Schema.String;
3216
+ /** The workspace secret's name; absent when the secret is `generated`. */
3217
+ readonly secret: Schema.optionalKey<Schema.String>;
3218
+ /** The platform derives the secret for the webhook; workspace code registers it. */
3219
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
3199
3220
  readonly algorithm: Schema.optionalKey<Schema.String>;
3200
3221
  readonly encoding: Schema.optionalKey<Schema.String>;
3201
3222
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -3659,7 +3680,10 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3659
3680
  readonly description: Schema.String;
3660
3681
  readonly signature: Schema.optionalKey<Schema.Struct<{
3661
3682
  readonly scheme: Schema.String;
3662
- readonly secret: Schema.String;
3683
+ /** The workspace secret's name; absent when the secret is `generated`. */
3684
+ readonly secret: Schema.optionalKey<Schema.String>;
3685
+ /** The platform derives the secret for the webhook; workspace code registers it. */
3686
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
3663
3687
  readonly algorithm: Schema.optionalKey<Schema.String>;
3664
3688
  readonly encoding: Schema.optionalKey<Schema.String>;
3665
3689
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -3928,7 +3952,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3928
3952
  readonly headers?: readonly string[] | undefined;
3929
3953
  readonly signature?: {
3930
3954
  readonly scheme: string;
3931
- readonly secret: string;
3932
3955
  readonly message?: readonly (string | {
3933
3956
  readonly body: true;
3934
3957
  } | {
@@ -3946,6 +3969,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3946
3969
  readonly tolerance?: number | undefined;
3947
3970
  readonly header?: string | undefined;
3948
3971
  } | undefined;
3972
+ readonly generated?: true | undefined;
3949
3973
  readonly algorithm?: string | undefined;
3950
3974
  readonly encoding?: string | undefined;
3951
3975
  readonly signatures?: {
@@ -3957,6 +3981,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3957
3981
  } | undefined;
3958
3982
  readonly prefix?: string | undefined;
3959
3983
  } | undefined;
3984
+ readonly secret?: string | undefined;
3960
3985
  } | undefined;
3961
3986
  readonly verification?: {
3962
3987
  readonly scheme: string;
@@ -4276,7 +4301,6 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4276
4301
  readonly headers?: readonly string[] | undefined;
4277
4302
  readonly signature?: {
4278
4303
  readonly scheme: string;
4279
- readonly secret: string;
4280
4304
  readonly message?: readonly (string | {
4281
4305
  readonly body: true;
4282
4306
  } | {
@@ -4294,6 +4318,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4294
4318
  readonly tolerance?: number | undefined;
4295
4319
  readonly header?: string | undefined;
4296
4320
  } | undefined;
4321
+ readonly generated?: true | undefined;
4297
4322
  readonly algorithm?: string | undefined;
4298
4323
  readonly encoding?: string | undefined;
4299
4324
  readonly signatures?: {
@@ -4305,6 +4330,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4305
4330
  } | undefined;
4306
4331
  readonly prefix?: string | undefined;
4307
4332
  } | undefined;
4333
+ readonly secret?: string | undefined;
4308
4334
  } | undefined;
4309
4335
  readonly verification?: {
4310
4336
  readonly scheme: string;
@@ -4630,7 +4656,6 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
4630
4656
  readonly headers?: readonly string[] | undefined;
4631
4657
  readonly signature?: {
4632
4658
  readonly scheme: string;
4633
- readonly secret: string;
4634
4659
  readonly message?: readonly (string | {
4635
4660
  readonly body: true;
4636
4661
  } | {
@@ -4648,6 +4673,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
4648
4673
  readonly tolerance?: number | undefined;
4649
4674
  readonly header?: string | undefined;
4650
4675
  } | undefined;
4676
+ readonly generated?: true | undefined;
4651
4677
  readonly algorithm?: string | undefined;
4652
4678
  readonly encoding?: string | undefined;
4653
4679
  readonly signatures?: {
@@ -4659,6 +4685,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
4659
4685
  } | undefined;
4660
4686
  readonly prefix?: string | undefined;
4661
4687
  } | undefined;
4688
+ readonly secret?: string | undefined;
4662
4689
  } | undefined;
4663
4690
  readonly verification?: {
4664
4691
  readonly scheme: string;
@@ -4978,7 +5005,6 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
4978
5005
  readonly headers?: readonly string[] | undefined;
4979
5006
  readonly signature?: {
4980
5007
  readonly scheme: string;
4981
- readonly secret: string;
4982
5008
  readonly message?: readonly (string | {
4983
5009
  readonly body: true;
4984
5010
  } | {
@@ -4996,6 +5022,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
4996
5022
  readonly tolerance?: number | undefined;
4997
5023
  readonly header?: string | undefined;
4998
5024
  } | undefined;
5025
+ readonly generated?: true | undefined;
4999
5026
  readonly algorithm?: string | undefined;
5000
5027
  readonly encoding?: string | undefined;
5001
5028
  readonly signatures?: {
@@ -5007,6 +5034,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
5007
5034
  } | undefined;
5008
5035
  readonly prefix?: string | undefined;
5009
5036
  } | undefined;
5037
+ readonly secret?: string | undefined;
5010
5038
  } | undefined;
5011
5039
  readonly verification?: {
5012
5040
  readonly scheme: string;
package/dist/manifest.js CHANGED
@@ -96,7 +96,9 @@ import { CHANNEL_PROVIDERS, DEFAULT_SIGN_IN_POLICY, isGuestBotCheckScheme, isGue
96
96
  * agent. Older readers ignore it, register none and let none be taken over.
97
97
  * Within version 15, a webhook trigger's `signature` may have scheme `hmac` and carry the provider's
98
98
  * format, written only when declared. Older readers present the scheme and ignore the format; an
99
- * older publication check refuses the scheme.
99
+ * older publication check refuses the scheme. A signature may carry `generated: true` instead of
100
+ * `secret`: the platform derives the webhook's secret. Older readers find no secret name; an older
101
+ * publication check refuses it.
100
102
  */
101
103
  export const MANIFEST_VERSION = 15;
102
104
  /** The first version whose documents use snake_case names on the wire. */
@@ -350,7 +352,10 @@ export const WebhookTriggerManifest = Schema.Struct({
350
352
  description: Schema.String,
351
353
  signature: Schema.optionalKey(Schema.Struct({
352
354
  scheme: Schema.String,
353
- secret: Schema.String,
355
+ /** The workspace secret's name; absent when the secret is `generated`. */
356
+ secret: Schema.optionalKey(Schema.String),
357
+ /** The platform derives the secret for the webhook; workspace code registers it. */
358
+ generated: Schema.optionalKey(Schema.Literal(true)),
354
359
  algorithm: Schema.optionalKey(Schema.String),
355
360
  encoding: Schema.optionalKey(Schema.String),
356
361
  key: Schema.optionalKey(Schema.Struct({ decode: Schema.String, prefix: Schema.optionalKey(Schema.String) })),
@@ -448,7 +448,6 @@ export declare const MigrationPlan: Schema.Struct<{
448
448
  readonly headers?: readonly string[] | undefined;
449
449
  readonly signature?: {
450
450
  readonly scheme: string;
451
- readonly secret: string;
452
451
  readonly message?: readonly (string | {
453
452
  readonly body: true;
454
453
  } | {
@@ -466,6 +465,7 @@ export declare const MigrationPlan: Schema.Struct<{
466
465
  readonly tolerance?: number | undefined;
467
466
  readonly header?: string | undefined;
468
467
  } | undefined;
468
+ readonly generated?: true | undefined;
469
469
  readonly algorithm?: string | undefined;
470
470
  readonly encoding?: string | undefined;
471
471
  readonly signatures?: {
@@ -477,6 +477,7 @@ export declare const MigrationPlan: Schema.Struct<{
477
477
  } | undefined;
478
478
  readonly prefix?: string | undefined;
479
479
  } | undefined;
480
+ readonly secret?: string | undefined;
480
481
  } | undefined;
481
482
  readonly verification?: {
482
483
  readonly scheme: string;
@@ -796,7 +797,6 @@ export declare const MigrationPlan: Schema.Struct<{
796
797
  readonly headers?: readonly string[] | undefined;
797
798
  readonly signature?: {
798
799
  readonly scheme: string;
799
- readonly secret: string;
800
800
  readonly message?: readonly (string | {
801
801
  readonly body: true;
802
802
  } | {
@@ -814,6 +814,7 @@ export declare const MigrationPlan: Schema.Struct<{
814
814
  readonly tolerance?: number | undefined;
815
815
  readonly header?: string | undefined;
816
816
  } | undefined;
817
+ readonly generated?: true | undefined;
817
818
  readonly algorithm?: string | undefined;
818
819
  readonly encoding?: string | undefined;
819
820
  readonly signatures?: {
@@ -825,6 +826,7 @@ export declare const MigrationPlan: Schema.Struct<{
825
826
  } | undefined;
826
827
  readonly prefix?: string | undefined;
827
828
  } | undefined;
829
+ readonly secret?: string | undefined;
828
830
  } | undefined;
829
831
  readonly verification?: {
830
832
  readonly scheme: string;
@@ -1146,7 +1148,6 @@ export declare const MigrationPlan: Schema.Struct<{
1146
1148
  readonly headers?: readonly string[] | undefined;
1147
1149
  readonly signature?: {
1148
1150
  readonly scheme: string;
1149
- readonly secret: string;
1150
1151
  readonly message?: readonly (string | {
1151
1152
  readonly body: true;
1152
1153
  } | {
@@ -1164,6 +1165,7 @@ export declare const MigrationPlan: Schema.Struct<{
1164
1165
  readonly tolerance?: number | undefined;
1165
1166
  readonly header?: string | undefined;
1166
1167
  } | undefined;
1168
+ readonly generated?: true | undefined;
1167
1169
  readonly algorithm?: string | undefined;
1168
1170
  readonly encoding?: string | undefined;
1169
1171
  readonly signatures?: {
@@ -1175,6 +1177,7 @@ export declare const MigrationPlan: Schema.Struct<{
1175
1177
  } | undefined;
1176
1178
  readonly prefix?: string | undefined;
1177
1179
  } | undefined;
1180
+ readonly secret?: string | undefined;
1178
1181
  } | undefined;
1179
1182
  readonly verification?: {
1180
1183
  readonly scheme: string;
@@ -1494,7 +1497,6 @@ export declare const MigrationPlan: Schema.Struct<{
1494
1497
  readonly headers?: readonly string[] | undefined;
1495
1498
  readonly signature?: {
1496
1499
  readonly scheme: string;
1497
- readonly secret: string;
1498
1500
  readonly message?: readonly (string | {
1499
1501
  readonly body: true;
1500
1502
  } | {
@@ -1512,6 +1514,7 @@ export declare const MigrationPlan: Schema.Struct<{
1512
1514
  readonly tolerance?: number | undefined;
1513
1515
  readonly header?: string | undefined;
1514
1516
  } | undefined;
1517
+ readonly generated?: true | undefined;
1515
1518
  readonly algorithm?: string | undefined;
1516
1519
  readonly encoding?: string | undefined;
1517
1520
  readonly signatures?: {
@@ -1523,6 +1526,7 @@ export declare const MigrationPlan: Schema.Struct<{
1523
1526
  } | undefined;
1524
1527
  readonly prefix?: string | undefined;
1525
1528
  } | undefined;
1529
+ readonly secret?: string | undefined;
1526
1530
  } | undefined;
1527
1531
  readonly verification?: {
1528
1532
  readonly scheme: string;
@@ -1957,7 +1961,8 @@ export declare const MigrationPlan: Schema.Struct<{
1957
1961
  readonly description: Schema.String;
1958
1962
  readonly signature: Schema.optionalKey<Schema.Struct<{
1959
1963
  readonly scheme: Schema.String;
1960
- readonly secret: Schema.String;
1964
+ readonly secret: Schema.optionalKey<Schema.String>;
1965
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
1961
1966
  readonly algorithm: Schema.optionalKey<Schema.String>;
1962
1967
  readonly encoding: Schema.optionalKey<Schema.String>;
1963
1968
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -2301,7 +2306,8 @@ export declare const MigrationPlan: Schema.Struct<{
2301
2306
  readonly description: Schema.String;
2302
2307
  readonly signature: Schema.optionalKey<Schema.Struct<{
2303
2308
  readonly scheme: Schema.String;
2304
- readonly secret: Schema.String;
2309
+ readonly secret: Schema.optionalKey<Schema.String>;
2310
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
2305
2311
  readonly algorithm: Schema.optionalKey<Schema.String>;
2306
2312
  readonly encoding: Schema.optionalKey<Schema.String>;
2307
2313
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -2541,7 +2547,6 @@ export declare const MigrationPlan: Schema.Struct<{
2541
2547
  readonly headers?: readonly string[] | undefined;
2542
2548
  readonly signature?: {
2543
2549
  readonly scheme: string;
2544
- readonly secret: string;
2545
2550
  readonly message?: readonly (string | {
2546
2551
  readonly body: true;
2547
2552
  } | {
@@ -2559,6 +2564,7 @@ export declare const MigrationPlan: Schema.Struct<{
2559
2564
  readonly tolerance?: number | undefined;
2560
2565
  readonly header?: string | undefined;
2561
2566
  } | undefined;
2567
+ readonly generated?: true | undefined;
2562
2568
  readonly algorithm?: string | undefined;
2563
2569
  readonly encoding?: string | undefined;
2564
2570
  readonly signatures?: {
@@ -2570,6 +2576,7 @@ export declare const MigrationPlan: Schema.Struct<{
2570
2576
  } | undefined;
2571
2577
  readonly prefix?: string | undefined;
2572
2578
  } | undefined;
2579
+ readonly secret?: string | undefined;
2573
2580
  } | undefined;
2574
2581
  readonly verification?: {
2575
2582
  readonly scheme: string;
@@ -2889,7 +2896,6 @@ export declare const MigrationPlan: Schema.Struct<{
2889
2896
  readonly headers?: readonly string[] | undefined;
2890
2897
  readonly signature?: {
2891
2898
  readonly scheme: string;
2892
- readonly secret: string;
2893
2899
  readonly message?: readonly (string | {
2894
2900
  readonly body: true;
2895
2901
  } | {
@@ -2907,6 +2913,7 @@ export declare const MigrationPlan: Schema.Struct<{
2907
2913
  readonly tolerance?: number | undefined;
2908
2914
  readonly header?: string | undefined;
2909
2915
  } | undefined;
2916
+ readonly generated?: true | undefined;
2910
2917
  readonly algorithm?: string | undefined;
2911
2918
  readonly encoding?: string | undefined;
2912
2919
  readonly signatures?: {
@@ -2918,6 +2925,7 @@ export declare const MigrationPlan: Schema.Struct<{
2918
2925
  } | undefined;
2919
2926
  readonly prefix?: string | undefined;
2920
2927
  } | undefined;
2928
+ readonly secret?: string | undefined;
2921
2929
  } | undefined;
2922
2930
  readonly verification?: {
2923
2931
  readonly scheme: string;
@@ -0,0 +1,19 @@
1
+ import type { OntologyManifest } from "../manifest.js";
2
+ /**
3
+ * `{ "$me": [] }` in the `where` of a find, as a role scope writes the caller: the caller's user id.
4
+ * A to-one relationship compared with it, bare or with `eq`, `ne` or `in`, reads its
5
+ * `<relationship>_id` key (`{ "reporter": { "$me": [] } }` is `{ "reporter_id": "<id>" }`), so the
6
+ * filter reads no `user` record and needs no `user:read`. Any other value it stands in is the id
7
+ * itself, such as `{ "created_by": { "$me": [] } }`. A path from it is a role scope's only: it is
8
+ * refused, since a find compares values, not the sets a path reaches.
9
+ *
10
+ * Runs on the arguments as sent, before the query schema decodes them, and returns a `where`
11
+ * without markers unchanged.
12
+ */
13
+ export declare function resolveCallerInWhere(manifest: OntologyManifest, entity: string, where: unknown, callerId: string): {
14
+ readonly where: unknown;
15
+ } | {
16
+ readonly refused: ReadonlyArray<string | number>;
17
+ };
18
+ /** The message of a refused path from `me` in a find. */
19
+ export declare const CALLER_PATH_REFUSED = "A find compares with { \"$me\": [] }, the caller's user id; paths from it are for role scopes.";
@@ -0,0 +1,126 @@
1
+ import { mePathOf } from "../me.js";
2
+ import { FILTER_OPERATORS, findEntityManifest, MANY_QUANTIFIERS, queryFields, queryLinks, } from "../query.js";
3
+ /**
4
+ * `{ "$me": [] }` in the `where` of a find, as a role scope writes the caller: the caller's user id.
5
+ * A to-one relationship compared with it, bare or with `eq`, `ne` or `in`, reads its
6
+ * `<relationship>_id` key (`{ "reporter": { "$me": [] } }` is `{ "reporter_id": "<id>" }`), so the
7
+ * filter reads no `user` record and needs no `user:read`. Any other value it stands in is the id
8
+ * itself, such as `{ "created_by": { "$me": [] } }`. A path from it is a role scope's only: it is
9
+ * refused, since a find compares values, not the sets a path reaches.
10
+ *
11
+ * Runs on the arguments as sent, before the query schema decodes them, and returns a `where`
12
+ * without markers unchanged.
13
+ */
14
+ export function resolveCallerInWhere(manifest, entity, where, callerId) {
15
+ if (!holdsMarker(where)) {
16
+ return { where };
17
+ }
18
+ try {
19
+ return { where: resolveWhere(manifest, entity, where, callerId, ["where"]) };
20
+ }
21
+ catch (error) {
22
+ if (error instanceof PathRefused) {
23
+ return { refused: error.path };
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+ /** The message of a refused path from `me` in a find. */
29
+ export const CALLER_PATH_REFUSED = 'A find compares with { "$me": [] }, the caller\'s user id; paths from it are for role scopes.';
30
+ class PathRefused extends Error {
31
+ path;
32
+ constructor(path) {
33
+ super(CALLER_PATH_REFUSED);
34
+ this.path = path;
35
+ }
36
+ }
37
+ const OPERATORS = new Set(FILTER_OPERATORS);
38
+ const QUANTIFIERS = new Set(MANY_QUANTIFIERS);
39
+ function isPlainObject(value) {
40
+ return typeof value === "object" && value !== null && !Array.isArray(value);
41
+ }
42
+ function holdsMarker(value) {
43
+ if (mePathOf(value) !== undefined) {
44
+ return true;
45
+ }
46
+ if (Array.isArray(value)) {
47
+ return value.some(holdsMarker);
48
+ }
49
+ return isPlainObject(value) && Object.values(value).some(holdsMarker);
50
+ }
51
+ /** A bare `me` or an object of operators comparing with it: what a key field takes. */
52
+ function comparesCaller(condition) {
53
+ if (mePathOf(condition) !== undefined) {
54
+ return true;
55
+ }
56
+ return (isPlainObject(condition) &&
57
+ Object.keys(condition).length > 0 &&
58
+ Object.keys(condition).every((key) => OPERATORS.has(key)) &&
59
+ holdsMarker(condition));
60
+ }
61
+ function resolveValue(value, callerId, path) {
62
+ const hops = mePathOf(value);
63
+ if (hops !== undefined) {
64
+ if (hops.length > 0) {
65
+ throw new PathRefused(path);
66
+ }
67
+ return callerId;
68
+ }
69
+ if (Array.isArray(value)) {
70
+ return value.map((item, index) => resolveValue(item, callerId, [...path, index]));
71
+ }
72
+ if (isPlainObject(value)) {
73
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
74
+ key,
75
+ resolveValue(item, callerId, [...path, key]),
76
+ ]));
77
+ }
78
+ return value;
79
+ }
80
+ function resolveWhere(manifest, entityName, where, callerId, path) {
81
+ const entity = findEntityManifest(manifest, entityName);
82
+ if (entity === undefined || !isPlainObject(where) || mePathOf(where) !== undefined) {
83
+ return where;
84
+ }
85
+ const keys = new Set(queryFields(entity).map((field) => field.name));
86
+ const links = new Map(queryLinks(manifest, entityName, true).map((link) => [link.name, link]));
87
+ const entries = [];
88
+ for (const [key, condition] of Object.entries(where)) {
89
+ const at = [...path, key];
90
+ const link = links.get(key);
91
+ if ((key === "and" || key === "or") && Array.isArray(condition)) {
92
+ entries.push([
93
+ key,
94
+ condition.map((sub, index) => resolveWhere(manifest, entityName, sub, callerId, [...at, index])),
95
+ ]);
96
+ }
97
+ else if (key === "not") {
98
+ entries.push([key, resolveWhere(manifest, entityName, condition, callerId, at)]);
99
+ }
100
+ else if (link === undefined || !holdsMarker(condition)) {
101
+ entries.push([key, resolveValue(condition, callerId, at)]);
102
+ }
103
+ else if (link.cardinality === "one" && keys.has(`${key}_id`) && comparesCaller(condition)) {
104
+ entries.push([`${key}_id`, resolveValue(condition, callerId, at)]);
105
+ }
106
+ else if (link.cardinality === "many" &&
107
+ isPlainObject(condition) &&
108
+ Object.keys(condition).every((quantifier) => QUANTIFIERS.has(quantifier))) {
109
+ entries.push([
110
+ key,
111
+ Object.fromEntries(Object.entries(condition).map(([quantifier, sub]) => [
112
+ quantifier,
113
+ resolveWhere(manifest, link.target, sub, callerId, [...at, quantifier]),
114
+ ])),
115
+ ]);
116
+ }
117
+ else {
118
+ entries.push([key, resolveWhere(manifest, link.target, condition, callerId, at)]);
119
+ }
120
+ }
121
+ const names = entries.map(([key]) => key);
122
+ // `{ reporter: me, reporter_id: … }` names the key twice: both conditions must hold.
123
+ return new Set(names).size === names.length
124
+ ? Object.fromEntries(entries)
125
+ : { and: entries.map(([key, condition]) => ({ [key]: condition })) };
126
+ }
@@ -52,7 +52,8 @@ export const FIND_CONDITIONS = "where: a field takes a bare value (eq), null (is
52
52
  "dates and timestamps also gt, gte, lt, lte and between [from, to], which includes both ends. " +
53
53
  "ne also matches empty values. Dates are YYYY-MM-DD. A relationship takes conditions on its " +
54
54
  "target's fields, one level deep; a to-many one wraps them in some, none or every. and and or " +
55
- "take lists of conditions; not takes one. Unknown fields and operators are refused.";
55
+ 'take lists of conditions; not takes one. {"$me": []} is the caller\'s user id, also for a ' +
56
+ 'to-one relationship: {"owner": {"$me": []}}. Unknown fields and operators are refused.';
56
57
  // Clients that shorten schema descriptions keep this first sentence.
57
58
  const WHERE_DESCRIPTION = "Operators: eq, ne, in, is_null; text contains, starts_with; ordered gt, gte, lt, lte, " +
58
59
  "between. All conditions must hold; the tool description has the rules.";
@@ -8,6 +8,7 @@ import { encodeManifest } from "../../manifest.js";
8
8
  import { describeError, SCOPE_MISMATCH } from "../metric-query-platform.js";
9
9
  import { gatewayCredential, gatewayDecisions } from "../decisions.js";
10
10
  import { CurrentPrincipal, withPrincipal } from "../authentication.js";
11
+ import { CALLER_PATH_REFUSED, resolveCallerInWhere } from "../caller-where.js";
11
12
  import { currentCaller } from "../caller-context.js";
12
13
  import { requireRuntimeOperation, runtimeManifest } from "./runtime-policy.js";
13
14
  import { CONFIRMATION_PREVIEW_METHOD, previewLocale, withoutConfirmation } from "./confirmation.js";
@@ -427,7 +428,13 @@ function runTool(server, tool, args, request, preview) {
427
428
  return structured(yield* chats.history(yield* decodeArguments(ChatHistory, args), identityToken(request)));
428
429
  case "find": {
429
430
  const schemas = built.schemas.get(target.planned.name);
430
- const query = (yield* decodeArguments(schemas.query, args));
431
+ const resolved = resolveCallerInWhere(built.plan.ontology.manifest, target.planned.name, args.where, (yield* CurrentPrincipal).id);
432
+ if ("refused" in resolved) {
433
+ return yield* new InvalidArguments({
434
+ issues: [{ path: resolved.refused, message: CALLER_PATH_REFUSED }],
435
+ });
436
+ }
437
+ const query = (yield* decodeArguments(schemas.query, args.where === undefined ? args : { ...args, where: resolved.where }));
431
438
  const page = yield* operations.find(target.planned, toQuery(query));
432
439
  return structured(yield* encoded(schemas.findResult, page));
433
440
  }
@@ -627,8 +634,8 @@ export const FEEDBACK_INSTRUCTIONS = "Feedback: this app accepts feedback for it
627
634
  "something works badly or asks for something new. Do not offer what you can do yourself; offer " +
628
635
  "once per problem. Before submitting, look for the user's open app_feedback with the same " +
629
636
  "subject. Submit with app_feedback_submit only after the user agrees to the title and summary you " +
630
- "show in their language. When the user asks about their feedback, read only their own: filter " +
631
- "app_feedback by reporter, their user id from me, since builders can read everyone's. Tell " +
637
+ "show in their language. When the user asks about their feedback, read only their own, since " +
638
+ 'builders can read everyone\'s: app_feedback_find with where {"reporter": {"$me": []}}. Tell ' +
632
639
  "them the builders' response, then call app_feedback_acknowledge with its responded_at.";
633
640
  export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested work, this workspace preauthorizes minimal technical failure reports. " +
634
641
  "Automatically record unexpected or persistent tool/connection failures through app_feedback_submit; " +
@@ -638,7 +645,7 @@ export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested
638
645
  "and never claim it was recorded before a successful submission or existing-item read. " +
639
646
  "Feature/change requests and sharing task contents or files still require the member's agreement " +
640
647
  "to the title and summary. Asked about their feedback, read only the caller's own items " +
641
- "(reporter is their user id from me) and acknowledge only the response you relayed.";
648
+ '(where {"reporter": {"$me": []}}) and acknowledge only the response you relayed.';
642
649
  /**
643
650
  * Fixed guidance keeps initialization independent of ontology size: the feedback paragraph depends
644
651
  * only on whether the caller may run `app_feedback_submit`.
@@ -585,7 +585,8 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
585
585
  readonly description: Schema.String;
586
586
  readonly signature: Schema.optionalKey<Schema.Struct<{
587
587
  readonly scheme: Schema.String;
588
- readonly secret: Schema.String;
588
+ readonly secret: Schema.optionalKey<Schema.String>;
589
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
589
590
  readonly algorithm: Schema.optionalKey<Schema.String>;
590
591
  readonly encoding: Schema.optionalKey<Schema.String>;
591
592
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -942,7 +943,8 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
942
943
  readonly description: Schema.String;
943
944
  readonly signature: Schema.optionalKey<Schema.Struct<{
944
945
  readonly scheme: Schema.String;
945
- readonly secret: Schema.String;
946
+ readonly secret: Schema.optionalKey<Schema.String>;
947
+ readonly generated: Schema.optionalKey<Schema.Literal<true>>;
946
948
  readonly algorithm: Schema.optionalKey<Schema.String>;
947
949
  readonly encoding: Schema.optionalKey<Schema.String>;
948
950
  readonly key: Schema.optionalKey<Schema.Struct<{
@@ -107,6 +107,18 @@ export declare function hmacSignature(format: WebhookHmacFormat): SignatureSchem
107
107
  export declare const stripeSignature: SignatureScheme;
108
108
  /** Meta signs exactly the raw body; its app secret stays in the backend. */
109
109
  export declare const metaSignature: SignatureScheme;
110
+ /**
111
+ * Whether a delivery to a webhook with a generated secret carries a signature of it. The service
112
+ * holds the workspace's webhook secret, which the generated secret derives from, so it computes
113
+ * the HMAC itself and compares each candidate in constant time.
114
+ */
115
+ export declare function generatedSignatureMatches(input: {
116
+ readonly webhookSecret: string;
117
+ readonly webhook: string;
118
+ readonly format: WebhookHmacFormat;
119
+ readonly message: Uint8Array;
120
+ readonly signatures: ReadonlyArray<string>;
121
+ }): boolean;
110
122
  /**
111
123
  * What checks a manifest signature: its format and that format's scheme, or `undefined` for a
112
124
  * scheme this SDK does not know.
@@ -1,5 +1,7 @@
1
+ import { createHmac, timingSafeEqual } from "node:crypto";
1
2
  import { Effect, Redacted } from "effect";
2
3
  import { META_SIGNATURE_FORMAT, STRIPE_SIGNATURE_FORMAT, WEBHOOK_HMAC_DIGEST_BYTES, WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS, webhookHmacFormat, } from "../webhooks.js";
4
+ import { webhookSigningKey } from "../webhook-token.js";
3
5
  import { fixedWindowLimiter } from "./rate-limit.js";
4
6
  /** How long the service waits for the backend's answer before answering 503. */
5
7
  export const WEBHOOK_VERIFY_TIMEOUT_MS = 3_000;
@@ -220,6 +222,20 @@ export function hmacSignature(format) {
220
222
  export const stripeSignature = hmacSignature(STRIPE_SIGNATURE_FORMAT);
221
223
  /** Meta signs exactly the raw body; its app secret stays in the backend. */
222
224
  export const metaSignature = hmacSignature(META_SIGNATURE_FORMAT);
225
+ /**
226
+ * Whether a delivery to a webhook with a generated secret carries a signature of it. The service
227
+ * holds the workspace's webhook secret, which the generated secret derives from, so it computes
228
+ * the HMAC itself and compares each candidate in constant time.
229
+ */
230
+ export function generatedSignatureMatches(input) {
231
+ const expected = createHmac(input.format.algorithm, webhookSigningKey(input.webhookSecret, input.webhook))
232
+ .update(input.message)
233
+ .digest();
234
+ return input.signatures.some((signature) => {
235
+ const received = Buffer.from(signature, input.format.encoding === "hex" ? "hex" : "base64");
236
+ return received.byteLength === expected.byteLength && timingSafeEqual(received, expected);
237
+ });
238
+ }
223
239
  /**
224
240
  * What checks a manifest signature: its format and that format's scheme, or `undefined` for a
225
241
  * scheme this SDK does not know.
@@ -15,8 +15,8 @@ import { unavailableFiles } from "../storage/file-access.js";
15
15
  import { ActionRejected, PermissionDenied, } from "../errors.js";
16
16
  import { isActionTrigger, isCronTrigger, isWebhookTrigger, } from "../manifest.js";
17
17
  import { webhookTokenMatches } from "../webhook-token.js";
18
- import { MAX_WEBHOOK_BODY_BYTES, MAX_META_WEBHOOK_BODY_BYTES, MAX_WEBHOOK_VERIFICATION_CHARS, WEBHOOK_BASE_PATH, webhookPath, } from "../webhooks.js";
19
- import { readWebhookBody, signatureCheck, signatureRefusals, utf8Text, } from "./webhooks.js";
18
+ import { MAX_WEBHOOK_BODY_BYTES, MAX_META_WEBHOOK_BODY_BYTES, MAX_WEBHOOK_VERIFICATION_CHARS, presentedWebhookSignature, WEBHOOK_BASE_PATH, webhookPath, } from "../webhooks.js";
19
+ import { generatedSignatureMatches, readWebhookBody, signatureCheck, signatureRefusals, utf8Text, } from "./webhooks.js";
20
20
  import { clientAddress } from "./rate-limit.js";
21
21
  import { isAllowed, workflowPermission } from "../permissions.js";
22
22
  import { OntologyActions, OntologyReads, OntologyWrites, } from "../runtime.js";
@@ -561,7 +561,9 @@ function workflowView(workflow, now) {
561
561
  description: trigger.description,
562
562
  // Never the token: whoever reads the declarations does not get the URL.
563
563
  path: webhookPath(trigger.name),
564
- signature: trigger.signature ?? null,
564
+ signature: trigger.signature === undefined
565
+ ? null
566
+ : presentedWebhookSignature(trigger.signature),
565
567
  headers: trigger.headers ?? [],
566
568
  connection: trigger.connection ?? null,
567
569
  },
@@ -1349,7 +1351,11 @@ export function workflowsLayer(options) {
1349
1351
  return yield* refuse("signature_throttled", 429, { "retry-after": String(wait) });
1350
1352
  }
1351
1353
  const checked = signatureCheck(signature);
1352
- if (checked === undefined || options.webhookVerifier === undefined) {
1354
+ const generated = signature.generated === true;
1355
+ if (checked === undefined ||
1356
+ (generated
1357
+ ? options.webhookSecret === undefined
1358
+ : options.webhookVerifier === undefined)) {
1353
1359
  return yield* refuse("scheme_unavailable", 503);
1354
1360
  }
1355
1361
  const { format } = checked;
@@ -1358,26 +1364,46 @@ export function workflowsLayer(options) {
1358
1364
  refusals.refused(client);
1359
1365
  return yield* refuse(content.reason, 401);
1360
1366
  }
1361
- const verifier = options.webhookVerifier;
1362
- const verified = yield* Effect.promise(() => verifier({
1363
- secret: signature.secret,
1364
- algorithm: format.algorithm,
1365
- encoding: format.encoding,
1366
- ...(format.key === undefined ? {} : { key: format.key }),
1367
- message: content.message,
1368
- signatures: content.signatures,
1369
- identityToken: requestIdentityToken(request),
1370
- requestId,
1371
- }));
1372
- if (verified.outcome === "unavailable") {
1373
- yield* Effect.logError("ontology.webhook.verification_unavailable", verified.cause).pipe(Effect.annotateLogs({ requestId, webhook: trigger.name, status: verified.status }));
1374
- return yield* refuse("verification_unavailable", 503);
1367
+ // The platform derives a generated secret from the workspace's webhook secret, which
1368
+ // this service holds, so it checks the signature itself.
1369
+ if (generated) {
1370
+ if (!generatedSignatureMatches({
1371
+ webhookSecret: Redacted.value(options.webhookSecret),
1372
+ webhook: trigger.name,
1373
+ format,
1374
+ message: content.message,
1375
+ signatures: content.signatures,
1376
+ })) {
1377
+ refusals.refused(client);
1378
+ return yield* refuse("signature_invalid", 401);
1379
+ }
1375
1380
  }
1376
- if (verified.outcome !== "valid") {
1377
- refusals.refused(client);
1378
- return yield* refuse(verified.outcome === "secret_missing"
1379
- ? "signature_secret_missing"
1380
- : "signature_invalid", 401);
1381
+ else {
1382
+ const verifier = options.webhookVerifier;
1383
+ const verified = yield* Effect.promise(() => verifier({
1384
+ secret: signature.secret ?? "",
1385
+ algorithm: format.algorithm,
1386
+ encoding: format.encoding,
1387
+ ...(format.key === undefined ? {} : { key: format.key }),
1388
+ message: content.message,
1389
+ signatures: content.signatures,
1390
+ identityToken: requestIdentityToken(request),
1391
+ requestId,
1392
+ }));
1393
+ if (verified.outcome === "unavailable") {
1394
+ yield* Effect.logError("ontology.webhook.verification_unavailable", verified.cause).pipe(Effect.annotateLogs({
1395
+ requestId,
1396
+ webhook: trigger.name,
1397
+ status: verified.status,
1398
+ }));
1399
+ return yield* refuse("verification_unavailable", 503);
1400
+ }
1401
+ if (verified.outcome !== "valid") {
1402
+ refusals.refused(client);
1403
+ return yield* refuse(verified.outcome === "secret_missing"
1404
+ ? "signature_secret_missing"
1405
+ : "signature_invalid", 401);
1406
+ }
1381
1407
  }
1382
1408
  }
1383
1409
  const text = utf8Text(body);
@@ -8,6 +8,14 @@ export declare const WEBHOOK_TOKEN_CONTEXT = "segmentstream.webhook.v1:";
8
8
  * no token is ever stored (`tests/fixtures/ontology-webhook-token.json`).
9
9
  */
10
10
  export declare function webhookToken(secret: string, name: string): string;
11
+ /** What a generated signing secret signs before the webhook's name, apart from its token. */
12
+ export declare const WEBHOOK_SIGNING_CONTEXT = "segmentstream.webhook-signature.v1:";
13
+ /**
14
+ * The signing secret the platform derives for a webhook that declares `generated: true`:
15
+ * `base64url(HMAC-SHA256(secret, context + name))`, 43 characters, from the workspace's webhook
16
+ * secret and the webhook's wire name. The provider signs deliveries with it as text.
17
+ */
18
+ export declare function webhookSigningKey(secret: string, name: string): string;
11
19
  /** Whether a received token is the webhook's, compared in constant time. */
12
20
  export declare function webhookTokenMatches(secret: string, name: string, received: string): boolean;
13
21
  /**
@@ -35,3 +43,14 @@ export declare class WebhookUrlUnavailable extends Error {
35
43
  * {@link WebhookUrlUnavailable} in previews and local runs, which receive no webhooks.
36
44
  */
37
45
  export declare function webhookUrl(trigger: WebhookTrigger, environment?: Readonly<Record<string, string | undefined>>): string;
46
+ /**
47
+ * The signing secret of a webhook that declares `signature: { generated: true, … }`, for workspace
48
+ * code to register with the provider through the webhook's connection, as BTCPay Server's or
49
+ * GitHub's webhook APIs take one. The ontology service checks deliveries with the same secret. It
50
+ * starts runs for anyone who has it, as a token URL does: send it only to the provider, never to
51
+ * records, logs or people.
52
+ *
53
+ * Call it in a step or an action's `prepare`. It throws {@link WebhookUrlUnavailable} in previews
54
+ * and local runs, and an error for a webhook whose secret is not generated.
55
+ */
56
+ export declare function webhookSigningSecret(trigger: WebhookTrigger, environment?: Readonly<Record<string, string | undefined>>): string;
@@ -12,6 +12,18 @@ export const WEBHOOK_TOKEN_CONTEXT = "segmentstream.webhook.v1:";
12
12
  export function webhookToken(secret, name) {
13
13
  return createHmac("sha256", secret).update(`${WEBHOOK_TOKEN_CONTEXT}${name}`).digest("base64url");
14
14
  }
15
+ /** What a generated signing secret signs before the webhook's name, apart from its token. */
16
+ export const WEBHOOK_SIGNING_CONTEXT = "segmentstream.webhook-signature.v1:";
17
+ /**
18
+ * The signing secret the platform derives for a webhook that declares `generated: true`:
19
+ * `base64url(HMAC-SHA256(secret, context + name))`, 43 characters, from the workspace's webhook
20
+ * secret and the webhook's wire name. The provider signs deliveries with it as text.
21
+ */
22
+ export function webhookSigningKey(secret, name) {
23
+ return createHmac("sha256", secret)
24
+ .update(`${WEBHOOK_SIGNING_CONTEXT}${name}`)
25
+ .digest("base64url");
26
+ }
15
27
  /** Whether a received token is the webhook's, compared in constant time. */
16
28
  export function webhookTokenMatches(secret, name, received) {
17
29
  const expected = Buffer.from(webhookToken(secret, name));
@@ -63,3 +75,25 @@ export function webhookUrl(trigger, environment = process.env) {
63
75
  }
64
76
  return `${url}/${webhookToken(secret, wire)}`;
65
77
  }
78
+ /**
79
+ * The signing secret of a webhook that declares `signature: { generated: true, … }`, for workspace
80
+ * code to register with the provider through the webhook's connection, as BTCPay Server's or
81
+ * GitHub's webhook APIs take one. The ontology service checks deliveries with the same secret. It
82
+ * starts runs for anyone who has it, as a token URL does: send it only to the provider, never to
83
+ * records, logs or people.
84
+ *
85
+ * Call it in a step or an action's `prepare`. It throws {@link WebhookUrlUnavailable} in previews
86
+ * and local runs, and an error for a webhook whose secret is not generated.
87
+ */
88
+ export function webhookSigningSecret(trigger, environment = process.env) {
89
+ const wire = nameToWire(trigger.name);
90
+ if (trigger.signature?.generated !== true) {
91
+ throw new Error(`The webhook ${wire} does not declare signature.generated, so the platform keeps no secret ` +
92
+ "for it to register.");
93
+ }
94
+ const secret = environment[WEBHOOK_SECRET_VARIABLE];
95
+ if (!secret) {
96
+ throw new WebhookUrlUnavailable(wire);
97
+ }
98
+ return webhookSigningKey(secret, wire);
99
+ }
@@ -88,6 +88,26 @@ export declare const META_SIGNATURE_FORMAT: WebhookHmacFormat;
88
88
  export declare function webhookHmacFormat(signature: {
89
89
  readonly scheme: string;
90
90
  }): WebhookHmacFormat | undefined;
91
+ /**
92
+ * Where a signature's secret comes from: a workspace secret by `secret` name, or with
93
+ * `generated: true` the secret the platform derives for the webhook, which workspace code reads
94
+ * with `webhookSigningSecret(trigger)` to register it.
95
+ */
96
+ export interface WebhookSignatureSource {
97
+ readonly scheme: string;
98
+ readonly secret?: string | undefined;
99
+ readonly generated?: true | undefined;
100
+ }
101
+ /**
102
+ * A signature as listings present it: its scheme and the secret's name, which reads `generated`
103
+ * with `generated: true` for a secret the platform derives, so readers that know only names still
104
+ * show something true.
105
+ */
106
+ export declare function presentedWebhookSignature(signature: WebhookSignatureSource): {
107
+ readonly scheme: string;
108
+ readonly secret: string;
109
+ readonly generated?: true;
110
+ };
91
111
  /** A problem with a signature declaration: the key it concerns, under `signature`, and why. */
92
112
  export interface WebhookSignatureProblem {
93
113
  readonly path: string;
@@ -103,13 +123,7 @@ export declare function webhookSignatureProblems(signature: unknown): ReadonlyAr
103
123
  * A valid signature declaration as the manifest records it: `{ scheme, secret }` for a named
104
124
  * scheme, and for `hmac` its format's settings, each written only when declared.
105
125
  */
106
- export declare function webhookSignatureManifest(signature: {
107
- readonly scheme: string;
108
- readonly secret: string;
109
- } & Partial<WebhookHmacFormat>): {
110
- readonly scheme: string;
111
- readonly secret: string;
112
- } & Partial<WebhookHmacFormat>;
126
+ export declare function webhookSignatureManifest(signature: WebhookSignatureSource & Partial<WebhookHmacFormat>): WebhookSignatureSource & Partial<WebhookHmacFormat>;
113
127
  /** The most webhook triggers one ontology may declare across its workflows. */
114
128
  export declare const MAX_WEBHOOKS = 20;
115
129
  /** The most request headers one webhook trigger may pass to its workflow. */
package/dist/webhooks.js CHANGED
@@ -61,6 +61,16 @@ export function webhookHmacFormat(signature) {
61
61
  return undefined;
62
62
  }
63
63
  }
64
+ /**
65
+ * A signature as listings present it: its scheme and the secret's name, which reads `generated`
66
+ * with `generated: true` for a secret the platform derives, so readers that know only names still
67
+ * show something true.
68
+ */
69
+ export function presentedWebhookSignature(signature) {
70
+ return signature.generated === true
71
+ ? { scheme: signature.scheme, secret: "generated", generated: true }
72
+ : { scheme: signature.scheme, secret: signature.secret ?? "" };
73
+ }
64
74
  const FORMAT_KEYS = ["algorithm", "encoding", "key", "signatures", "timestamp", "message"];
65
75
  const PRINTABLE = /^[\x21-\x7e]{1,16}$/;
66
76
  const ENTRY_NAME = /^[A-Za-z0-9_.-]{1,16}$/;
@@ -82,25 +92,38 @@ export function webhookSignatureProblems(signature) {
82
92
  report("", "signature is { scheme, secret }, or omitted for a provider that signs nothing.");
83
93
  return problems;
84
94
  }
85
- const { scheme, secret } = signature;
95
+ const { scheme, secret, generated } = signature;
86
96
  if (!WEBHOOK_SIGNATURE_SCHEMES.includes(scheme)) {
87
97
  report("scheme", `${JSON.stringify(scheme) ?? "undefined"} is not a signature scheme; use one of ` +
88
98
  `${WEBHOOK_SIGNATURE_SCHEMES.join(", ")}, or omit signature for a provider that signs ` +
89
99
  "nothing.");
90
100
  }
91
- if (typeof secret !== "string" || !WORKSPACE_SECRET_NAME.test(secret)) {
101
+ if (generated !== undefined) {
102
+ if (generated !== true) {
103
+ report("generated", "generated is true, or omitted with a secret named instead.");
104
+ }
105
+ if (secret !== undefined) {
106
+ report("secret", "A signature names a secret or is generated: not both.");
107
+ }
108
+ if (signature.key !== undefined) {
109
+ report("key", "A generated secret is plain text: key, which decodes an issued secret, does not apply.");
110
+ }
111
+ }
112
+ else if (typeof secret !== "string" || !WORKSPACE_SECRET_NAME.test(secret)) {
92
113
  report("secret", `${JSON.stringify(secret) ?? "undefined"} is not a workspace secret name; name the ` +
93
- "secret that holds the provider's signing secret, such as stripe-webhook-signing-secret.");
114
+ "secret that holds the provider's signing secret, such as stripe-webhook-signing-secret, " +
115
+ "or set generated: true for a secret workspace code registers.");
94
116
  }
117
+ const named = ["scheme", "secret", "generated"];
95
118
  if (scheme !== "hmac") {
96
- for (const key of unknownKeys(signature, ["scheme", "secret"])) {
119
+ for (const key of unknownKeys(signature, named)) {
97
120
  report(key, FORMAT_KEYS.includes(key)
98
121
  ? `${key} declares a format, which only scheme "hmac" takes.`
99
122
  : `${key} is not a signature setting.`);
100
123
  }
101
124
  return problems;
102
125
  }
103
- for (const key of unknownKeys(signature, ["scheme", "secret", ...FORMAT_KEYS])) {
126
+ for (const key of unknownKeys(signature, [...named, ...FORMAT_KEYS])) {
104
127
  report(key, `${key} is not a signature setting.`);
105
128
  }
106
129
  if (!WEBHOOK_HMAC_ALGORITHMS.includes(signature.algorithm)) {
@@ -248,13 +271,14 @@ function signedHeaderProblem(name) {
248
271
  * scheme, and for `hmac` its format's settings, each written only when declared.
249
272
  */
250
273
  export function webhookSignatureManifest(signature) {
274
+ const source = signature.generated === true ? { generated: true } : { secret: signature.secret };
251
275
  if (signature.scheme !== "hmac") {
252
- return { scheme: signature.scheme, secret: signature.secret };
276
+ return { scheme: signature.scheme, ...source };
253
277
  }
254
278
  const { signatures, timestamp, key, message } = signature;
255
279
  return {
256
280
  scheme: "hmac",
257
- secret: signature.secret,
281
+ ...source,
258
282
  algorithm: signature.algorithm,
259
283
  encoding: signature.encoding,
260
284
  ...(key === undefined
@@ -126,15 +126,24 @@ export interface ActionTrigger<Name extends string = string, Input = unknown> {
126
126
  * that holds its signing secret. Without one, the webhook's URL ends in a token. `stripe` and `meta`
127
127
  * name published formats; `hmac` declares any other HMAC format with the trigger.
128
128
  */
129
- export type WebhookSignature = {
129
+ export type WebhookSignature = ({
130
130
  readonly scheme: Exclude<WebhookSignatureScheme, "hmac">;
131
- /** A workspace secret's name, such as `stripe-webhook-signing-secret`; its value stays in the platform. */
132
- readonly secret: string;
133
- } | ({
131
+ } & WebhookSignatureSecret) | ({
134
132
  readonly scheme: "hmac";
135
- /** A workspace secret's name, such as `btcpay-webhook-secret`; its value stays in the platform. */
133
+ } & WebhookSignatureSecret & WebhookHmacFormat);
134
+ /**
135
+ * The secret a signature is checked with: a workspace secret by name, whose value stays in the
136
+ * platform, or `generated: true`, the secret the platform derives for the webhook, which workspace
137
+ * code reads with `webhookSigningSecret(trigger)` to register it with the provider.
138
+ */
139
+ export type WebhookSignatureSecret = {
140
+ /** A workspace secret's name, such as `stripe-webhook-signing-secret`. */
136
141
  readonly secret: string;
137
- } & WebhookHmacFormat);
142
+ readonly generated?: undefined;
143
+ } | {
144
+ readonly generated: true;
145
+ readonly secret?: undefined;
146
+ };
138
147
  /** Meta's registration handshake; the verify token is a workspace secret, separate from the app secret. */
139
148
  export interface WebhookVerification {
140
149
  readonly scheme: "meta";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.126.0",
3
+ "version": "0.128.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",