@wirecat/cli-core 0.694.0 → 0.696.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.
@@ -1,12 +1,43 @@
1
- import { isAbsolute } from "node:path";
2
1
  import { CliError } from "@wirecat/cli-base";
3
2
  import * as v from "valibot";
4
3
  export const CLI_CATEGORIES = ["messenger", "mail", "notes", "files", "meetings", "calendar", "crm", "other"];
5
4
  /** Types the config in the editor; it returns what it is given. */
6
5
  export const defineCli = (config) => config;
7
6
  const word = v.pipe(v.string(), v.regex(/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/, "lower-case letters and digits, words joined by one dash"));
8
- /** In the project: no absolute path, no `..`, no scheme. */
9
- const projectPath = (text) => !isAbsolute(text) && !text.split(/[\\/]/).includes("..") && !/^[a-z][a-z0-9+.-]*:/i.test(text);
7
+ // A query parameter with such a word in its name holds a secret, which the URL would put into a committed file. Whole
8
+ // words, so `design` or `keyword` stay allowed.
9
+ const SECRET_WORDS = new Set([
10
+ "key",
11
+ "apikey",
12
+ "token",
13
+ "secret",
14
+ "sig",
15
+ "signature",
16
+ "auth",
17
+ "password",
18
+ "pass",
19
+ "passwd",
20
+ "credential",
21
+ "credentials",
22
+ "session",
23
+ "sessionid",
24
+ "jwt",
25
+ "code",
26
+ ]);
27
+ const secretName = (name) => name.split(/[_\-.]+|(?<=[a-z0-9])(?=[A-Z])/).some((part) => SECRET_WORDS.has(part.toLowerCase()));
28
+ const urlOf = (text) => (URL.canParse(text) ? new URL(text) : undefined);
29
+ /** An https URL that can be committed: a host, no spaces, no user name or password, no secret in its query. */
30
+ const httpsUrl = v.pipe(v.string(), v.check((text) => !/\s/.test(text), "an https URL without spaces"), v.check((text) => {
31
+ const url = urlOf(text);
32
+ return url?.protocol === "https:" && url.hostname.length > 0;
33
+ }, "a valid https URL with a host"), v.check((text) => {
34
+ const url = urlOf(text);
35
+ return !url?.username && !url?.password;
36
+ }, "an https URL without a user name or password, which would be committed"), v.check((text) => ![...(urlOf(text)?.searchParams.keys() ?? [])].some(secretName), "an https URL without a key or token in its query, which would be committed"),
37
+ // A token handed back in the fragment (OAuth's implicit flow) would be committed too; a description needs none.
38
+ v.check((text) => !urlOf(text)?.hash, "an https URL without a #fragment"));
39
+ const dotPath = v.pipe(v.string(), v.regex(/^[^.\s]+(\.[^.\s]+)*$/, "a dot path into the answer, such as data.id"));
40
+ const verify = v.optional(v.strictObject({ operation: v.pipe(v.string(), v.minLength(1)), id: dotPath, name: v.optional(dotPath) }));
10
41
  const schema = v.strictObject({
11
42
  identity: v.strictObject({
12
43
  command: word,
@@ -17,38 +48,52 @@ const schema = v.strictObject({
17
48
  }),
18
49
  spec: v.strictObject({
19
50
  kind: v.picklist(["openapi", "swagger"]),
20
- from: v.pipe(v.string(), v.check((text) => /^https:\/\/[^/\s]+/.test(text), "the https URL the description is published at")),
51
+ from: httpsUrl,
21
52
  }),
22
53
  auth: v.variant("preset", [
23
54
  v.strictObject({ preset: v.literal("none") }),
24
- v.strictObject({ preset: v.literal("token") }),
55
+ v.strictObject({ preset: v.literal("token"), verify }),
25
56
  v.strictObject({
26
57
  preset: v.literal("oauth2-pkce"),
58
+ verify,
27
59
  scopes: v.array(v.string()),
28
60
  scheme: v.optional(v.string()),
29
- revokeUrl: v.optional(v.pipe(v.string(), v.startsWith("https://"))),
61
+ revokeUrl: v.optional(httpsUrl),
30
62
  authorizeParams: v.optional(v.record(v.string(), v.string())),
31
63
  rotating: v.optional(v.boolean()),
32
64
  redirect: v.optional(v.variant("kind", [
33
65
  v.strictObject({ kind: v.literal("loopback"), host: v.optional(v.literal("localhost")) }),
34
- v.strictObject({ kind: v.literal("relay"), url: v.pipe(v.string(), v.startsWith("https://")) }),
66
+ v.strictObject({ kind: v.literal("relay"), url: httpsUrl }),
35
67
  ])),
36
68
  }),
37
69
  ]),
38
70
  category: v.picklist(CLI_CATEGORIES),
39
71
  source: v.optional(v.looseObject({})),
40
- hooks: v.optional(v.pipe(v.string(), v.startsWith("./"), v.check(projectPath, "a module in the project"))),
72
+ hooks: v.optional(v.pipe(v.string(), v.regex(/^\.\/([^\s]*\/)?[^/\s.][^/\s]*\.[cm]?[jt]s$/, "a module in the project, such as ./src/hooks.ts"), v.check((text) => !/\.d\.[cm]?ts$/.test(text), "a module, not a .d.ts declaration file"), v.check((text) => !text.split(/[\\/]/).includes(".."), "a module in the project, without .."))),
41
73
  });
42
- /** The config checked against its shape; every error names where it is. A CRM CLI needs its source. */
74
+ /**
75
+ * The config checked against its shape; every error names where it is. A CRM CLI needs its source. `source` comes back
76
+ * unchecked: `parseCrmManifest` checks it against the generated API.
77
+ */
43
78
  export const parseCliConfig = (input) => {
44
79
  const parsed = v.safeParse(schema, input);
45
80
  if (!parsed.success) {
46
81
  const [issue] = parsed.issues;
47
- throw new CliError("configuration_error", `wirecat.config: ${v.getDotPath(issue) ?? "(root)"}: ${issue.message}`, {
82
+ // valibot's own words for a key: "Invalid key: Expected never but received …". By the issue's shape, so a value
83
+ // that is undefined inside a list or record keeps valibot's words.
84
+ const keyed = issue.type === "strict_object" || issue.type === "variant";
85
+ const message = keyed && issue.expected === "never"
86
+ ? "not a field of wirecat.config"
87
+ : keyed && issue.input === undefined
88
+ ? "missing"
89
+ : issue.message;
90
+ throw new CliError("configuration_error", `wirecat.config: ${v.getDotPath(issue) ?? "(root)"}: ${message}`, {
48
91
  reason: "manifest.invalid",
49
92
  });
50
93
  }
51
- const config = parsed.output;
94
+ // Typed, not cast: a field CliConfig needs that the schema lacks or types otherwise fails to compile (not the reverse).
95
+ const output = parsed.output;
96
+ const config = output;
52
97
  if (config.category !== "crm" && config.source)
53
98
  throw new CliError("configuration_error", "wirecat.config: source: only a crm CLI reads a source yet", {
54
99
  reason: "manifest.invalid",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.694.0",
3
+ "version": "0.696.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",