@synopackageland/cli 0.49.1 → 0.50.1
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 +11 -0
- package/README.md +10 -0
- package/dist/build-spk/generator.js +3 -3
- package/dist/build-spk/reproducible-archive.js +3 -1
- package/dist/ctl/ai-tools.d.ts +11 -1
- package/dist/ctl/ai-tools.js +4 -4
- package/dist/ctl/cli.d.ts +8 -1
- package/dist/ctl/cli.js +54 -5
- package/dist/ctl/descriptor.d.ts +15 -2
- package/dist/ctl/descriptor.js +9 -4
- package/dist/ctl/diagnostics.d.ts +16 -0
- package/dist/ctl/diagnostics.js +11 -0
- package/dist/ctl/flows.js +2 -0
- package/dist/ctl/input.d.ts +5 -3
- package/dist/ctl/input.js +3 -3
- package/dist/ctl/native-auth-command.d.ts +9 -0
- package/dist/ctl/native-auth-command.js +103 -0
- package/dist/ctl/native-executor.d.ts +16 -0
- package/dist/ctl/native-executor.js +187 -0
- package/dist/ctl/native-file-types.d.ts +16 -0
- package/dist/ctl/native-file-types.js +57 -0
- package/dist/ctl/native-files.d.ts +13 -0
- package/dist/ctl/native-files.js +115 -0
- package/dist/ctl/native-metadata.d.ts +10 -0
- package/dist/ctl/native-metadata.js +50 -0
- package/dist/ctl/native-paths.d.ts +4 -0
- package/dist/ctl/native-paths.js +65 -0
- package/dist/ctl/native-protocol.d.ts +21 -0
- package/dist/ctl/native-protocol.js +150 -0
- package/dist/ctl/native-registry.d.ts +8 -0
- package/dist/ctl/native-registry.js +182 -0
- package/dist/ctl/native-schemas.d.ts +7 -0
- package/dist/ctl/native-schemas.js +25 -0
- package/dist/ctl/native-store.d.ts +67 -0
- package/dist/ctl/native-store.js +342 -0
- package/dist/ctl/native-transfers.d.ts +13 -0
- package/dist/ctl/native-transfers.js +331 -0
- package/dist/ctl/native-transport.d.ts +30 -0
- package/dist/ctl/native-transport.js +148 -0
- package/dist/ctl/native-types.d.ts +15 -0
- package/dist/ctl/native-types.js +1 -0
- package/dist/ctl/registry.d.ts +5 -4
- package/dist/ctl/registry.js +12 -1
- package/dist/ctl/service.d.ts +34 -1
- package/dist/ctl/service.js +122 -1
- package/dist/ctl/windows-acl.d.ts +39 -0
- package/dist/ctl/windows-acl.js +80 -0
- package/dist/ctl/windows-prompt.d.ts +5 -0
- package/dist/ctl/windows-prompt.js +119 -0
- package/dist/ctl/windows-sddl.d.ts +20 -0
- package/dist/ctl/windows-sddl.js +162 -0
- package/dist/ctl/windows-security-helper.d.ts +7 -0
- package/dist/ctl/windows-security-helper.js +88 -0
- package/dist/ctl/windows-security.d.ts +10 -0
- package/dist/ctl/windows-security.js +95 -0
- package/dist/ctl/windows-tools.d.ts +11 -0
- package/dist/ctl/windows-tools.js +23 -0
- package/dist/error-registry.js +15 -0
- package/dist/generated/native-http.d.ts +189 -0
- package/dist/generated/native-http.js +1 -0
- package/dist/nas/store.d.ts +3 -1
- package/dist/nas/store.js +6 -3
- package/dist/nas/transport.d.ts +1 -0
- package/dist/native-http-schema/descriptor.schema.json +242 -0
- package/dist/native-http-schema/inventory.schema.json +121 -0
- package/dist/native-http-schema/registry.schema.json +872 -0
- package/dist/native-http-schema/source.json +1 -0
- package/dist/runtime/sdk-source.d.ts +1 -1
- package/dist/runtime/sdk-source.js +1 -1
- package/dist/spk/extract.d.ts +6 -0
- package/dist/spk/extract.js +39 -0
- package/dist/standalone/api-description.d.ts +2 -5
- package/dist/standalone/build.js +5 -1
- package/dist/standalone/compose-errors.js +5 -0
- package/dist/standalone/daemon.js +1 -0
- package/dist/standalone/inspect-spk.js +23 -37
- package/dist/standalone/install-ports.js +2 -1
- package/dist/standalone/lifecycle.js +9 -2
- package/dist/standalone/manifest.d.ts +1 -0
- package/dist/standalone/manifest.js +22 -5
- package/dist/standalone/native-http-inspect.d.ts +9 -0
- package/dist/standalone/native-http-inspect.js +43 -0
- package/dist/standalone/native-http.d.ts +31 -0
- package/dist/standalone/native-http.js +120 -0
- package/dist/standalone/native-layout.js +2 -0
- package/dist/standalone/operation-registry.d.ts +2 -7
- package/dist/standalone/operation-registry.js +3 -0
- package/dist/standalone/spk.d.ts +1 -0
- package/dist/standalone/spk.js +15 -1
- package/package.json +3 -2
- package/schema/dsm-api-contract.json +8 -7
- package/skills/SKILL.md +2 -2
- package/skills/reference/compose.md +21 -0
- package/skills/reference/ctl.md +229 -0
- package/skills/reference/daemon-mode.md +1 -1
- package/skills/reference/upstream-fork.md +5 -2
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { CtlError } from './errors.js';
|
|
2
|
+
import { strictJson } from './json.js';
|
|
3
|
+
import { validateNativeToken } from './native-store.js';
|
|
4
|
+
import { nativeBytes } from './native-transport.js';
|
|
5
|
+
export function pointer(value, path) {
|
|
6
|
+
if (path === '')
|
|
7
|
+
return value;
|
|
8
|
+
if (!path.startsWith('/') || /~(?![01])/.test(path))
|
|
9
|
+
throw mismatch();
|
|
10
|
+
for (const key of path.slice(1).split('/').map(part => part.replaceAll('~1', '/').replaceAll('~0', '~'))) {
|
|
11
|
+
if (!value || typeof value !== 'object' || !Object.hasOwn(value, key))
|
|
12
|
+
throw mismatch();
|
|
13
|
+
value = value[key];
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
export function mismatch() { return new CtlError('CTL_NATIVE_RESPONSE_INVALID', 'Native response does not match the declared contract.', 'Check App version and API configuration; the request was not replayed.', 5); }
|
|
18
|
+
export function nativeMime(response, allowed) {
|
|
19
|
+
const encoding = response.headers['content-encoding'];
|
|
20
|
+
const mime = String(response.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase();
|
|
21
|
+
if (encoding && encoding !== 'identity' || !allowed.map(type => type.toLowerCase()).includes(mime)) {
|
|
22
|
+
response.body.destroy();
|
|
23
|
+
throw mismatch();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function project(value, spec) {
|
|
27
|
+
if (!spec.project)
|
|
28
|
+
return value;
|
|
29
|
+
if ('pointer' in spec.project)
|
|
30
|
+
return pointer(value, spec.project.pointer);
|
|
31
|
+
return Object.fromEntries(Object.entries(spec.project.fields).map(([key, path]) => [key, pointer(value, path)]));
|
|
32
|
+
}
|
|
33
|
+
export async function decodeNative(response, spec, maxBytes, mutation = false) {
|
|
34
|
+
if (!spec.statuses.includes(response.status)) {
|
|
35
|
+
response.body.destroy();
|
|
36
|
+
throw mismatch();
|
|
37
|
+
}
|
|
38
|
+
// Only an explicitly authored mutation can acknowledge success without a
|
|
39
|
+
// representation. Drain and verify emptiness, rather than trusting the header.
|
|
40
|
+
if (mutation && (response.status === 204 || response.status === 200 && response.headers['content-length'] === '0')) {
|
|
41
|
+
const encoding = response.headers['content-encoding'], length = response.headers['content-length'];
|
|
42
|
+
if (encoding && encoding !== 'identity' || length !== undefined && length !== '0') {
|
|
43
|
+
response.body.destroy();
|
|
44
|
+
throw mismatch();
|
|
45
|
+
}
|
|
46
|
+
const body = await nativeBytes(response, Math.min(maxBytes, spec.maxBytes, 16 * 1024 * 1024), true);
|
|
47
|
+
if (body.length) {
|
|
48
|
+
response.body.destroy();
|
|
49
|
+
throw mismatch();
|
|
50
|
+
}
|
|
51
|
+
const receipt = { ok: true, status: response.status };
|
|
52
|
+
return { raw: receipt, value: receipt, bytes: 0 };
|
|
53
|
+
}
|
|
54
|
+
nativeMime(response, spec.contentTypes);
|
|
55
|
+
const bytes = await nativeBytes(response, Math.min(maxBytes, spec.maxBytes, 16 * 1024 * 1024), mutation);
|
|
56
|
+
try {
|
|
57
|
+
const raw = spec.decode === 'json' ? strictJson(bytes) : spec.decode === 'text' ? new TextDecoder('utf-8', { fatal: true }).decode(bytes) : bytes;
|
|
58
|
+
return { raw, value: project(raw, spec), bytes: bytes.length };
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw mismatch();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function authorization(http, record) {
|
|
65
|
+
validateNativeToken(record.token);
|
|
66
|
+
if (record.kind !== http.auth.type)
|
|
67
|
+
throw new CtlError('CTL_NATIVE_AUTH_INVALID', 'Stored credential uses a different auth scheme.', 'Run ctl auth --app <package> in your own terminal.', 2);
|
|
68
|
+
return http.auth.type === 'header' ? http.auth.prefix + record.token : 'GoogleLogin auth=' + record.token;
|
|
69
|
+
}
|
|
70
|
+
/** Auth replies are private protocol input. Only selected display identity survives. */
|
|
71
|
+
export async function enrollNative(http, input, transport) {
|
|
72
|
+
const auth = http.auth;
|
|
73
|
+
let token, username, identity = {};
|
|
74
|
+
if (auth.type === 'header') {
|
|
75
|
+
validateNativeToken(input.token);
|
|
76
|
+
token = input.token;
|
|
77
|
+
const reply = await transport(auth.prefix + token)({ method: 'GET', path: auth.verify.path });
|
|
78
|
+
const result = await decodeNative(reply, auth.verify.response, 65536);
|
|
79
|
+
// Never display arbitrary fields from a raw profile, even when no projection is declared.
|
|
80
|
+
if (auth.verify.response.project && 'fields' in auth.verify.response.project) {
|
|
81
|
+
if (!result.value || typeof result.value !== 'object' || Array.isArray(result.value))
|
|
82
|
+
throw mismatch();
|
|
83
|
+
for (const [key, value] of Object.entries(result.value)) {
|
|
84
|
+
if (!['string', 'number', 'boolean'].includes(typeof value))
|
|
85
|
+
throw mismatch();
|
|
86
|
+
const text = String(value);
|
|
87
|
+
if (Buffer.byteLength(text) > 512 || /[\x00-\x1f\x7f]/.test(text) || text.includes(token) || key.includes(token))
|
|
88
|
+
throw mismatch();
|
|
89
|
+
identity[key] = text;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (typeof input.username !== 'string' || !input.username || Buffer.byteLength(input.username) > 256 || /[\x00-\x1f\x7f]/.test(input.username) || typeof input.apiPassword !== 'string' || !input.apiPassword || Buffer.byteLength(input.apiPassword) > 8192)
|
|
95
|
+
throw new CtlError('CTL_NATIVE_AUTH_INVALID', 'Native username and API password are required.', 'Use the App profile API password; never use a DSM password.', 2);
|
|
96
|
+
username = input.username;
|
|
97
|
+
const body = Buffer.from(new URLSearchParams({ Email: username, Passwd: input.apiPassword }).toString());
|
|
98
|
+
const exchange = await transport()({ method: 'POST', effect: 'read', path: auth.exchangePath, headers: { 'content-type': 'application/x-www-form-urlencoded' }, body });
|
|
99
|
+
if (exchange.status !== 200) {
|
|
100
|
+
exchange.body.destroy();
|
|
101
|
+
throw mismatch();
|
|
102
|
+
}
|
|
103
|
+
nativeMime(exchange, ['text/plain']);
|
|
104
|
+
const data = await nativeBytes(exchange, 4096);
|
|
105
|
+
let text;
|
|
106
|
+
try {
|
|
107
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(data);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
throw mismatch();
|
|
111
|
+
}
|
|
112
|
+
const fields = new Map();
|
|
113
|
+
for (const line of text.replace(/\r\n/g, '\n').replace(/\n$/, '').split('\n')) {
|
|
114
|
+
const match = /^(SID|LSID|Auth)=([^\x00-\x20\x7f]+)$/.exec(line);
|
|
115
|
+
if (!match || fields.has(match[1]))
|
|
116
|
+
throw mismatch();
|
|
117
|
+
fields.set(match[1], match[2]);
|
|
118
|
+
}
|
|
119
|
+
token = fields.get('Auth') ?? '';
|
|
120
|
+
validateNativeToken(token);
|
|
121
|
+
const response = await transport('GoogleLogin auth=' + token)({ method: 'GET', path: auth.identityPath });
|
|
122
|
+
const result = await decodeNative(response, { statuses: [200], decode: 'json', maxBytes: 65536, contentTypes: ['application/json', 'text/html'] }, 65536);
|
|
123
|
+
if (!result.value || typeof result.value !== 'object' || Array.isArray(result.value) || result.value.userId !== username || username.includes(token))
|
|
124
|
+
throw mismatch();
|
|
125
|
+
identity = { userId: username };
|
|
126
|
+
}
|
|
127
|
+
return { version: 1, kind: auth.type, token, ...(username ? { username } : {}), verifiedAt: new Date().toISOString(), identity };
|
|
128
|
+
}
|
|
129
|
+
export async function nativeActionToken(http, transport) {
|
|
130
|
+
if (http.auth.type !== 'greader-client-login')
|
|
131
|
+
throw mismatch();
|
|
132
|
+
const response = await transport({ method: 'GET', path: http.auth.actionTokenPath });
|
|
133
|
+
if (response.status !== 200) {
|
|
134
|
+
response.body.destroy();
|
|
135
|
+
throw mismatch();
|
|
136
|
+
}
|
|
137
|
+
nativeMime(response, ['text/plain', 'text/html']);
|
|
138
|
+
const bytes = await nativeBytes(response, 4096);
|
|
139
|
+
let text;
|
|
140
|
+
try {
|
|
141
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes).replace(/\r?\n$/, '');
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
throw mismatch();
|
|
145
|
+
}
|
|
146
|
+
if (text.includes('<'))
|
|
147
|
+
throw mismatch();
|
|
148
|
+
validateNativeToken(text);
|
|
149
|
+
return text;
|
|
150
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NativeRegistry } from './native-types.js';
|
|
2
|
+
export type { NativeRegistry } from './native-types.js';
|
|
3
|
+
export declare function reservedNativeField(name: string): boolean;
|
|
4
|
+
export declare function validateNativeRegistry(value: any, context?: {
|
|
5
|
+
basePath: string;
|
|
6
|
+
upstreamAuthPaths: string[];
|
|
7
|
+
entry: string;
|
|
8
|
+
}): asserts value is NativeRegistry;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
2
|
+
import { schemaRegExp } from './pattern.js';
|
|
3
|
+
import { nativeShape } from './native-schemas.js';
|
|
4
|
+
import { nativeWithin } from './native-transport.js';
|
|
5
|
+
const validator = new Ajv2020.default({ strict: false, validateFormats: false, addUsedSchema: false, code: { regExp: schemaRegExp } });
|
|
6
|
+
const fail = () => { throw Error('CTL_NATIVE_REGISTRY_INVALID: native metadata violates the SDK contract.'); };
|
|
7
|
+
const parameter = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
8
|
+
export function reservedNativeField(name) { return ['token', 'authorization', 'cookie', 'synotoken', 'x-syno-token', 'remote-user', 'remote_user', 'x-remote-user', 'x-forwarded-user', 'access_token', 'api_token', 'api_key', 'password', 'passwd', 'credential'].includes(name.toLowerCase()); }
|
|
9
|
+
function clean(path, template = false) {
|
|
10
|
+
if (path === '/' || !(template ? /^\/[A-Za-z0-9_./{}~-]+$/ : /^\/[A-Za-z0-9_./~-]+$/).test(path) || path.includes('//'))
|
|
11
|
+
return false;
|
|
12
|
+
return !path.split('/').some(segment => segment === '.' || segment === '..' || /[{}]/.test(segment) && (!template || !/^\{[A-Za-z_][A-Za-z0-9_]*\}$/.test(segment)));
|
|
13
|
+
}
|
|
14
|
+
function mime(value) { return /^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(value); }
|
|
15
|
+
function schema(value) {
|
|
16
|
+
const walk = (s, depth = 0) => {
|
|
17
|
+
if (depth > 32)
|
|
18
|
+
fail();
|
|
19
|
+
if (!s || typeof s !== 'object' || Array.isArray(s))
|
|
20
|
+
return;
|
|
21
|
+
if (Object.hasOwn(s, '$id') || s.$schema && s.$schema !== 'https://json-schema.org/draft/2020-12/schema')
|
|
22
|
+
fail();
|
|
23
|
+
for (const key of ['$ref', '$dynamicRef'])
|
|
24
|
+
if (Object.hasOwn(s, key) && (typeof s[key] !== 'string' || !s[key].startsWith('#')))
|
|
25
|
+
fail();
|
|
26
|
+
for (const key of ['properties', 'patternProperties', '$defs', 'dependentSchemas'])
|
|
27
|
+
for (const child of Object.values(s[key] ?? {}))
|
|
28
|
+
walk(child, depth + 1);
|
|
29
|
+
for (const key of ['allOf', 'anyOf', 'oneOf', 'prefixItems'])
|
|
30
|
+
for (const child of s[key] ?? [])
|
|
31
|
+
walk(child, depth + 1);
|
|
32
|
+
for (const key of ['items', 'additionalProperties', 'unevaluatedProperties', 'unevaluatedItems', 'contains', 'propertyNames', 'not', 'if', 'then', 'else'])
|
|
33
|
+
if (Object.hasOwn(s, key))
|
|
34
|
+
walk(s[key], depth + 1);
|
|
35
|
+
};
|
|
36
|
+
walk(value);
|
|
37
|
+
validator.compile(value);
|
|
38
|
+
}
|
|
39
|
+
function resolve(s, root, seen = new Set()) {
|
|
40
|
+
if (!s?.$ref)
|
|
41
|
+
return s;
|
|
42
|
+
if (typeof s.$ref !== 'string' || !s.$ref.startsWith('#/') || seen.has(s.$ref))
|
|
43
|
+
fail();
|
|
44
|
+
seen.add(s.$ref);
|
|
45
|
+
let value = root;
|
|
46
|
+
for (const key of s.$ref.slice(2).split('/').map((part) => part.replaceAll('~1', '/').replaceAll('~0', '~'))) {
|
|
47
|
+
if (!value || !Object.hasOwn(value, key))
|
|
48
|
+
fail();
|
|
49
|
+
value = value[key];
|
|
50
|
+
}
|
|
51
|
+
const { $ref: _, ...rest } = s;
|
|
52
|
+
return { ...resolve(value, root, seen), ...rest };
|
|
53
|
+
}
|
|
54
|
+
function scalar(s, array = false) {
|
|
55
|
+
const types = Array.isArray(s?.type) ? s.type : [s?.type];
|
|
56
|
+
return !!types.length && types.every((type) => ['string', 'integer', 'number', 'boolean', 'null'].includes(type) || type === 'array' && array && scalar(s.items));
|
|
57
|
+
}
|
|
58
|
+
function response(spec, max) { if (spec.maxBytes > max || spec.project && spec.decode !== 'json' || spec.contentTypes.some(type => !mime(type)))
|
|
59
|
+
fail(); }
|
|
60
|
+
export function validateNativeRegistry(value, context) {
|
|
61
|
+
if (!nativeShape.registry(value))
|
|
62
|
+
fail();
|
|
63
|
+
const registry = value, n = registry.nativeHttp;
|
|
64
|
+
if (!clean(n.basePath) || !n.basePath.endsWith('/') || context && n.basePath !== context.basePath)
|
|
65
|
+
fail();
|
|
66
|
+
const prefixes = context?.upstreamAuthPaths;
|
|
67
|
+
if (prefixes) {
|
|
68
|
+
if (!prefixes.length || prefixes.length > 16)
|
|
69
|
+
fail();
|
|
70
|
+
for (let i = 0; i < prefixes.length; i++)
|
|
71
|
+
if (!clean(prefixes[i]) || prefixes[i].length > 512 || nativeWithin(prefixes[i], ['/_sso', '/_spl']) || prefixes.slice(0, i).some(prefix => nativeWithin(prefixes[i], [prefix]) || nativeWithin(prefix, [prefixes[i]])))
|
|
72
|
+
fail();
|
|
73
|
+
}
|
|
74
|
+
const page = n.auth.credentialPage;
|
|
75
|
+
if (page.startsWith('/') || /[%\\#\r\n]/.test(page))
|
|
76
|
+
fail();
|
|
77
|
+
const at = page.indexOf('?'), pagePath = at < 0 ? page : page.slice(0, at), query = at < 0 ? '' : page.slice(at + 1);
|
|
78
|
+
if (pagePath && !clean('/' + pagePath))
|
|
79
|
+
fail();
|
|
80
|
+
const keys = new Set();
|
|
81
|
+
for (const [key] of new URLSearchParams(query)) {
|
|
82
|
+
if (keys.has(key) || reservedNativeField(key) || /callback|redirect/i.test(key))
|
|
83
|
+
fail();
|
|
84
|
+
keys.add(key);
|
|
85
|
+
}
|
|
86
|
+
const route = (path, template = false) => { if (!clean(path, template) || prefixes && !nativeWithin(path, prefixes))
|
|
87
|
+
fail(); };
|
|
88
|
+
if (n.auth.type === 'header') {
|
|
89
|
+
route(n.auth.verify.path);
|
|
90
|
+
response(n.auth.verify.response, n.limits.maxResponseBytes);
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
for (const path of [n.auth.exchangePath, n.auth.identityPath, n.auth.actionTokenPath])
|
|
94
|
+
route(path);
|
|
95
|
+
const cli = new Set();
|
|
96
|
+
for (const [name, op] of Object.entries(registry.operations)) {
|
|
97
|
+
if (name === 'synopkgland.describe' || name.startsWith('identity.') || op.flow.method !== name || cli.has(op.cli.join(' ')))
|
|
98
|
+
fail();
|
|
99
|
+
cli.add(op.cli.join(' '));
|
|
100
|
+
if (context?.entry === 'admin-only' && op.permission !== 'administrators')
|
|
101
|
+
fail();
|
|
102
|
+
if (['read', 'preview'].includes(op.ai) && op.effect !== 'read')
|
|
103
|
+
fail();
|
|
104
|
+
if (op.ai === 'draft') {
|
|
105
|
+
const preview = registry.operations[op.preview];
|
|
106
|
+
if (op.effect === 'read' || !preview || preview.ai !== 'preview' || preview.effect !== 'read')
|
|
107
|
+
fail();
|
|
108
|
+
}
|
|
109
|
+
if (op.preview && op.ai !== 'draft')
|
|
110
|
+
fail();
|
|
111
|
+
if (n.auth.type === 'greader-client-login' && [n.auth.exchangePath, n.auth.actionTokenPath].includes(op.http.path))
|
|
112
|
+
fail();
|
|
113
|
+
route(op.http.path, true);
|
|
114
|
+
schema(op.paramsSchema);
|
|
115
|
+
schema(op.resultSchema);
|
|
116
|
+
const properties = Object.fromEntries(Object.entries(op.paramsSchema.properties ?? {}).map(([key, s]) => [key, resolve(s, op.paramsSchema)]));
|
|
117
|
+
const required = op.paramsSchema.required ?? [];
|
|
118
|
+
const local = op.flow.type === 'upload-http' ? op.flow.fileParam : op.flow.type === 'download-file' ? op.flow.outputParam : undefined;
|
|
119
|
+
if (local && (!required.includes(local) || properties[local]?.type !== 'string' || properties[local]?.writeOnly !== true))
|
|
120
|
+
fail();
|
|
121
|
+
for (const segment of op.http.path.split('/'))
|
|
122
|
+
if (segment.startsWith('{')) {
|
|
123
|
+
const key = segment.slice(1, -1);
|
|
124
|
+
if (!required.includes(key) || key === local || !scalar(properties[key]))
|
|
125
|
+
fail();
|
|
126
|
+
}
|
|
127
|
+
if (['GET', 'DELETE'].includes(op.http.method) && op.http.body)
|
|
128
|
+
fail();
|
|
129
|
+
const bindings = (fields, json = false, form = false) => {
|
|
130
|
+
for (const [key, b] of Object.entries(fields)) {
|
|
131
|
+
if (!parameter.test(key) || reservedNativeField(key))
|
|
132
|
+
fail();
|
|
133
|
+
if ('actionToken' in b) {
|
|
134
|
+
if (n.auth.type !== 'greader-client-login' || !form || key !== 'T')
|
|
135
|
+
fail();
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (n.auth.type === 'greader-client-login' && key === 'T')
|
|
139
|
+
fail();
|
|
140
|
+
if ('param' in b && (!properties[b.param] || b.param === local || !json && !scalar(properties[b.param], true)))
|
|
141
|
+
fail();
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
bindings(op.http.query ?? {});
|
|
145
|
+
if (op.http.body) {
|
|
146
|
+
if (Object.keys(op.http.body.fields).length > 16)
|
|
147
|
+
fail();
|
|
148
|
+
bindings(op.http.body.fields, op.http.body.encoding === 'json', op.http.body.encoding === 'form');
|
|
149
|
+
}
|
|
150
|
+
response(op.http.response, op.flow.type === 'download-file' ? n.limits.maxTransferBytes : n.limits.maxResponseBytes);
|
|
151
|
+
const f = op.flow, h = op.http;
|
|
152
|
+
if (f.type === 'invoke') {
|
|
153
|
+
if (op.kind !== 'rpc' || h.response.decode === 'binary')
|
|
154
|
+
fail();
|
|
155
|
+
}
|
|
156
|
+
else if (f.type === 'paginate-http') {
|
|
157
|
+
const p = f.pagination, first = p.mode === 'page' ? p.pageParam : p.cursorParam;
|
|
158
|
+
if (op.kind !== 'rpc' || op.effect !== 'read' || h.method !== 'GET' || h.response.decode !== 'json' || h.response.project || !parameter.test(first) || !parameter.test(p.sizeParam) || first === p.sizeParam || reservedNativeField(first) || reservedNativeField(p.sizeParam) || Object.hasOwn(h.query ?? {}, first) || Object.hasOwn(h.query ?? {}, p.sizeParam))
|
|
159
|
+
fail();
|
|
160
|
+
}
|
|
161
|
+
else if (f.type === 'ndjson') {
|
|
162
|
+
if (op.kind !== 'stream' || op.effect !== 'read' || h.method !== 'GET' || h.response.decode !== 'text' || h.response.project || !h.response.contentTypes.includes('application/x-ndjson'))
|
|
163
|
+
fail();
|
|
164
|
+
}
|
|
165
|
+
else if (f.type === 'upload-http') {
|
|
166
|
+
if (op.kind !== 'stream' || op.ai !== 'human-only' || op.effect === 'read' || h.method !== 'POST' || h.response.maxBytes > 65536 || h.response.decode === 'binary')
|
|
167
|
+
fail();
|
|
168
|
+
if (f.encoding === 'raw' && (h.body || 'part' in f))
|
|
169
|
+
fail();
|
|
170
|
+
if (f.encoding === 'multipart' && (!parameter.test(f.part) || h.body && h.body.encoding !== 'form' || Object.hasOwn(h.body?.fields ?? {}, f.part)))
|
|
171
|
+
fail();
|
|
172
|
+
}
|
|
173
|
+
else if (f.type === 'download-file') {
|
|
174
|
+
if (op.kind !== 'stream' || op.ai !== 'human-only' || op.effect !== 'read' || h.method !== 'GET' || h.response.decode !== 'binary' || h.response.maxBytes > f.maxBytes || h.response.contentTypes.some(type => !f.contentTypes.includes(type)))
|
|
175
|
+
fail();
|
|
176
|
+
}
|
|
177
|
+
if ('maxBytes' in f && local && f.maxBytes > n.limits.maxTransferBytes)
|
|
178
|
+
fail();
|
|
179
|
+
if ('contentTypes' in f && f.contentTypes.some(type => !mime(type)))
|
|
180
|
+
fail();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
3
|
+
import { schemaRegExp } from './pattern.js';
|
|
4
|
+
let contract;
|
|
5
|
+
function load() {
|
|
6
|
+
if (contract)
|
|
7
|
+
return contract;
|
|
8
|
+
const ajv = new Ajv2020.default({ strict: false, allErrors: true, validateFormats: false, addUsedSchema: false, code: { regExp: schemaRegExp } });
|
|
9
|
+
const documents = Object.fromEntries(['registry', 'descriptor', 'inventory'].map(name => [name, JSON.parse(readFileSync(new URL(`../../dist/native-http-schema/${name}.schema.json`, import.meta.url), 'utf8'))]));
|
|
10
|
+
// Relative references use SDK resource filenames. No remote loader exists.
|
|
11
|
+
const base = 'https://synopkgland.invalid/spec/api/v3/';
|
|
12
|
+
for (const [name, schema] of Object.entries(documents))
|
|
13
|
+
ajv.addSchema(schema, base + name + '.schema.json');
|
|
14
|
+
return contract = { documents, validators: Object.fromEntries(Object.keys(documents).map(name => [name, ajv.getSchema(base + name + '.schema.json')])) };
|
|
15
|
+
}
|
|
16
|
+
function check(name) { const validate = (value) => { const fn = load().validators[name], ok = fn(value); validate.errors = fn.errors; return ok; }; return validate; }
|
|
17
|
+
/** Legacy RPC/Compose imports do not load the opt-in native contract. */
|
|
18
|
+
export const nativeShape = { registry: check('registry'), descriptor: check('descriptor'), inventory: check('inventory') };
|
|
19
|
+
export function nativeFeatures() {
|
|
20
|
+
const spec = load().documents.descriptor.properties.payload.properties.features, features = spec.const ?? spec.prefixItems.map((item) => item.const);
|
|
21
|
+
const supported = new Set(['native-http-v1', 'auth-header-v1', 'auth-greader-client-login-v1', 'paginate-http-v1', 'ndjson-http-v1', 'upload-http-v1', 'download-file-v1']);
|
|
22
|
+
if (features.length !== supported.size || features.some((feature) => !supported.has(feature)))
|
|
23
|
+
throw Error('CTL_NATIVE_SDK_REQUIRED: unsupported native HTTP feature marker.');
|
|
24
|
+
return features;
|
|
25
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type WindowsAclDiagnostic } from './windows-acl.js';
|
|
2
|
+
import type { Host } from '../nas/store.js';
|
|
3
|
+
import type { NativeHttp, NativeCredential } from './native-types.js';
|
|
4
|
+
export type NativeStoreState = 'missing' | 'stored' | 'locked' | 'invalid' | 'unavailable';
|
|
5
|
+
export type NativeStoreKind = 'keychain' | 'encrypted-file';
|
|
6
|
+
export interface SecretBackend {
|
|
7
|
+
get(key: string): Promise<string | null>;
|
|
8
|
+
set(key: string, value: string): Promise<void>;
|
|
9
|
+
delete(key: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare const NATIVE_SERVICE = "synopkgland.ctl.native";
|
|
12
|
+
export declare function authDigest(http: NativeHttp, entryPolicyDigest?: string): string;
|
|
13
|
+
export declare function nativeScope(host: Pick<Host, 'name' | 'origin' | 'username'>, pkg: string, http: NativeHttp | {
|
|
14
|
+
authSha256: string;
|
|
15
|
+
}): string;
|
|
16
|
+
export declare function nativeHostScope(host: Pick<Host, 'name' | 'origin' | 'username'>): string;
|
|
17
|
+
export type KeyringEntry = {
|
|
18
|
+
getPassword(): Promise<string | null>;
|
|
19
|
+
setPassword(value: string): Promise<unknown>;
|
|
20
|
+
deleteCredential(): Promise<unknown>;
|
|
21
|
+
};
|
|
22
|
+
/** The same audited binding selects Keychain, Credential Manager or Secret Service. */
|
|
23
|
+
export declare class NativeKeychain implements SecretBackend {
|
|
24
|
+
private factory?;
|
|
25
|
+
constructor(factory?: ((service: string, key: string, options: {
|
|
26
|
+
linux: {
|
|
27
|
+
store: "secret-service";
|
|
28
|
+
};
|
|
29
|
+
}) => Promise<KeyringEntry>) | undefined);
|
|
30
|
+
private entry;
|
|
31
|
+
get(key: string): Promise<string | null>;
|
|
32
|
+
set(key: string, value: string): Promise<void>;
|
|
33
|
+
delete(key: string): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export declare class NativeCredentialStore {
|
|
36
|
+
root: string;
|
|
37
|
+
keychain: SecretBackend;
|
|
38
|
+
private unlock?;
|
|
39
|
+
private passphrase?;
|
|
40
|
+
private resolvedRoot?;
|
|
41
|
+
private get stateRoot();
|
|
42
|
+
private prepare;
|
|
43
|
+
constructor(root: string, keychain?: SecretBackend, unlock?: (() => Promise<string>) | undefined);
|
|
44
|
+
setUnlock(unlock: () => Promise<string>): void;
|
|
45
|
+
private get dir();
|
|
46
|
+
private index;
|
|
47
|
+
private locked;
|
|
48
|
+
private saveIndex;
|
|
49
|
+
private password;
|
|
50
|
+
private vaultFile;
|
|
51
|
+
private decrypt;
|
|
52
|
+
private encrypt;
|
|
53
|
+
kind(key: string): Promise<NativeStoreKind | undefined>;
|
|
54
|
+
state(key: string): Promise<NativeStoreState>;
|
|
55
|
+
status(key: string): Promise<{
|
|
56
|
+
state: NativeStoreState;
|
|
57
|
+
reason?: string;
|
|
58
|
+
detail?: WindowsAclDiagnostic;
|
|
59
|
+
}>;
|
|
60
|
+
get(key: string): Promise<NativeCredential | null>;
|
|
61
|
+
set(key: string, host: string, value: NativeCredential, store?: NativeStoreKind): Promise<void>;
|
|
62
|
+
invalid(key: string): Promise<void>;
|
|
63
|
+
private deleteRecord;
|
|
64
|
+
delete(key: string): Promise<void>;
|
|
65
|
+
removeHost(host: string): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
export declare function validateNativeToken(value: unknown): asserts value is string;
|