@tupik/openapi 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +106 -2
- package/bin/contract-cli.mjs +4 -0
- package/bin/tupik.mjs +4 -0
- package/dist/cli.d.mts +68 -0
- package/dist/cli.d.ts +68 -0
- package/dist/cli.mjs +299 -0
- package/dist/index.d.mts +164 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.mjs +223 -0
- package/dist/shared/openapi.B3aGIYpz.mjs +544 -0
- package/dist/shared/openapi.BCSoJoZR.d.mts +246 -0
- package/dist/shared/openapi.BCSoJoZR.d.ts +246 -0
- package/package.json +66 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { a as ContractSnapshot, S as SchemaConverter, C as ContractAppLike } from './shared/openapi.BCSoJoZR.mjs';
|
|
2
|
+
export { b as ContractChange, c as ContractChangeKind, d as ContractChangeSeverity, e as ContractRequestFlags, f as ContractRoute, G as GenerateOpenApiOptions, J as JsonSchemaObject, M as MissingConverterMode, O as OpenApiDocument, g as OpenApiInfo, h as OpenApiOperation, i as OpenApiParameter, j as OpenApiPathItem, k as OpenApiRouteInfo, l as OpenApiSecurityRequirement, m as OpenApiSecurityScheme, R as RouteSecurityResolver, n as SchemaDirection, o as SnapshotContractOptions, p as annotateDataClasses, q as diffContracts, r as flattenDataClasses, s as formatContractChanges, t as generateOpenApiDocument, u as hasBreakingChanges, v as jsonSchemaOf, w as operationIdOf, x as ownJsonSchema, y as parseContractSnapshot, z as problemSchema, A as routeProblems, B as routeSchemaConverter, D as snapshotContract, E as toOpenApiPath } from './shared/openapi.BCSoJoZR.mjs';
|
|
3
|
+
import * as _tupik_core from '@tupik/core';
|
|
4
|
+
import { RouteMeta, TupikContext, Access, AccessKind, DataClass } from '@tupik/core';
|
|
5
|
+
|
|
6
|
+
type AuditSeverity = 'error' | 'warning' | 'note';
|
|
7
|
+
/** `none` never fails; otherwise the lowest severity that fails the run. */
|
|
8
|
+
type AuditFailOn = AuditSeverity | 'none';
|
|
9
|
+
type AuditRuleId = 'public-sensitive-data' | 'sensitive-data' | 'write-without-rate-limit' | 'id-without-owner' | 'public-route' | 'breaking-change' | 'contract-warning';
|
|
10
|
+
interface AuditRule {
|
|
11
|
+
readonly severity: AuditSeverity;
|
|
12
|
+
readonly description: string;
|
|
13
|
+
}
|
|
14
|
+
/** Every rule, its default severity and what it checks (also the SARIF rule table). */
|
|
15
|
+
declare const AUDIT_RULES: Readonly<Record<AuditRuleId, AuditRule>>;
|
|
16
|
+
interface AuditFinding {
|
|
17
|
+
readonly rule: AuditRuleId;
|
|
18
|
+
readonly severity: AuditSeverity;
|
|
19
|
+
readonly method: string;
|
|
20
|
+
readonly path: string;
|
|
21
|
+
readonly message: string;
|
|
22
|
+
/** Reason the route gave with `acceptAudit` (the finding is then a `note`). */
|
|
23
|
+
readonly accepted?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Route tag that accepts one audit finding on purpose, with the reason the
|
|
27
|
+
* report shows: `tags: [acceptAudit('public-sensitive-data', 'sign-up receives the e-mail')]`.
|
|
28
|
+
* The finding stays in the report as a `note` (SARIF: suppressed in source).
|
|
29
|
+
*/
|
|
30
|
+
declare function acceptAudit(rule: AuditRuleId, reason: string): string;
|
|
31
|
+
interface AuditReport {
|
|
32
|
+
readonly routes: number;
|
|
33
|
+
readonly findings: readonly AuditFinding[];
|
|
34
|
+
readonly counts: Readonly<Record<AuditSeverity, number>>;
|
|
35
|
+
}
|
|
36
|
+
interface AuditOptions {
|
|
37
|
+
/** Previous contract snapshot: breaking changes are errors, contract warnings are warnings. */
|
|
38
|
+
readonly baseline?: ContractSnapshot;
|
|
39
|
+
/** Tag a rate-limit middleware adds to `meta.tags`. Default `'rate-limited'`. */
|
|
40
|
+
readonly rateLimitTag?: string;
|
|
41
|
+
/** Path parameter names treated as record ids. Default: `id`, `*Id`, `*_id`. */
|
|
42
|
+
readonly idParam?: RegExp;
|
|
43
|
+
readonly disable?: readonly AuditRuleId[];
|
|
44
|
+
/** Used for required body fields when comparing with `baseline`. */
|
|
45
|
+
readonly schemaConverter?: SchemaConverter;
|
|
46
|
+
}
|
|
47
|
+
/** `body.email (pii); response 200: profile.diagnosis (health)` or `''`. */
|
|
48
|
+
declare function describeSensitiveData(meta: RouteMeta): string;
|
|
49
|
+
/** Security audit of an app's route metadata (and of its contract against `baseline`). */
|
|
50
|
+
declare function auditApp(app: ContractAppLike, options?: AuditOptions): AuditReport;
|
|
51
|
+
/** Whether `report` has a finding at `failOn` or worse. */
|
|
52
|
+
declare function shouldFail(report: AuditReport, failOn: AuditFailOn): boolean;
|
|
53
|
+
/** One block per finding (`SEVERITY rule METHOD /path` + indented message) and a summary line. */
|
|
54
|
+
declare function formatAuditText(report: AuditReport): string;
|
|
55
|
+
interface SarifLog {
|
|
56
|
+
readonly version: '2.1.0';
|
|
57
|
+
readonly $schema: string;
|
|
58
|
+
readonly runs: readonly {
|
|
59
|
+
readonly tool: {
|
|
60
|
+
readonly driver: {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly informationUri: string;
|
|
63
|
+
readonly rules: readonly {
|
|
64
|
+
id: string;
|
|
65
|
+
shortDescription: {
|
|
66
|
+
text: string;
|
|
67
|
+
};
|
|
68
|
+
defaultConfiguration: {
|
|
69
|
+
level: AuditSeverity;
|
|
70
|
+
};
|
|
71
|
+
}[];
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
readonly results: readonly {
|
|
75
|
+
ruleId: AuditRuleId;
|
|
76
|
+
level: AuditSeverity;
|
|
77
|
+
message: {
|
|
78
|
+
text: string;
|
|
79
|
+
};
|
|
80
|
+
locations: readonly unknown[];
|
|
81
|
+
suppressions?: readonly {
|
|
82
|
+
kind: 'inSource';
|
|
83
|
+
justification: string;
|
|
84
|
+
}[];
|
|
85
|
+
}[];
|
|
86
|
+
}[];
|
|
87
|
+
}
|
|
88
|
+
/** SARIF 2.1.0 (GitHub code scanning). `uri` is the app module every result points at. */
|
|
89
|
+
declare function auditToSarif(report: AuditReport, options?: {
|
|
90
|
+
readonly uri?: string;
|
|
91
|
+
}): SarifLog;
|
|
92
|
+
|
|
93
|
+
/** Tags that mark the subject request routes, read by `buildDataMap`. */
|
|
94
|
+
declare const SUBJECT_REQUEST_TAGS: {
|
|
95
|
+
readonly export: "subject-request:export";
|
|
96
|
+
readonly erase: "subject-request:erase";
|
|
97
|
+
};
|
|
98
|
+
type SubjectRequestKind = keyof typeof SUBJECT_REQUEST_TAGS;
|
|
99
|
+
type AnyContext = TupikContext<Record<string, unknown>, {
|
|
100
|
+
params: Record<string, string>;
|
|
101
|
+
}>;
|
|
102
|
+
interface SubjectRequestOptions {
|
|
103
|
+
/** Every personal data record of the subject (JSON-serialisable). */
|
|
104
|
+
readonly export: (subjectId: string, ctx: AnyContext) => unknown;
|
|
105
|
+
/** Deletes or anonymises the subject's data (may enqueue the work). */
|
|
106
|
+
readonly erase: (subjectId: string, ctx: AnyContext) => unknown;
|
|
107
|
+
/** Who may call the routes. Default `'authenticated'`; `'public'` is refused. */
|
|
108
|
+
readonly access?: Access;
|
|
109
|
+
/** Route prefix. Default `'/privacy'` (`GET /privacy/export`, `DELETE /privacy/data`). */
|
|
110
|
+
readonly prefix?: string;
|
|
111
|
+
/** Subject of the request. Default: the principal id (`principal.id`, or a string principal). */
|
|
112
|
+
readonly subjectId?: (ctx: AnyContext) => string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Routes for data subject requests (LGPD art. 18, GDPR arts. 15, 17 and 20):
|
|
116
|
+
* `GET <prefix>/export` answers `{ subjectId, exportedAt, data }` with
|
|
117
|
+
* `cache-control: no-store`; `DELETE <prefix>/data` runs `erase` and answers
|
|
118
|
+
* 202. Both need a principal and are tagged so `tupik data-map` lists them.
|
|
119
|
+
*/
|
|
120
|
+
declare function defineSubjectRequests(options: SubjectRequestOptions): _tupik_core.WithRoute<Record<string, unknown>, never, string, "DELETE", "/data", _tupik_core.RouteSchema, Response, unknown, "required" | "optional", never>;
|
|
121
|
+
|
|
122
|
+
/** What the team writes about a route's processing (merged into the map). */
|
|
123
|
+
interface DataMapAnnotation {
|
|
124
|
+
readonly purpose?: string;
|
|
125
|
+
/** LGPD art. 7/11 or GDPR art. 6/9 basis, in your words. */
|
|
126
|
+
readonly legalBasis?: string;
|
|
127
|
+
readonly retention?: string;
|
|
128
|
+
readonly notes?: string;
|
|
129
|
+
}
|
|
130
|
+
/** `tupik data-map --config <file>`: annotations per `METHOD /path`, plus defaults for every route. */
|
|
131
|
+
interface DataMapConfig {
|
|
132
|
+
readonly defaults?: DataMapAnnotation;
|
|
133
|
+
readonly routes?: Readonly<Record<string, DataMapAnnotation>>;
|
|
134
|
+
}
|
|
135
|
+
type FieldsByClass = {
|
|
136
|
+
readonly [TClass in DataClass]?: readonly string[];
|
|
137
|
+
};
|
|
138
|
+
interface DataMapEntry extends DataMapAnnotation {
|
|
139
|
+
readonly method: string;
|
|
140
|
+
readonly path: string;
|
|
141
|
+
readonly access: AccessKind;
|
|
142
|
+
/** Request fields per class, prefixed by slot (`body.email`). */
|
|
143
|
+
readonly receives: FieldsByClass;
|
|
144
|
+
/** Response fields per class, by status. */
|
|
145
|
+
readonly returns: Readonly<Record<number, FieldsByClass>>;
|
|
146
|
+
}
|
|
147
|
+
interface DataMap {
|
|
148
|
+
readonly routes: readonly DataMapEntry[];
|
|
149
|
+
/** Subject request routes (`defineSubjectRequests`), as `METHOD /path`. */
|
|
150
|
+
readonly subjectRequests: {
|
|
151
|
+
readonly [TKind in SubjectRequestKind]?: string;
|
|
152
|
+
};
|
|
153
|
+
/** Routes with classified data but no purpose or legal basis. */
|
|
154
|
+
readonly unannotated: readonly string[];
|
|
155
|
+
}
|
|
156
|
+
/** Validates untrusted JSON (the `--config` file) as a `DataMapConfig`. */
|
|
157
|
+
declare function parseDataMapConfig(value: unknown): DataMapConfig;
|
|
158
|
+
/** Inventory of the personal data each route receives and returns, merged with `config`. */
|
|
159
|
+
declare function buildDataMap(app: ContractAppLike, config?: DataMapConfig): DataMap;
|
|
160
|
+
/** Markdown report: one table row per route, then the subject requests and the routes still missing a legal basis. */
|
|
161
|
+
declare function formatDataMapMarkdown(map: DataMap): string;
|
|
162
|
+
|
|
163
|
+
export { AUDIT_RULES, ContractAppLike, ContractSnapshot, SUBJECT_REQUEST_TAGS, SchemaConverter, acceptAudit, auditApp, auditToSarif, buildDataMap, defineSubjectRequests, describeSensitiveData, formatAuditText, formatDataMapMarkdown, parseDataMapConfig, shouldFail };
|
|
164
|
+
export type { AuditFailOn, AuditFinding, AuditOptions, AuditReport, AuditRule, AuditRuleId, AuditSeverity, DataMap, DataMapAnnotation, DataMapConfig, DataMapEntry, FieldsByClass, SarifLog, SubjectRequestKind, SubjectRequestOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { a as ContractSnapshot, S as SchemaConverter, C as ContractAppLike } from './shared/openapi.BCSoJoZR.js';
|
|
2
|
+
export { b as ContractChange, c as ContractChangeKind, d as ContractChangeSeverity, e as ContractRequestFlags, f as ContractRoute, G as GenerateOpenApiOptions, J as JsonSchemaObject, M as MissingConverterMode, O as OpenApiDocument, g as OpenApiInfo, h as OpenApiOperation, i as OpenApiParameter, j as OpenApiPathItem, k as OpenApiRouteInfo, l as OpenApiSecurityRequirement, m as OpenApiSecurityScheme, R as RouteSecurityResolver, n as SchemaDirection, o as SnapshotContractOptions, p as annotateDataClasses, q as diffContracts, r as flattenDataClasses, s as formatContractChanges, t as generateOpenApiDocument, u as hasBreakingChanges, v as jsonSchemaOf, w as operationIdOf, x as ownJsonSchema, y as parseContractSnapshot, z as problemSchema, A as routeProblems, B as routeSchemaConverter, D as snapshotContract, E as toOpenApiPath } from './shared/openapi.BCSoJoZR.js';
|
|
3
|
+
import * as _tupik_core from '@tupik/core';
|
|
4
|
+
import { RouteMeta, TupikContext, Access, AccessKind, DataClass } from '@tupik/core';
|
|
5
|
+
|
|
6
|
+
type AuditSeverity = 'error' | 'warning' | 'note';
|
|
7
|
+
/** `none` never fails; otherwise the lowest severity that fails the run. */
|
|
8
|
+
type AuditFailOn = AuditSeverity | 'none';
|
|
9
|
+
type AuditRuleId = 'public-sensitive-data' | 'sensitive-data' | 'write-without-rate-limit' | 'id-without-owner' | 'public-route' | 'breaking-change' | 'contract-warning';
|
|
10
|
+
interface AuditRule {
|
|
11
|
+
readonly severity: AuditSeverity;
|
|
12
|
+
readonly description: string;
|
|
13
|
+
}
|
|
14
|
+
/** Every rule, its default severity and what it checks (also the SARIF rule table). */
|
|
15
|
+
declare const AUDIT_RULES: Readonly<Record<AuditRuleId, AuditRule>>;
|
|
16
|
+
interface AuditFinding {
|
|
17
|
+
readonly rule: AuditRuleId;
|
|
18
|
+
readonly severity: AuditSeverity;
|
|
19
|
+
readonly method: string;
|
|
20
|
+
readonly path: string;
|
|
21
|
+
readonly message: string;
|
|
22
|
+
/** Reason the route gave with `acceptAudit` (the finding is then a `note`). */
|
|
23
|
+
readonly accepted?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Route tag that accepts one audit finding on purpose, with the reason the
|
|
27
|
+
* report shows: `tags: [acceptAudit('public-sensitive-data', 'sign-up receives the e-mail')]`.
|
|
28
|
+
* The finding stays in the report as a `note` (SARIF: suppressed in source).
|
|
29
|
+
*/
|
|
30
|
+
declare function acceptAudit(rule: AuditRuleId, reason: string): string;
|
|
31
|
+
interface AuditReport {
|
|
32
|
+
readonly routes: number;
|
|
33
|
+
readonly findings: readonly AuditFinding[];
|
|
34
|
+
readonly counts: Readonly<Record<AuditSeverity, number>>;
|
|
35
|
+
}
|
|
36
|
+
interface AuditOptions {
|
|
37
|
+
/** Previous contract snapshot: breaking changes are errors, contract warnings are warnings. */
|
|
38
|
+
readonly baseline?: ContractSnapshot;
|
|
39
|
+
/** Tag a rate-limit middleware adds to `meta.tags`. Default `'rate-limited'`. */
|
|
40
|
+
readonly rateLimitTag?: string;
|
|
41
|
+
/** Path parameter names treated as record ids. Default: `id`, `*Id`, `*_id`. */
|
|
42
|
+
readonly idParam?: RegExp;
|
|
43
|
+
readonly disable?: readonly AuditRuleId[];
|
|
44
|
+
/** Used for required body fields when comparing with `baseline`. */
|
|
45
|
+
readonly schemaConverter?: SchemaConverter;
|
|
46
|
+
}
|
|
47
|
+
/** `body.email (pii); response 200: profile.diagnosis (health)` or `''`. */
|
|
48
|
+
declare function describeSensitiveData(meta: RouteMeta): string;
|
|
49
|
+
/** Security audit of an app's route metadata (and of its contract against `baseline`). */
|
|
50
|
+
declare function auditApp(app: ContractAppLike, options?: AuditOptions): AuditReport;
|
|
51
|
+
/** Whether `report` has a finding at `failOn` or worse. */
|
|
52
|
+
declare function shouldFail(report: AuditReport, failOn: AuditFailOn): boolean;
|
|
53
|
+
/** One block per finding (`SEVERITY rule METHOD /path` + indented message) and a summary line. */
|
|
54
|
+
declare function formatAuditText(report: AuditReport): string;
|
|
55
|
+
interface SarifLog {
|
|
56
|
+
readonly version: '2.1.0';
|
|
57
|
+
readonly $schema: string;
|
|
58
|
+
readonly runs: readonly {
|
|
59
|
+
readonly tool: {
|
|
60
|
+
readonly driver: {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly informationUri: string;
|
|
63
|
+
readonly rules: readonly {
|
|
64
|
+
id: string;
|
|
65
|
+
shortDescription: {
|
|
66
|
+
text: string;
|
|
67
|
+
};
|
|
68
|
+
defaultConfiguration: {
|
|
69
|
+
level: AuditSeverity;
|
|
70
|
+
};
|
|
71
|
+
}[];
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
readonly results: readonly {
|
|
75
|
+
ruleId: AuditRuleId;
|
|
76
|
+
level: AuditSeverity;
|
|
77
|
+
message: {
|
|
78
|
+
text: string;
|
|
79
|
+
};
|
|
80
|
+
locations: readonly unknown[];
|
|
81
|
+
suppressions?: readonly {
|
|
82
|
+
kind: 'inSource';
|
|
83
|
+
justification: string;
|
|
84
|
+
}[];
|
|
85
|
+
}[];
|
|
86
|
+
}[];
|
|
87
|
+
}
|
|
88
|
+
/** SARIF 2.1.0 (GitHub code scanning). `uri` is the app module every result points at. */
|
|
89
|
+
declare function auditToSarif(report: AuditReport, options?: {
|
|
90
|
+
readonly uri?: string;
|
|
91
|
+
}): SarifLog;
|
|
92
|
+
|
|
93
|
+
/** Tags that mark the subject request routes, read by `buildDataMap`. */
|
|
94
|
+
declare const SUBJECT_REQUEST_TAGS: {
|
|
95
|
+
readonly export: "subject-request:export";
|
|
96
|
+
readonly erase: "subject-request:erase";
|
|
97
|
+
};
|
|
98
|
+
type SubjectRequestKind = keyof typeof SUBJECT_REQUEST_TAGS;
|
|
99
|
+
type AnyContext = TupikContext<Record<string, unknown>, {
|
|
100
|
+
params: Record<string, string>;
|
|
101
|
+
}>;
|
|
102
|
+
interface SubjectRequestOptions {
|
|
103
|
+
/** Every personal data record of the subject (JSON-serialisable). */
|
|
104
|
+
readonly export: (subjectId: string, ctx: AnyContext) => unknown;
|
|
105
|
+
/** Deletes or anonymises the subject's data (may enqueue the work). */
|
|
106
|
+
readonly erase: (subjectId: string, ctx: AnyContext) => unknown;
|
|
107
|
+
/** Who may call the routes. Default `'authenticated'`; `'public'` is refused. */
|
|
108
|
+
readonly access?: Access;
|
|
109
|
+
/** Route prefix. Default `'/privacy'` (`GET /privacy/export`, `DELETE /privacy/data`). */
|
|
110
|
+
readonly prefix?: string;
|
|
111
|
+
/** Subject of the request. Default: the principal id (`principal.id`, or a string principal). */
|
|
112
|
+
readonly subjectId?: (ctx: AnyContext) => string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Routes for data subject requests (LGPD art. 18, GDPR arts. 15, 17 and 20):
|
|
116
|
+
* `GET <prefix>/export` answers `{ subjectId, exportedAt, data }` with
|
|
117
|
+
* `cache-control: no-store`; `DELETE <prefix>/data` runs `erase` and answers
|
|
118
|
+
* 202. Both need a principal and are tagged so `tupik data-map` lists them.
|
|
119
|
+
*/
|
|
120
|
+
declare function defineSubjectRequests(options: SubjectRequestOptions): _tupik_core.WithRoute<Record<string, unknown>, never, string, "DELETE", "/data", _tupik_core.RouteSchema, Response, unknown, "required" | "optional", never>;
|
|
121
|
+
|
|
122
|
+
/** What the team writes about a route's processing (merged into the map). */
|
|
123
|
+
interface DataMapAnnotation {
|
|
124
|
+
readonly purpose?: string;
|
|
125
|
+
/** LGPD art. 7/11 or GDPR art. 6/9 basis, in your words. */
|
|
126
|
+
readonly legalBasis?: string;
|
|
127
|
+
readonly retention?: string;
|
|
128
|
+
readonly notes?: string;
|
|
129
|
+
}
|
|
130
|
+
/** `tupik data-map --config <file>`: annotations per `METHOD /path`, plus defaults for every route. */
|
|
131
|
+
interface DataMapConfig {
|
|
132
|
+
readonly defaults?: DataMapAnnotation;
|
|
133
|
+
readonly routes?: Readonly<Record<string, DataMapAnnotation>>;
|
|
134
|
+
}
|
|
135
|
+
type FieldsByClass = {
|
|
136
|
+
readonly [TClass in DataClass]?: readonly string[];
|
|
137
|
+
};
|
|
138
|
+
interface DataMapEntry extends DataMapAnnotation {
|
|
139
|
+
readonly method: string;
|
|
140
|
+
readonly path: string;
|
|
141
|
+
readonly access: AccessKind;
|
|
142
|
+
/** Request fields per class, prefixed by slot (`body.email`). */
|
|
143
|
+
readonly receives: FieldsByClass;
|
|
144
|
+
/** Response fields per class, by status. */
|
|
145
|
+
readonly returns: Readonly<Record<number, FieldsByClass>>;
|
|
146
|
+
}
|
|
147
|
+
interface DataMap {
|
|
148
|
+
readonly routes: readonly DataMapEntry[];
|
|
149
|
+
/** Subject request routes (`defineSubjectRequests`), as `METHOD /path`. */
|
|
150
|
+
readonly subjectRequests: {
|
|
151
|
+
readonly [TKind in SubjectRequestKind]?: string;
|
|
152
|
+
};
|
|
153
|
+
/** Routes with classified data but no purpose or legal basis. */
|
|
154
|
+
readonly unannotated: readonly string[];
|
|
155
|
+
}
|
|
156
|
+
/** Validates untrusted JSON (the `--config` file) as a `DataMapConfig`. */
|
|
157
|
+
declare function parseDataMapConfig(value: unknown): DataMapConfig;
|
|
158
|
+
/** Inventory of the personal data each route receives and returns, merged with `config`. */
|
|
159
|
+
declare function buildDataMap(app: ContractAppLike, config?: DataMapConfig): DataMap;
|
|
160
|
+
/** Markdown report: one table row per route, then the subject requests and the routes still missing a legal basis. */
|
|
161
|
+
declare function formatDataMapMarkdown(map: DataMap): string;
|
|
162
|
+
|
|
163
|
+
export { AUDIT_RULES, ContractAppLike, ContractSnapshot, SUBJECT_REQUEST_TAGS, SchemaConverter, acceptAudit, auditApp, auditToSarif, buildDataMap, defineSubjectRequests, describeSensitiveData, formatAuditText, formatDataMapMarkdown, parseDataMapConfig, shouldFail };
|
|
164
|
+
export type { AuditFailOn, AuditFinding, AuditOptions, AuditReport, AuditRule, AuditRuleId, AuditSeverity, DataMap, DataMapAnnotation, DataMapConfig, DataMapEntry, FieldsByClass, SarifLog, SubjectRequestKind, SubjectRequestOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { k as classifiedJsonSchema, l as isRecord } from './shared/openapi.B3aGIYpz.mjs';
|
|
2
|
+
export { A as AUDIT_RULES, S as SUBJECT_REQUEST_TAGS, m as acceptAudit, n as annotateDataClasses, e as auditApp, i as auditToSarif, b as buildDataMap, o as defineSubjectRequests, q as describeSensitiveData, d as diffContracts, r as flattenDataClasses, j as formatAuditText, f as formatContractChanges, c as formatDataMapMarkdown, h as hasBreakingChanges, t as jsonSchemaOf, u as ownJsonSchema, p as parseContractSnapshot, a as parseDataMapConfig, g as shouldFail, s as snapshotContract } from './shared/openapi.B3aGIYpz.mjs';
|
|
3
|
+
import '@tupik/core';
|
|
4
|
+
|
|
5
|
+
const UNAUTHORIZED = { code: "UNAUTHORIZED", status: 401, title: "Unauthorized" };
|
|
6
|
+
const ACCESS_DENIALS = {
|
|
7
|
+
public: [],
|
|
8
|
+
authenticated: [],
|
|
9
|
+
policy: [{ code: "FORBIDDEN", status: 403, title: "Forbidden" }],
|
|
10
|
+
owner: [{ code: "NOT_FOUND", status: 404, title: "Not Found" }]
|
|
11
|
+
};
|
|
12
|
+
function routeProblems(meta) {
|
|
13
|
+
return [...meta.auth === "required" ? [UNAUTHORIZED] : [], ...ACCESS_DENIALS[meta.access], ...meta.errors];
|
|
14
|
+
}
|
|
15
|
+
function problemSchema(codes) {
|
|
16
|
+
return {
|
|
17
|
+
type: "object",
|
|
18
|
+
required: ["status", "title"],
|
|
19
|
+
properties: {
|
|
20
|
+
type: { type: "string" },
|
|
21
|
+
title: { type: "string" },
|
|
22
|
+
status: { type: "integer" },
|
|
23
|
+
detail: { type: "string" },
|
|
24
|
+
code: codes.length === 0 ? { type: "string" } : { type: "string", enum: [...codes] }
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function problemResponses(problems) {
|
|
29
|
+
const byStatus = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const problem of problems) {
|
|
31
|
+
const list = byStatus.get(String(problem.status)) ?? [];
|
|
32
|
+
if (!list.some((known) => known.code === problem.code)) list.push(problem);
|
|
33
|
+
byStatus.set(String(problem.status), list);
|
|
34
|
+
}
|
|
35
|
+
const responses = /* @__PURE__ */ new Map();
|
|
36
|
+
for (const [status, list] of byStatus) {
|
|
37
|
+
responses.set(status, {
|
|
38
|
+
description: [...new Set(list.map((problem) => problem.title))].join("; "),
|
|
39
|
+
content: { "application/problem+json": { schema: problemSchema(list.map((problem) => problem.code)) } }
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return responses;
|
|
43
|
+
}
|
|
44
|
+
const SECURITY_BY_AUTH = {
|
|
45
|
+
none: () => [],
|
|
46
|
+
optional: (scheme) => [{}, { [scheme]: [] }],
|
|
47
|
+
required: (scheme) => [{ [scheme]: [] }]
|
|
48
|
+
};
|
|
49
|
+
function securityFor(auth, scheme) {
|
|
50
|
+
return SECURITY_BY_AUTH[auth](scheme);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const METHOD_KEYS = {
|
|
54
|
+
GET: "get",
|
|
55
|
+
POST: "post",
|
|
56
|
+
PUT: "put",
|
|
57
|
+
DELETE: "delete",
|
|
58
|
+
PATCH: "patch",
|
|
59
|
+
HEAD: "head",
|
|
60
|
+
OPTIONS: "options"
|
|
61
|
+
};
|
|
62
|
+
function toOpenApiPath(path) {
|
|
63
|
+
return path.replace(/:([A-Za-z0-9_]+)/g, "{$1}");
|
|
64
|
+
}
|
|
65
|
+
function pathParameterNames(path) {
|
|
66
|
+
const names = [];
|
|
67
|
+
const re = /:([A-Za-z0-9_]+)/g;
|
|
68
|
+
let match;
|
|
69
|
+
while ((match = re.exec(path)) !== null) {
|
|
70
|
+
const name = match[1];
|
|
71
|
+
if (name !== void 0) names.push(name);
|
|
72
|
+
}
|
|
73
|
+
return names;
|
|
74
|
+
}
|
|
75
|
+
function routeSchemaConverter(route, converter, onMissing = "error") {
|
|
76
|
+
return (schema, slot, direction = "input") => {
|
|
77
|
+
const json = classifiedJsonSchema(schema, direction, converter);
|
|
78
|
+
if (json !== void 0) return json;
|
|
79
|
+
const message = `@tupik/openapi: ${route.method} ${route.path} ${slot}: the "${schema["~standard"].vendor}" schema has no JSON Schema; pass options.schemaConverter (e.g. z.toJSONSchema) or onMissingConverter: 'warn'`;
|
|
80
|
+
if (onMissing === "error") throw new Error(message);
|
|
81
|
+
if (onMissing === "warn") console.warn(message);
|
|
82
|
+
return {};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function objectProperties(json) {
|
|
86
|
+
const properties = json["properties"];
|
|
87
|
+
if (!isRecord(properties)) return [];
|
|
88
|
+
const required = Array.isArray(json["required"]) ? json["required"] : [];
|
|
89
|
+
return Object.entries(properties).map(([name, schema]) => ({
|
|
90
|
+
name,
|
|
91
|
+
required: required.includes(name),
|
|
92
|
+
schema: isRecord(schema) ? schema : {}
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
function parametersOf(route, convert) {
|
|
96
|
+
const schema = route.schema;
|
|
97
|
+
const pathSchemas = /* @__PURE__ */ new Map();
|
|
98
|
+
if (schema?.params !== void 0) {
|
|
99
|
+
for (const p of objectProperties(convert(schema.params, "params"))) pathSchemas.set(p.name, p.schema);
|
|
100
|
+
}
|
|
101
|
+
const parameters = pathParameterNames(route.path).map((name) => ({
|
|
102
|
+
name,
|
|
103
|
+
in: "path",
|
|
104
|
+
required: true,
|
|
105
|
+
schema: pathSchemas.get(name) ?? { type: "string" }
|
|
106
|
+
}));
|
|
107
|
+
for (const [slot, location] of [["query", "query"], ["headers", "header"]]) {
|
|
108
|
+
const slotSchema = schema?.[slot];
|
|
109
|
+
if (slotSchema === void 0) continue;
|
|
110
|
+
for (const p of objectProperties(convert(slotSchema, slot))) {
|
|
111
|
+
parameters.push({ name: p.name, in: location, required: p.required, schema: p.schema });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return parameters;
|
|
115
|
+
}
|
|
116
|
+
function pascal(text) {
|
|
117
|
+
return text.split(/[^A-Za-z0-9]+/).filter((word) => word !== "").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
118
|
+
}
|
|
119
|
+
function operationIdOf(route) {
|
|
120
|
+
const words = route.path.split("/").filter((segment) => segment !== "").map((segment) => {
|
|
121
|
+
if (segment.startsWith(":")) return "By" + pascal(segment.slice(1));
|
|
122
|
+
if (segment.startsWith("*")) return "Wildcard" + pascal(segment.slice(1));
|
|
123
|
+
return pascal(segment);
|
|
124
|
+
});
|
|
125
|
+
return route.method.toLowerCase() + (words.join("") || "Root");
|
|
126
|
+
}
|
|
127
|
+
function defaultTags(route) {
|
|
128
|
+
const first = route.path.split("/").find((segment) => segment !== "");
|
|
129
|
+
return first === void 0 || first.startsWith(":") || first.startsWith("*") ? void 0 : [first];
|
|
130
|
+
}
|
|
131
|
+
const STATUS_DESCRIPTIONS = {
|
|
132
|
+
"200": "OK",
|
|
133
|
+
"201": "Created",
|
|
134
|
+
"202": "Accepted",
|
|
135
|
+
"204": "No Content",
|
|
136
|
+
"400": "Bad Request",
|
|
137
|
+
"401": "Unauthorized",
|
|
138
|
+
"403": "Forbidden",
|
|
139
|
+
"404": "Not Found",
|
|
140
|
+
"409": "Conflict",
|
|
141
|
+
"422": "Unprocessable Content",
|
|
142
|
+
"500": "Internal Server Error"
|
|
143
|
+
};
|
|
144
|
+
function describeResponse(status, schema, convert) {
|
|
145
|
+
const description = STATUS_DESCRIPTIONS[status] ?? `Response ${status}`;
|
|
146
|
+
if (status === "204") return { description };
|
|
147
|
+
return { description, content: { "application/json": { schema: convert(schema, `response ${status}`, "output") } } };
|
|
148
|
+
}
|
|
149
|
+
function defaultScheme(options) {
|
|
150
|
+
const names = Object.keys(options.securitySchemes ?? {});
|
|
151
|
+
const scheme = options.securityScheme;
|
|
152
|
+
if (scheme === void 0) return names.length === 1 ? names[0] : void 0;
|
|
153
|
+
if (!names.includes(scheme)) {
|
|
154
|
+
throw new Error(`@tupik/openapi: securityScheme "${scheme}" is not in securitySchemes (${names.join(", ") || "none"})`);
|
|
155
|
+
}
|
|
156
|
+
return scheme;
|
|
157
|
+
}
|
|
158
|
+
function routeSecurity(route, options, scheme) {
|
|
159
|
+
const custom = options.routeSecurity?.(route);
|
|
160
|
+
if (custom !== void 0) return custom;
|
|
161
|
+
return route.meta === void 0 || scheme === void 0 ? void 0 : securityFor(route.meta.auth, scheme);
|
|
162
|
+
}
|
|
163
|
+
function generateOpenApiDocument(app, options = {}) {
|
|
164
|
+
const tagsOf = options.tags ?? defaultTags;
|
|
165
|
+
const scheme = defaultScheme(options);
|
|
166
|
+
const paths = {};
|
|
167
|
+
const operationIds = /* @__PURE__ */ new Set();
|
|
168
|
+
for (const route of app.routes) {
|
|
169
|
+
const convert = routeSchemaConverter(route, options.schemaConverter, options.onMissingConverter ?? "error");
|
|
170
|
+
const openApiPath = toOpenApiPath(route.path);
|
|
171
|
+
const item = paths[openApiPath] ??= {};
|
|
172
|
+
const key = METHOD_KEYS[route.method];
|
|
173
|
+
const responses = {};
|
|
174
|
+
let hasSuccess = false;
|
|
175
|
+
const responseSchemas = route.schema?.response;
|
|
176
|
+
if (responseSchemas !== void 0) {
|
|
177
|
+
for (const [status, schema] of Object.entries(responseSchemas)) {
|
|
178
|
+
if (status.startsWith("2")) hasSuccess = true;
|
|
179
|
+
responses[status] = describeResponse(status, schema, convert);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!hasSuccess) responses["200"] = { description: "Successful response" };
|
|
183
|
+
const baseId = operationIdOf(route);
|
|
184
|
+
let operationId = baseId;
|
|
185
|
+
for (let n = 2; operationIds.has(operationId); n += 1) operationId = `${baseId}_${n}`;
|
|
186
|
+
operationIds.add(operationId);
|
|
187
|
+
const operation = { operationId, responses };
|
|
188
|
+
const tags = tagsOf(route);
|
|
189
|
+
if (tags !== void 0 && tags.length > 0) operation.tags = tags;
|
|
190
|
+
const parameters = parametersOf(route, convert);
|
|
191
|
+
if (parameters.length > 0) operation.parameters = parameters;
|
|
192
|
+
const bodySchema = route.schema?.body;
|
|
193
|
+
if (bodySchema !== void 0) {
|
|
194
|
+
operation.requestBody = {
|
|
195
|
+
required: true,
|
|
196
|
+
content: { "application/json": { schema: convert(bodySchema, "body") } }
|
|
197
|
+
};
|
|
198
|
+
responses["400"] ??= {
|
|
199
|
+
description: "Validation error",
|
|
200
|
+
content: { "application/problem+json": { schema: { type: "object" } } }
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (route.meta !== void 0) {
|
|
204
|
+
for (const [status, response] of problemResponses(routeProblems(route.meta))) {
|
|
205
|
+
if (responseSchemas?.[Number(status)] === void 0) responses[status] = response;
|
|
206
|
+
}
|
|
207
|
+
operation["x-access"] = route.meta.access;
|
|
208
|
+
}
|
|
209
|
+
const security = routeSecurity(route, options, scheme);
|
|
210
|
+
if (security !== void 0) operation.security = security;
|
|
211
|
+
item[key] = operation;
|
|
212
|
+
}
|
|
213
|
+
const document = {
|
|
214
|
+
openapi: "3.1.0",
|
|
215
|
+
info: options.info ?? { title: "Tupik", version: "0.0.0" },
|
|
216
|
+
paths
|
|
217
|
+
};
|
|
218
|
+
if (options.securitySchemes !== void 0) document.components = { securitySchemes: options.securitySchemes };
|
|
219
|
+
if (options.security !== void 0) document.security = options.security;
|
|
220
|
+
return document;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export { generateOpenApiDocument, operationIdOf, problemSchema, routeProblems, routeSchemaConverter, toOpenApiPath };
|