@dongnanyang/deploy-cli 0.5.2 → 0.5.3

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/bin.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const argv = process.argv.slice(2);
3
3
  if (argv.length === 0 || argv[0] === "tui") await import("./main.js");
4
4
  else {
5
- const cli = await import("./cli-BHTqsfkl.js");
5
+ const cli = await import("./cli-T37L9fUN.js");
6
6
  process.exitCode = await cli.main(argv);
7
7
  }
8
8
  //#endregion
@@ -459,6 +459,10 @@ function removeProject(cfg, name) {
459
459
  const CONF_PREFIX = "deploy-cli-";
460
460
  const CONF_SUFFIX = ".conf";
461
461
  const SLUG_RE = /^[a-z0-9-]+$/;
462
+ /** 渲染进 conf 的字段形状校验(防 conf 注入):域名 / 绝对路径 / host:port,统一排除 conf 元字符与空白 */
463
+ const DOMAIN_RE = /^(\*\.)?[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)*$/;
464
+ const ABS_PATH_RE = /^\/[A-Za-z0-9._/-]*$/;
465
+ const UPSTREAM_RE = /^(\[[0-9a-fA-F:.]+\]|[A-Za-z0-9.-]+):([0-9]{1,5})$/;
462
466
  const DEFAULT_SITES_AVAILABLE = "/etc/nginx/sites-available";
463
467
  const DEFAULT_SITES_ENABLED = "/etc/nginx/sites-enabled";
464
468
  const DEFAULT_CONF_D = "/etc/nginx/conf.d";
@@ -482,8 +486,19 @@ function validateSlug(slug) {
482
486
  function validateSpec(spec) {
483
487
  validateSlug(spec.slug);
484
488
  if (!spec.serverName?.trim()) throw new Error("NginxSiteSpec 缺少 serverName");
489
+ if (!DOMAIN_RE.test(spec.serverName)) throw new Error(`NginxSiteSpec.serverName 非法: ${JSON.stringify(spec.serverName)},仅允许域名字符(字母数字.-,可选 *. 前缀;禁止 conf 元字符与空白)`);
485
490
  if ((spec.root !== void 0 && spec.root !== "") === (spec.upstream !== void 0 && spec.upstream !== "")) throw new Error("NginxSiteSpec 需要且仅需要 root(static 型)或 upstream(代理型)之一");
491
+ if (spec.root !== void 0 && spec.root !== "") validateAbsPath("root", spec.root);
492
+ if (spec.upstream !== void 0 && spec.upstream !== "") {
493
+ const match = spec.upstream.match(UPSTREAM_RE);
494
+ if (!match || Number(match[2]) < 1 || Number(match[2]) > 65535) throw new Error(`NginxSiteSpec.upstream 非法: ${JSON.stringify(spec.upstream)},须为 host:port(端口 1-65535;禁止 conf 元字符与空白)`);
495
+ }
486
496
  if (spec.ssl === "custom" && (!spec.certPath || !spec.keyPath)) throw new Error("ssl:custom 需要 certPath 与 keyPath");
497
+ if (spec.certPath) validateAbsPath("certPath", spec.certPath);
498
+ if (spec.keyPath) validateAbsPath("keyPath", spec.keyPath);
499
+ }
500
+ function validateAbsPath(field, value) {
501
+ if (!ABS_PATH_RE.test(value)) throw new Error(`NginxSiteSpec.${field} 非法: ${JSON.stringify(value)},须为绝对路径且不含 conf 元字符与空白`);
487
502
  }
488
503
  /** 失败信息:命令原文 + exit 码 + stderr 摘要(包裹为 ExecError,附完整结果) */
489
504
  function execFailure(cmd, args, result, note) {
@@ -558,14 +573,28 @@ function createNginx(runner = run, paths) {
558
573
  const link = sitesEnabled ? join$1(sitesEnabled, confName(slug)) : void 0;
559
574
  const linkCreated = link ? !pathExists(link) : false;
560
575
  if (link && linkCreated) symlinkSync(conf, link);
561
- const test = await runner("nginx", ["-t"]);
562
- if (test.code !== 0) {
576
+ const rollback = () => {
563
577
  if (existed) renameSync(bak, conf);
564
578
  else unlinkSync(conf);
565
579
  if (link && linkCreated) unlinkSync(link);
580
+ };
581
+ let test;
582
+ try {
583
+ test = await runner("nginx", ["-t"]);
584
+ } catch (error) {
585
+ rollback();
586
+ throw error;
587
+ }
588
+ if (test.code !== 0) {
589
+ rollback();
566
590
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚站点配置 ${conf}`);
567
591
  }
568
- const reload = await runner("nginx", ["-s", "reload"]);
592
+ let reload;
593
+ try {
594
+ reload = await runner("nginx", ["-s", "reload"]);
595
+ } catch (error) {
596
+ throw error;
597
+ }
569
598
  if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, `nginx reload 失败,保留备份 ${bak} 供人工恢复`);
570
599
  if (existed) unlinkSync(bak);
571
600
  }
@@ -629,13 +658,27 @@ function createNginx(runner = run, paths) {
629
658
  const hadLink = link ? pathExists(link) : false;
630
659
  renameSync(conf, bak);
631
660
  if (link && hadLink) unlinkSync(link);
632
- const test = await runner("nginx", ["-t"]);
633
- if (test.code !== 0) {
661
+ const rollback = () => {
634
662
  renameSync(bak, conf);
635
663
  if (link && hadLink) symlinkSync(conf, link);
664
+ };
665
+ let test;
666
+ try {
667
+ test = await runner("nginx", ["-t"]);
668
+ } catch (error) {
669
+ rollback();
670
+ throw error;
671
+ }
672
+ if (test.code !== 0) {
673
+ rollback();
636
674
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚删除操作 ${conf}`);
637
675
  }
638
- const reload = await runner("nginx", ["-s", "reload"]);
676
+ let reload;
677
+ try {
678
+ reload = await runner("nginx", ["-s", "reload"]);
679
+ } catch (error) {
680
+ throw error;
681
+ }
639
682
  if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, `nginx reload 失败,保留备份 ${bak} 供人工恢复`);
640
683
  unlinkSync(bak);
641
684
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongnanyang/deploy-cli",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Local CLI TUI for SSH tunnels & deploy scripts",
5
5
  "type": "module",
6
6
  "bin": {