@heybox/hb-sdk 0.6.7-alpha.1 → 0.6.7-alpha.2

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/dist/vite.esm.js CHANGED
@@ -1,11 +1,10 @@
1
1
  import { readFileSync, existsSync, writeFileSync, mkdirSync, readdirSync } from 'node:fs';
2
2
  import path from 'node:path';
3
- import { AsyncLocalStorage } from 'node:async_hooks';
4
3
 
5
4
  /** 构建时替换为当前发布包的实际版本。 */
6
5
  const HB_SDK_VERSION = typeof undefined === 'string'
7
6
  ? undefined
8
- : '0.6.7-alpha.1';
7
+ : '0.6.7-alpha.2';
9
8
 
10
9
  var re = {exports: {}};
11
10
 
@@ -11430,13 +11429,17 @@ function walk$1(node, visit) {
11430
11429
 
11431
11430
  const FORBIDDEN_RESOURCE_HINTS = new Set(['dns-prefetch', 'preconnect', 'prerender']);
11432
11431
  const RELATIVE_URL_BASE = new URL('https://heybox-relative.invalid/');
11432
+ const OFFICIAL_RESOURCE_ROOT_DOMAINS = ['xiaoheihe.cn', 'max-c.com', 'debugmode.cn', 'maxjia.com'];
11433
+ const OFFICIAL_RESOURCE_SOURCES = OFFICIAL_RESOURCE_ROOT_DOMAINS.flatMap(rootDomain => [
11434
+ `https://${rootDomain}`,
11435
+ `https://*.${rootDomain}`,
11436
+ ]);
11433
11437
  const URL_ATTRIBUTES = {
11434
11438
  audio: ['src'],
11435
11439
  embed: ['src'],
11436
11440
  iframe: ['src'],
11437
11441
  img: ['src'],
11438
11442
  input: ['src'],
11439
- link: ['href'],
11440
11443
  object: ['data'],
11441
11444
  script: ['src'],
11442
11445
  source: ['src', 'srcset'],
@@ -11446,11 +11449,11 @@ const URL_ATTRIBUTES = {
11446
11449
  const PRODUCTION_CSP = [
11447
11450
  "default-src 'none'",
11448
11451
  "connect-src 'none'",
11449
- "script-src 'self' 'unsafe-inline'",
11450
- "style-src 'self' 'unsafe-inline'",
11451
- "img-src 'self' data: blob:",
11452
- "font-src 'self' data:",
11453
- "media-src 'self' blob:",
11452
+ `script-src 'self' 'unsafe-inline' ${OFFICIAL_RESOURCE_SOURCES.join(' ')}`,
11453
+ `style-src 'self' 'unsafe-inline' ${OFFICIAL_RESOURCE_SOURCES.join(' ')}`,
11454
+ `img-src 'self' data: blob: ${OFFICIAL_RESOURCE_SOURCES.join(' ')}`,
11455
+ `font-src 'self' data: ${OFFICIAL_RESOURCE_SOURCES.join(' ')}`,
11456
+ `media-src 'self' blob: ${OFFICIAL_RESOURCE_SOURCES.join(' ')}`,
11454
11457
  "manifest-src 'self'",
11455
11458
  "frame-src 'none'",
11456
11459
  "child-src 'none'",
@@ -11469,9 +11472,6 @@ function enforceMiniappHtmlPolicy(html, options = {}) {
11469
11472
  removePlatformCspMarkers(document);
11470
11473
  walk(document, element => validateElement(element));
11471
11474
  injectMiniappRuntimeGate(document);
11472
- if (options.skipPlatformCsp) {
11473
- return serialize(document);
11474
- }
11475
11475
  const content = options.hmrWebSocketUrl
11476
11476
  ? PRODUCTION_CSP.replace("connect-src 'none'", `connect-src ${options.hmrWebSocketUrl}`)
11477
11477
  : PRODUCTION_CSP;
@@ -11508,19 +11508,25 @@ function validateElement(element) {
11508
11508
  if (forbidden) {
11509
11509
  throw new Error(`index.html 不允许 rel="${forbidden}"`);
11510
11510
  }
11511
- if (rels.includes('preload') || rels.includes('modulepreload')) {
11511
+ if (rels.includes('stylesheet')) {
11512
+ validateResourceUrl(attrs.get('href'), '<link rel="stylesheet" href>', false, true);
11513
+ }
11514
+ else if (rels.includes('preload') || rels.includes('modulepreload')) {
11512
11515
  validateRelativeUrl(attrs.get('href'), `<link rel="${rels.includes('modulepreload') ? 'modulepreload' : 'preload'}" href>`, false);
11513
11516
  }
11517
+ else {
11518
+ validateRelativeUrl(attrs.get('href'), '<link href>', false);
11519
+ }
11514
11520
  }
11515
11521
  for (const attribute of URL_ATTRIBUTES[tagName] ?? []) {
11516
11522
  const value = attrs.get(attribute);
11517
11523
  if (attribute === 'srcset') {
11518
11524
  for (const candidate of (value ?? '').split(',').map(item => item.trim().split(/\s+/)[0]).filter(Boolean)) {
11519
- validateRelativeUrl(candidate, `<${tagName} ${attribute}>`, true);
11525
+ validateResourceUrl(candidate, `<${tagName} ${attribute}>`, true, supportsOfficialResourceUrl(tagName, attribute));
11520
11526
  }
11521
11527
  }
11522
11528
  else {
11523
- validateRelativeUrl(value, `<${tagName} ${attribute}>`, true);
11529
+ validateResourceUrl(value, `<${tagName} ${attribute}>`, true, supportsOfficialResourceUrl(tagName, attribute));
11524
11530
  }
11525
11531
  }
11526
11532
  }
@@ -11535,16 +11541,43 @@ function removePlatformCspMarkers(node) {
11535
11541
  for (const child of node.childNodes)
11536
11542
  removePlatformCspMarkers(child);
11537
11543
  }
11538
- function validateRelativeUrl(value, label, allowDataBlob) {
11544
+ function supportsOfficialResourceUrl(tagName, attribute) {
11545
+ return ((tagName === 'script' && attribute === 'src') ||
11546
+ (tagName === 'img' && attribute === 'src') ||
11547
+ (tagName === 'source' && (attribute === 'src' || attribute === 'srcset')) ||
11548
+ (tagName === 'audio' && attribute === 'src') ||
11549
+ (tagName === 'track' && attribute === 'src') ||
11550
+ (tagName === 'video' && (attribute === 'src' || attribute === 'poster')));
11551
+ }
11552
+ function validateResourceUrl(value, label, allowDataBlob, allowOfficialResourceUrl) {
11539
11553
  if (!value || value.startsWith('#'))
11540
11554
  return;
11541
11555
  const normalized = value.trim().toLowerCase();
11542
11556
  if (allowDataBlob && (normalized.startsWith('data:') || normalized.startsWith('blob:')))
11543
11557
  return;
11558
+ if (allowOfficialResourceUrl && isOfficialResourceUrl(value))
11559
+ return;
11544
11560
  if (!isRelativeUrl(value, normalized)) {
11545
11561
  throw new Error(`${label} 必须使用相对路径${allowDataBlob ? '或明确的 data:/blob: URL' : ''}:${value}`);
11546
11562
  }
11547
11563
  }
11564
+ function validateRelativeUrl(value, label, allowDataBlob) {
11565
+ validateResourceUrl(value, label, allowDataBlob, false);
11566
+ }
11567
+ function isOfficialResourceUrl(value) {
11568
+ try {
11569
+ const url = new URL(value);
11570
+ if (url.protocol !== 'https:' || url.username || url.password)
11571
+ return false;
11572
+ const hostname = url.hostname.toLowerCase();
11573
+ if (hostname.endsWith('.'))
11574
+ return false;
11575
+ return OFFICIAL_RESOURCE_ROOT_DOMAINS.some(rootDomain => hostname === rootDomain || hostname.endsWith(`.${rootDomain}`));
11576
+ }
11577
+ catch {
11578
+ return false;
11579
+ }
11580
+ }
11548
11581
  function isRelativeUrl(value, normalized) {
11549
11582
  const browserNormalized = normalized.replace(/[\t\n\r]/g, '');
11550
11583
  if (browserNormalized.includes('\\') || browserNormalized.startsWith('//'))
@@ -11581,19 +11614,9 @@ function walk(node, visit) {
11581
11614
  walk(node.content, visit);
11582
11615
  }
11583
11616
 
11584
- const HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN_ENV = 'HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN';
11585
- const HB_SDK_RUNTIME_PERMISSION_CONTEXT_ENV = 'HB_SDK_RUNTIME_PERMISSION_CONTEXT';
11586
- const VERIFIED_RUNTIME_PERMISSION_CONTEXT = 'verified';
11587
- new AsyncLocalStorage();
11588
- Promise.resolve();
11589
- function shouldSkipMiniappPlatformCspFromEnv(env = process.env) {
11590
- return env[HB_SDK_RUNTIME_PERMISSION_CONTEXT_ENV] === VERIFIED_RUNTIME_PERMISSION_CONTEXT && env[HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN_ENV] === '1';
11591
- }
11592
-
11593
11617
  const sdkVersionPlaceholder = ['__HB_SDK', 'VERSION__'].join('_');
11594
11618
  const sdkVersionBuildConstant = ['__HB_SDK_BUILD', 'VERSION__'].join('_');
11595
11619
  function miniappManifest() {
11596
- const skipPlatformCsp = shouldSkipMiniappPlatformCspFromEnv();
11597
11620
  let root = process.cwd();
11598
11621
  let outDir = 'dist';
11599
11622
  let command = 'build';
@@ -11625,7 +11648,6 @@ function miniappManifest() {
11625
11648
  transformIndexHtml(html) {
11626
11649
  return enforceMiniappHtmlPolicy(html, {
11627
11650
  ...(command === 'serve' ? { hmrWebSocketUrl } : {}),
11628
- skipPlatformCsp,
11629
11651
  });
11630
11652
  },
11631
11653
  async closeBundle() {
@@ -11648,7 +11670,7 @@ function miniappManifest() {
11648
11670
  if (!htmlFiles.includes(indexHtmlPath)) {
11649
11671
  throw new Error('构建产物必须包含入口 dist/index.html');
11650
11672
  }
11651
- writeFileSync(indexHtmlPath, enforceMiniappHtmlPolicy(readFileSync(indexHtmlPath, 'utf8'), { skipPlatformCsp }));
11673
+ writeFileSync(indexHtmlPath, enforceMiniappHtmlPolicy(readFileSync(indexHtmlPath, 'utf8')));
11652
11674
  const manifestPath = path.join(outputRoot, 'manifest.json');
11653
11675
  mkdirSync(path.dirname(manifestPath), { recursive: true });
11654
11676
  writeFileSync(manifestPath, renderMiniappManifest({ version, sdkVersion }));
@@ -11690,7 +11712,7 @@ function resolveHmrWebSocketUrl(resolved) {
11690
11712
  if (hmr === false)
11691
11713
  return undefined;
11692
11714
  const options = typeof hmr === 'object' ? hmr : {};
11693
- const protocol = options.protocol ?? 'ws';
11715
+ const protocol = options.protocol ?? (resolved.server.https ? 'wss' : 'ws');
11694
11716
  const configuredHost = options.host ?? resolved.server.host;
11695
11717
  const host = typeof configuredHost === 'string' && configuredHost !== '0.0.0.0' ? configuredHost : '127.0.0.1';
11696
11718
  const port = options.clientPort ?? options.port ?? resolved.server.port;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heybox/hb-sdk",
3
- "version": "0.6.7-alpha.1",
3
+ "version": "0.6.7-alpha.2",
4
4
  "sideEffects": [
5
5
  "./src/index.ts",
6
6
  "./src/core/singleton.ts",
@@ -19,7 +19,7 @@
19
19
  ## Package metadata
20
20
 
21
21
  - Package: `@heybox/hb-sdk`
22
- - Version at generation time: `0.6.7-alpha.1`
22
+ - Version at generation time: `0.6.7-alpha.2`
23
23
  - Public root export: `@heybox/hb-sdk`
24
24
  - Protocol export: `@heybox/hb-sdk/protocol`
25
25
  - Vite plugin export: `@heybox/hb-sdk/vite`
@@ -204,7 +204,6 @@ import { HB_SDK_VERSION } from '../core/version';
204
204
  import { renderMiniappManifest, validateMiniappPackageVersionForBuild } from '../miniapp-manifest/schema';
205
205
  import { readMiniappVersionFromPackageJson } from '../miniapp-manifest/node';
206
206
  import { enforceMiniappHtmlPolicy } from './html-policy';
207
- import { shouldSkipMiniappPlatformCspFromEnv } from './runtime-permission-env';
208
207
 
209
208
  type MiniappManifestPlugin = {
210
209
  name: string;
@@ -233,6 +232,7 @@ interface MiniappManifestResolvedConfig {
233
232
  };
234
233
  server: {
235
234
  host?: string | boolean;
235
+ https?: boolean | Record<string, unknown>;
236
236
  port?: number;
237
237
  hmr?:
238
238
  | boolean
@@ -249,7 +249,6 @@ const sdkVersionPlaceholder = ['__HB_SDK', 'VERSION__'].join('_');
249
249
  const sdkVersionBuildConstant = ['__HB_SDK_BUILD', 'VERSION__'].join('_');
250
250
 
251
251
  export function miniappManifest(): MiniappManifestPlugin {
252
- const skipPlatformCsp = shouldSkipMiniappPlatformCspFromEnv();
253
252
  let root = process.cwd();
254
253
  let outDir = 'dist';
255
254
  let command: 'build' | 'serve' = 'build';
@@ -282,7 +281,6 @@ export function miniappManifest(): MiniappManifestPlugin {
282
281
  transformIndexHtml(html) {
283
282
  return enforceMiniappHtmlPolicy(html, {
284
283
  ...(command === 'serve' ? { hmrWebSocketUrl } : {}),
285
- skipPlatformCsp,
286
284
  });
287
285
  },
288
286
  async closeBundle() {
@@ -308,7 +306,7 @@ export function miniappManifest(): MiniappManifestPlugin {
308
306
  if (!htmlFiles.includes(indexHtmlPath)) {
309
307
  throw new Error('构建产物必须包含入口 dist/index.html');
310
308
  }
311
- writeFileSync(indexHtmlPath, enforceMiniappHtmlPolicy(readFileSync(indexHtmlPath, 'utf8'), { skipPlatformCsp }));
309
+ writeFileSync(indexHtmlPath, enforceMiniappHtmlPolicy(readFileSync(indexHtmlPath, 'utf8')));
312
310
 
313
311
  const manifestPath = path.join(outputRoot, 'manifest.json');
314
312
  mkdirSync(path.dirname(manifestPath), { recursive: true });
@@ -351,7 +349,7 @@ function resolveHmrWebSocketUrl(resolved: MiniappManifestResolvedConfig) {
351
349
  const hmr = resolved.server.hmr;
352
350
  if (hmr === false) return undefined;
353
351
  const options = typeof hmr === 'object' ? hmr : {};
354
- const protocol = options.protocol ?? 'ws';
352
+ const protocol = options.protocol ?? (resolved.server.https ? 'wss' : 'ws');
355
353
  const configuredHost = options.host ?? resolved.server.host;
356
354
  const host = typeof configuredHost === 'string' && configuredHost !== '0.0.0.0' ? configuredHost : '127.0.0.1';
357
355
  const port = options.clientPort ?? options.port ?? resolved.server.port;
@@ -386,11 +384,11 @@ export default defineConfig({
386
384
 
387
385
  工坊小程序构建产物只能在兼容的小黑盒 Runtime 中启动。普通浏览器直接打开时不会执行标准业务脚本,并会提示在小黑盒 APP 内打开。项目应使用标准 Vite module 入口。
388
386
 
389
- 插件只接受单页应用:输出目录只能存在入口 `index.html`。`hb-sdk dev` `hb-sdk remote deploy` 会读取绑定小程序的远端 Runtime 权限;仅当 `network.request` 已启用且 `useOfficialDomain=true` 时跳过平台 CSP,否则 build 会把平台 CSP 插入 `<head>` 首位。直接运行 `vite build`、匿名 dev、非法快照或远端权限读取失败时均继续注入。已有 CSP 始终原样保留;注入平台 CSP 时,两份策略按浏览器交集生效。
387
+ 插件只接受单页应用:输出目录只能存在入口 `index.html`。builddev 都会把平台 CSP 插入 `<head>` 首位,已有 CSP 始终原样保留;两份策略按浏览器交集生效。平台 CSP 在脚本、样式、图片、字体和媒体指令中放行 `xiaoheihe.cn`、`max-c.com`、`debugmode.cn`、`maxjia.com` HTTPS 根域及子域;入口 HTML 中相应资源标签也可使用这些官方 HTTPS URL。
390
388
 
391
- 平台 CSP 会禁止 `fetch`、XHR、WebSocket、EventSource、Beacon、Worker、iframe、表单和对象加载等浏览器原生出口;dev 只额外放行当前 Vite 的精确 HMR WebSocket 地址。跳过平台 CSP 时,这些能力不再由 SDK 构建产物统一限制,但需要宿主授权的业务网络请求仍应使用 `network.request()`。本地 Mock 权限覆盖不会改变构建 CSP,构建判定始终以远端权限快照为准。
389
+ 平台 CSP 会禁止 `fetch`、XHR、WebSocket、EventSource、Beacon、Worker、iframe、表单和对象加载等浏览器原生出口;`connect-src` 始终为 `'none'`,dev 只额外放行当前 Vite 的精确 HMR WebSocket 地址。需要宿主授权的业务网络请求仍应使用 `network.request()`。
392
390
 
393
- meta refresh、外部 anchor、`dns-prefetch`、`preconnect`、`prerender`、外部资源 URL 和额外 HTML 会使 build 失败。内联 script/style 允许,但 `unsafe-eval` 不允许。`dev`、`hb-sdk build` 和手动 Vite build 都不请求远端最低 SDK 版本;构建仍会把当前 `sdkVersion` 写入 Manifest,`hb-sdk remote deploy` 仍校验 Manifest 结构并经过服务端 precheck / submit-audit 策略。
391
+ meta refresh、外部 anchor、`dns-prefetch`、`preconnect`、`prerender`、非官方外部资源 URL 和额外 HTML 会使 build 失败。内联 script/style 允许,但 `unsafe-eval` 不允许。`dev`、`hb-sdk build` 和手动 Vite build 都不请求远端最低 SDK 版本;构建仍会把当前 `sdkVersion` 写入 Manifest,`hb-sdk remote deploy` 仍校验 Manifest 结构并经过服务端 precheck / submit-audit 策略。
394
392
 
395
393
  失败与警告语义:
396
394
 
package/skill/skill.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "hb-sdk",
3
- "skillVersion": "0.6.7-alpha.1+skill.00dab20ae3bc",
3
+ "skillVersion": "0.6.7-alpha.2+skill.ed2b204ea04c",
4
4
  "sdk": {
5
5
  "package": "@heybox/hb-sdk",
6
- "version": "0.6.7-alpha.1",
7
- "compatibility": "0.6.7-alpha.1"
6
+ "version": "0.6.7-alpha.2",
7
+ "compatibility": "0.6.7-alpha.2"
8
8
  },
9
9
  "source": "https://open.xiaoheihe.cn/agent-skills/hb-sdk",
10
- "integrity": "sha256-00dab20ae3bceff225037abb08cf86b24daee2d7b2002aa90e720492ef8b58f8"
10
+ "integrity": "sha256-ed2b204ea04c904a30c99cfc1c4a60833dde694b9a5974bbc885f2b751224e68"
11
11
  }
@@ -1,5 +1,4 @@
1
1
  export declare const PRODUCTION_CSP: string;
2
2
  export declare function enforceMiniappHtmlPolicy(html: string, options?: {
3
3
  hmrWebSocketUrl?: string;
4
- skipPlatformCsp?: boolean;
5
4
  }): string;
@@ -22,6 +22,7 @@ interface MiniappManifestResolvedConfig {
22
22
  };
23
23
  server: {
24
24
  host?: string | boolean;
25
+ https?: boolean | Record<string, unknown>;
25
26
  port?: number;
26
27
  hmr?: boolean | {
27
28
  clientPort?: number;
@@ -1,9 +0,0 @@
1
- export declare const HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN_ENV = "HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN";
2
- export declare const HB_SDK_RUNTIME_PERMISSION_CONTEXT_ENV = "HB_SDK_RUNTIME_PERMISSION_CONTEXT";
3
- export declare function shouldSkipMiniappPlatformCspFromEnv(env?: NodeJS.ProcessEnv): boolean;
4
- export declare function createMiniappPlatformCspEnv(env: NodeJS.ProcessEnv, skipPlatformCsp: boolean): {
5
- HB_SDK_RUNTIME_PERMISSION_CONTEXT: string;
6
- HB_SDK_RUNTIME_USE_OFFICIAL_DOMAIN: string;
7
- TZ?: string | undefined;
8
- };
9
- export declare function withMiniappPlatformCspEnv<T>(skipPlatformCsp: boolean, action: () => Promise<T>, env?: NodeJS.ProcessEnv): Promise<T>;