@synopackageland/cli 0.52.4 → 0.52.5

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,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.52.5
4
+
5
+ - Fix first executor approval from mobile App entries by resuming DSM's native Noise session and signing protected requests
6
+ - Honor each DSM API's advertised request format, including JSON-encoded string parameters and password confirmation tokens
7
+ - Keep the desktop AppWindow transport and existing administrator, password confirmation and task verification checks
8
+
3
9
  ## 0.52.4
4
10
 
5
11
  - Wire executor approval into mobile App entries using the same tasks, digest pins and access runtime as DSM windows
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Honor DSM API formats and resume Noise only when mobile approval needs it. Reuse
3
+ * the NAS libraries instead of shipping another implementation or login flow.
4
+ * The desktop keeps its injected AppWindow WebAPI channel.
5
+ */
6
+ export declare const MOBILE_APPROVAL_WEBAPI = "\nvar mobileApprovalCredential;\nfunction createMobileApprovalWebApi(ctx) {\n var formats = {};\n function url(path) {\n var base = new URL(ctx.files.endpoint);\n base.pathname = base.pathname.slice(0, base.pathname.indexOf(\"/webman/3rdparty/\") + 1);\n base.search = \"\"; base.hash = \"\";\n return new URL(path, base).href;\n }\n function failure() { return {code: \"dsm_119\"}; }\n function script(path) {\n return new Promise(function (resolve, reject) {\n var element = document.createElement(\"script\");\n var timer = setTimeout(function () { element.remove(); reject(failure()); }, ctx.requestTimeout || 300000);\n element.src = url(path);\n element.onload = function () { clearTimeout(timer); resolve(); };\n element.onerror = function () { clearTimeout(timer); element.remove(); reject(failure()); };\n document.head.appendChild(element);\n });\n }\n async function request(params, headers, json, path) {\n var body = new URLSearchParams();\n Object.keys(params).forEach(function (key) {\n var value = params[key];\n var routing = key === \"api\" || key === \"method\" || key === \"version\";\n body.set(key, (json && !routing) || typeof value !== \"string\" ? JSON.stringify(value) : value);\n });\n var response = await fetch(url(\"webapi/\" + (path || \"entry.cgi\")), {\n method: \"POST\", mode: \"same-origin\", credentials: \"same-origin\",\n redirect: \"error\", cache: \"no-store\", headers: headers, body: body,\n signal: AbortSignal.timeout(ctx.requestTimeout || 300000)\n });\n var reply = await response.json();\n if (response.ok && reply && reply.success === true) return reply.data;\n throw {code: \"dsm_\" + (reply && reply.error && reply.error.code || \"error\")};\n }\n async function credential() {\n var native = globalThis.SYNO && globalThis.SYNO.SDS && globalThis.SYNO.SDS.HandShake;\n if (native && native.cred) { await native.cred.WaitForReady(); return native.cred; }\n var stored;\n try { stored = JSON.parse(localStorage.getItem(\"lastHS\")); } catch (_) {}\n var remote = document.cookie.match(/(?:^|;\\s*)_SSID=([^;]+)/);\n // Older DSM and sessions without Noise retain their existing server checks.\n if (!remote || !stored || stored.account !== ctx.user.name || !stored.authtok) return;\n if (!mobileApprovalCredential) mobileApprovalCredential = (async function () {\n if (!globalThis.noise_c_wasm) {\n await script(\"scripts/noise/noise-c.js\");\n await script(\"scripts/noise/constants.js\");\n await script(\"scripts/noise/index.js\");\n }\n if (!globalThis.sodium) await script(\"scripts/noise/sodium.js\");\n if (!globalThis.synocredential) await script(\"scripts/synocredential.js/synocredential.min.js\");\n await globalThis.sodium.ready;\n var noise = await new Promise(function (resolve) { globalThis.noise_c_wasm(resolve); });\n var cred = new globalThis.synocredential(noise, {account: stored.account, authtok: stored.authtok});\n var params = cred.GetResumeParams({enable_syno_token: \"yes\"}, remote[1]);\n if (!params) throw failure();\n var resumed = await request(params, {\"X-SYNO-TOKEN\": ctx.files.token() || \"\"});\n if (!resumed || !resumed.kk_message) throw failure();\n // SetResumeResult's token updater belongs to the desktop's global session.\n // Keep this transport's token in its own credential headers instead.\n cred.SetResumeResult({kk_message: resumed.kk_message});\n cred.info.headers[\"X-SYNO-TOKEN\"] = resumed.synotoken\n ? encodeURIComponent(resumed.synotoken) : ctx.files.token() || \"\";\n return cred;\n })().catch(function (error) { mobileApprovalCredential = undefined; throw error; });\n return mobileApprovalCredential;\n }\n return async function (api, method, version, params) {\n if (!formats[api]) formats[api] = ctx.webApi(\"SYNO.API.Info\", \"query\", 1, {query: api}).then(function (info) {\n var entry = info && info[api];\n if (!entry || !/^[A-Za-z0-9_.-]+\\.cgi$/.test(entry.path)) throw {code: \"dsm_102\"};\n return entry;\n }).catch(function (error) { delete formats[api]; throw error; });\n var info = await formats[api];\n var protectedRequest = api === \"SYNO.Core.User.PasswordConfirm\" || api === \"SYNO.Core.EventScheduler.Root\" ||\n api === \"SYNO.Core.TaskScheduler.Root\";\n var cred = protectedRequest ? await credential() : undefined;\n // GetRequestHeaders generates a fresh hash/nonce for every request.\n return request(Object.assign({}, params, {api: api, method: method, version: version}),\n cred ? cred.GetRequestHeaders() : {\"X-SYNO-TOKEN\": ctx.files.token() || \"\"},\n info.requestFormat === \"JSON\", info.path);\n };\n}\n";
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Honor DSM API formats and resume Noise only when mobile approval needs it. Reuse
3
+ * the NAS libraries instead of shipping another implementation or login flow.
4
+ * The desktop keeps its injected AppWindow WebAPI channel.
5
+ */
6
+ export const MOBILE_APPROVAL_WEBAPI = `
7
+ var mobileApprovalCredential;
8
+ function createMobileApprovalWebApi(ctx) {
9
+ var formats = {};
10
+ function url(path) {
11
+ var base = new URL(ctx.files.endpoint);
12
+ base.pathname = base.pathname.slice(0, base.pathname.indexOf("/webman/3rdparty/") + 1);
13
+ base.search = ""; base.hash = "";
14
+ return new URL(path, base).href;
15
+ }
16
+ function failure() { return {code: "dsm_119"}; }
17
+ function script(path) {
18
+ return new Promise(function (resolve, reject) {
19
+ var element = document.createElement("script");
20
+ var timer = setTimeout(function () { element.remove(); reject(failure()); }, ctx.requestTimeout || 300000);
21
+ element.src = url(path);
22
+ element.onload = function () { clearTimeout(timer); resolve(); };
23
+ element.onerror = function () { clearTimeout(timer); element.remove(); reject(failure()); };
24
+ document.head.appendChild(element);
25
+ });
26
+ }
27
+ async function request(params, headers, json, path) {
28
+ var body = new URLSearchParams();
29
+ Object.keys(params).forEach(function (key) {
30
+ var value = params[key];
31
+ var routing = key === "api" || key === "method" || key === "version";
32
+ body.set(key, (json && !routing) || typeof value !== "string" ? JSON.stringify(value) : value);
33
+ });
34
+ var response = await fetch(url("webapi/" + (path || "entry.cgi")), {
35
+ method: "POST", mode: "same-origin", credentials: "same-origin",
36
+ redirect: "error", cache: "no-store", headers: headers, body: body,
37
+ signal: AbortSignal.timeout(ctx.requestTimeout || 300000)
38
+ });
39
+ var reply = await response.json();
40
+ if (response.ok && reply && reply.success === true) return reply.data;
41
+ throw {code: "dsm_" + (reply && reply.error && reply.error.code || "error")};
42
+ }
43
+ async function credential() {
44
+ var native = globalThis.SYNO && globalThis.SYNO.SDS && globalThis.SYNO.SDS.HandShake;
45
+ if (native && native.cred) { await native.cred.WaitForReady(); return native.cred; }
46
+ var stored;
47
+ try { stored = JSON.parse(localStorage.getItem("lastHS")); } catch (_) {}
48
+ var remote = document.cookie.match(/(?:^|;\\s*)_SSID=([^;]+)/);
49
+ // Older DSM and sessions without Noise retain their existing server checks.
50
+ if (!remote || !stored || stored.account !== ctx.user.name || !stored.authtok) return;
51
+ if (!mobileApprovalCredential) mobileApprovalCredential = (async function () {
52
+ if (!globalThis.noise_c_wasm) {
53
+ await script("scripts/noise/noise-c.js");
54
+ await script("scripts/noise/constants.js");
55
+ await script("scripts/noise/index.js");
56
+ }
57
+ if (!globalThis.sodium) await script("scripts/noise/sodium.js");
58
+ if (!globalThis.synocredential) await script("scripts/synocredential.js/synocredential.min.js");
59
+ await globalThis.sodium.ready;
60
+ var noise = await new Promise(function (resolve) { globalThis.noise_c_wasm(resolve); });
61
+ var cred = new globalThis.synocredential(noise, {account: stored.account, authtok: stored.authtok});
62
+ var params = cred.GetResumeParams({enable_syno_token: "yes"}, remote[1]);
63
+ if (!params) throw failure();
64
+ var resumed = await request(params, {"X-SYNO-TOKEN": ctx.files.token() || ""});
65
+ if (!resumed || !resumed.kk_message) throw failure();
66
+ // SetResumeResult's token updater belongs to the desktop's global session.
67
+ // Keep this transport's token in its own credential headers instead.
68
+ cred.SetResumeResult({kk_message: resumed.kk_message});
69
+ cred.info.headers["X-SYNO-TOKEN"] = resumed.synotoken
70
+ ? encodeURIComponent(resumed.synotoken) : ctx.files.token() || "";
71
+ return cred;
72
+ })().catch(function (error) { mobileApprovalCredential = undefined; throw error; });
73
+ return mobileApprovalCredential;
74
+ }
75
+ return async function (api, method, version, params) {
76
+ if (!formats[api]) formats[api] = ctx.webApi("SYNO.API.Info", "query", 1, {query: api}).then(function (info) {
77
+ var entry = info && info[api];
78
+ if (!entry || !/^[A-Za-z0-9_.-]+\\.cgi$/.test(entry.path)) throw {code: "dsm_102"};
79
+ return entry;
80
+ }).catch(function (error) { delete formats[api]; throw error; });
81
+ var info = await formats[api];
82
+ var protectedRequest = api === "SYNO.Core.User.PasswordConfirm" || api === "SYNO.Core.EventScheduler.Root" ||
83
+ api === "SYNO.Core.TaskScheduler.Root";
84
+ var cred = protectedRequest ? await credential() : undefined;
85
+ // GetRequestHeaders generates a fresh hash/nonce for every request.
86
+ return request(Object.assign({}, params, {api: api, method: method, version: version}),
87
+ cred ? cred.GetRequestHeaders() : {"X-SYNO-TOKEN": ctx.files.token() || ""},
88
+ info.requestFormat === "JSON", info.path);
89
+ };
90
+ }
91
+ `;
@@ -13,6 +13,7 @@ import { CLI_VERSION } from '../cli-version.js';
13
13
  import { isPortalAlias } from '../portal/alias.js';
14
14
  import { renderExecutorApproval } from './executor-approval.js';
15
15
  import { CALL_RETRY_RUNTIME } from './call-retry.js';
16
+ import { MOBILE_APPROVAL_WEBAPI } from './mobile-approval-webapi.js';
16
17
  const schema = JSON.parse(readFileSync(new URL('../../schema/mobile.v1.schema.json', import.meta.url), 'utf8'));
17
18
  const ajv = new Ajv({ allErrors: true });
18
19
  const validate = ajv.compile(schema);
@@ -83,12 +84,14 @@ export async function renderMobileAssets(manifest, uiDigest, moduleVersion) {
83
84
  ? `var executorApproval = {};
84
85
  ${renderExecutorApproval('executorApproval', manifest)}
85
86
  ${CALL_RETRY_RUNTIME}
86
- function attachExecutor(ctx) {
87
+ ${MOBILE_APPROVAL_WEBAPI}
88
+ function attachExecutor(ctx, desktopWebApi) {
87
89
  ctx.call = createRetryCall(ctx.call);
90
+ var send = desktopWebApi || createMobileApprovalWebApi(ctx);
88
91
  ctx.executor = executorApproval.createExecutorAccess({
89
92
  user: function () { return {name: ctx.user?.name, admin: ctx.user?.isAdmin === true}; },
90
- send: ctx.webApi,
91
- confirmPassword: executorApproval.createPasswordConfirm(ctx.webApi),
93
+ send: send,
94
+ confirmPassword: executorApproval.createPasswordConfirm(send),
92
95
  call: ctx.call
93
96
  });
94
97
  return ctx;
@@ -125,7 +128,7 @@ function attachExecutor(ctx) {
125
128
  const bundled = await build({ stdin: { contents: source, resolveDir: manifest.root, sourcefile: 'synopkgland-entry.generated.js' }, bundle: true, write: false, format: 'esm', platform: 'browser', target: 'es2022', minify: true, keepNames: true, plugins: [{ name: 'app-import', setup(api) { api.onResolve({ filter: /[?]v=/ }, args => ({ path: args.path, external: true })); } }], legalComments: 'none' });
126
129
  files.set('entry.js', Buffer.from(bundled.outputFiles[0].text));
127
130
  const contextSource = approval
128
- ? `import {createAppContext as createContext} from ${json(entryModule(manifest))};\n${approval}export function createAppContext(options) { return attachExecutor(createContext(options)); }`
131
+ ? `import {createAppContext as createContext} from ${json(entryModule(manifest))};\n${approval}export function createAppContext(options) { return attachExecutor(createContext(options), options.webApi); }`
129
132
  : `export {createAppContext} from ${json(entryModule(manifest))}`;
130
133
  const context = await build({ stdin: { contents: contextSource, resolveDir: manifest.root, sourcefile: 'synopkgland-context.generated.js' }, bundle: true, write: false, format: 'esm', platform: 'browser', target: 'es2022', minify: true, keepNames: true, legalComments: 'none' });
131
134
  files.set('entry-context.js', Buffer.from(context.outputFiles[0].text));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synopackageland/cli",
3
- "version": "0.52.4",
3
+ "version": "0.52.5",
4
4
  "description": "Syno Package Land Developer Kit CLI",
5
5
  "type": "module",
6
6
  "bin": {