@exulu/backend 4.3.2 → 5.0.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.
@@ -0,0 +1,110 @@
1
+ import { buildSkillEnv } from "./skill-env";
2
+
3
+ const base = { PATH: "/usr/bin", HOME: "/root", LANG: "de_DE.UTF-8" };
4
+
5
+ test("strips inventoried platform secrets", () => {
6
+ const { env, strippedSecretNames } = buildSkillEnv({
7
+ processEnv: { ...base, NEXTAUTH_SECRET: "s3cret", LITELLM_MASTER_KEY: "sk-0" },
8
+ grantedVariables: {},
9
+ });
10
+ expect(env.NEXTAUTH_SECRET).toBeUndefined();
11
+ expect(env.LITELLM_MASTER_KEY).toBeUndefined();
12
+ expect(strippedSecretNames).toEqual(["LITELLM_MASTER_KEY", "NEXTAUTH_SECRET"]);
13
+ });
14
+
15
+ test("a canary secret never reaches a skill", () => {
16
+ const { env } = buildSkillEnv({
17
+ processEnv: { ...base, NEXTAUTH_SECRET: "EXULU_CANARY_VALUE" },
18
+ grantedVariables: {},
19
+ });
20
+ expect(JSON.stringify(env)).not.toContain("EXULU_CANARY_VALUE");
21
+ });
22
+
23
+ test("third-party runtime settings pass through untouched", () => {
24
+ const { env } = buildSkillEnv({
25
+ processEnv: { ...base, PYMUPDF_MESSAGE: "fd:2", HF_HUB_ENABLE_HF_TRANSFER: "1" },
26
+ grantedVariables: {},
27
+ });
28
+ expect(env.PYMUPDF_MESSAGE).toBe("fd:2");
29
+ expect(env.HF_HUB_ENABLE_HF_TRANSFER).toBe("1");
30
+ expect(env.LANG).toBe("de_DE.UTF-8");
31
+ });
32
+
33
+ test("only granted variables are injected", () => {
34
+ const { env, grantedNames } = buildSkillEnv({
35
+ processEnv: base,
36
+ grantedVariables: { JIRA_CLIENT_ID: "abc" },
37
+ });
38
+ expect(env.JIRA_CLIENT_ID).toBe("abc");
39
+ expect(grantedNames).toEqual(["JIRA_CLIENT_ID"]);
40
+ });
41
+
42
+ test("a granted variable colliding with a base runtime key is skipped", () => {
43
+ const { env, skippedNames } = buildSkillEnv({
44
+ processEnv: base,
45
+ grantedVariables: { PATH: "/evil" },
46
+ });
47
+ expect(env.PATH).toBe("/usr/bin");
48
+ expect(skippedNames).toEqual(["PATH"]);
49
+ });
50
+
51
+ test("a granted variable named like a platform secret carries the admin value, never the platform one", () => {
52
+ const { env } = buildSkillEnv({
53
+ processEnv: { ...base, NEXTAUTH_SECRET: "platform-value" },
54
+ grantedVariables: { NEXTAUTH_SECRET: "admin-value" },
55
+ });
56
+ expect(env.NEXTAUTH_SECRET).toBe("admin-value");
57
+ });
58
+
59
+ test("names bash cannot export are skipped", () => {
60
+ const { env, skippedNames } = buildSkillEnv({
61
+ processEnv: base,
62
+ grantedVariables: { "MY-VAR": "x", "2FA_KEY": "y", OK_VAR: "z" },
63
+ });
64
+ expect(env["MY-VAR"]).toBeUndefined();
65
+ expect(env["2FA_KEY"]).toBeUndefined();
66
+ expect(env.OK_VAR).toBe("z");
67
+ expect(skippedNames).toEqual(["2FA_KEY", "MY-VAR"]);
68
+ });
69
+
70
+ test("computed overrides always win", () => {
71
+ const { env } = buildSkillEnv({
72
+ processEnv: base,
73
+ grantedVariables: { NODE_PATH: "/tmp/evil" },
74
+ overrides: { NODE_PATH: "/usr/lib/node_modules" },
75
+ });
76
+ expect(env.NODE_PATH).toBe("/usr/lib/node_modules");
77
+ });
78
+
79
+ test("a runtime-discovered secret name is stripped like an inventoried one", () => {
80
+ // The LiteLLM config's `os.environ/ACME_PROVIDER_KEY` case: not in the
81
+ // inventory, still a platform credential.
82
+ const { env, strippedSecretNames } = buildSkillEnv({
83
+ processEnv: { ...base, ACME_PROVIDER_KEY: "sk-acme" },
84
+ grantedVariables: {},
85
+ extraSecretNames: ["ACME_PROVIDER_KEY"],
86
+ });
87
+ expect(env.ACME_PROVIDER_KEY).toBeUndefined();
88
+ expect(strippedSecretNames).toContain("ACME_PROVIDER_KEY");
89
+ });
90
+
91
+ test("an extra secret name absent from the environment changes nothing", () => {
92
+ const { env, strippedSecretNames } = buildSkillEnv({
93
+ processEnv: base,
94
+ grantedVariables: {},
95
+ extraSecretNames: ["NOT_SET_HERE"],
96
+ });
97
+ expect(strippedSecretNames).toEqual([]);
98
+ expect(env.PATH).toBe("/usr/bin");
99
+ });
100
+
101
+ test("an admin-granted variable still wins over a runtime-discovered secret name", () => {
102
+ // Same rule as for inventoried secrets: the platform's value goes, the
103
+ // administrator's explicit grant stays.
104
+ const { env } = buildSkillEnv({
105
+ processEnv: { ...base, COHERE_API_KEY: "platform-value" },
106
+ grantedVariables: { COHERE_API_KEY: "admin-value" },
107
+ extraSecretNames: ["COHERE_API_KEY"],
108
+ });
109
+ expect(env.COHERE_API_KEY).toBe("admin-value");
110
+ });
@@ -0,0 +1,72 @@
1
+ import { isSecretEnvName } from "@SRC/exulu/skill-env/inventory";
2
+
3
+ export type BuildSkillEnvArgs = {
4
+ processEnv: NodeJS.ProcessEnv;
5
+ grantedVariables: Record<string, string>;
6
+ overrides?: NodeJS.ProcessEnv;
7
+ /**
8
+ * Secret names discovered at runtime rather than declared in the inventory —
9
+ * today the `os.environ/NAME` directives in the deployment's
10
+ * config.litellm.yaml (see src/exulu/litellm/os-environ-names.ts). Treated
11
+ * exactly like an inventoried secret. Still a denylist: nothing is kept
12
+ * because it appears here, only removed.
13
+ */
14
+ extraSecretNames?: readonly string[];
15
+ };
16
+
17
+ export type BuildSkillEnvResult = {
18
+ env: NodeJS.ProcessEnv;
19
+ strippedSecretNames: string[];
20
+ grantedNames: string[];
21
+ skippedNames: string[];
22
+ };
23
+
24
+ const POSIX_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
25
+
26
+ /**
27
+ * The only way a skill's bash environment is constructed.
28
+ *
29
+ * Order: base runtime env (process.env minus declared secrets), then granted
30
+ * variables, then computed overrides. Unknown names pass through by design —
31
+ * stripping what we cannot enumerate is what breaks the document toolchain.
32
+ */
33
+ export const buildSkillEnv = ({
34
+ processEnv,
35
+ grantedVariables,
36
+ overrides = {},
37
+ extraSecretNames = [],
38
+ }: BuildSkillEnvArgs): BuildSkillEnvResult => {
39
+ const env: NodeJS.ProcessEnv = {};
40
+ const strippedSecretNames: string[] = [];
41
+ const extraSecrets = new Set(extraSecretNames);
42
+
43
+ for (const [name, value] of Object.entries(processEnv)) {
44
+ if (isSecretEnvName(name) || extraSecrets.has(name)) {
45
+ strippedSecretNames.push(name);
46
+ continue;
47
+ }
48
+ env[name] = value;
49
+ }
50
+
51
+ const baseKeys = new Set(Object.keys(env));
52
+ const grantedNames: string[] = [];
53
+ const skippedNames: string[] = [];
54
+
55
+ for (const [name, value] of Object.entries(grantedVariables)) {
56
+ if (!POSIX_NAME.test(name) || baseKeys.has(name)) {
57
+ skippedNames.push(name);
58
+ continue;
59
+ }
60
+ env[name] = value;
61
+ grantedNames.push(name);
62
+ }
63
+
64
+ Object.assign(env, overrides);
65
+
66
+ return {
67
+ env,
68
+ strippedSecretNames: strippedSecretNames.sort(),
69
+ grantedNames: grantedNames.sort(),
70
+ skippedNames: skippedNames.sort(),
71
+ };
72
+ };
@@ -0,0 +1,52 @@
1
+ import { selectGrantedVariables, selectRowsToDecrypt, deriveWithheldNames } from "./variable-grants";
2
+
3
+ test("only variables with allow_skill_access are returned", () => {
4
+ const rows = [
5
+ { id: "1", name: "JIRA_CLIENT_ID", value: "a", encrypted: false, allow_skill_access: true },
6
+ { id: "2", name: "ANTHROPIC_API_KEY", value: "b", encrypted: false, allow_skill_access: false },
7
+ { id: "3", name: "LEGACY", value: "c", encrypted: false, allow_skill_access: null as any },
8
+ ];
9
+ expect(selectGrantedVariables(rows as any)).toEqual({ JIRA_CLIENT_ID: "a" });
10
+ });
11
+
12
+ test("underscore-prefixed and '=' names stay excluded", () => {
13
+ const rows = [
14
+ { id: "1", name: "_HIDDEN", value: "a", encrypted: false, allow_skill_access: true },
15
+ { id: "2", name: "BAD=NAME", value: "b", encrypted: false, allow_skill_access: true },
16
+ ];
17
+ expect(selectGrantedVariables(rows as any)).toEqual({});
18
+ });
19
+
20
+ test("only granted rows are handed to the decrypter", () => {
21
+ const rows = [
22
+ { id: "1", name: "GRANTED", value: "cipher-a", encrypted: true, allow_skill_access: true },
23
+ { id: "2", name: "UNGRANTED", value: "cipher-b", encrypted: true, allow_skill_access: false },
24
+ { id: "3", name: "LEGACY", value: "cipher-c", encrypted: true, allow_skill_access: null as any },
25
+ { id: "4", name: "", value: "cipher-d", encrypted: true, allow_skill_access: true },
26
+ ];
27
+ expect(selectRowsToDecrypt(rows as any).map((r) => r.name)).toEqual(["GRANTED"]);
28
+ });
29
+
30
+ test("a row whose decryption failed is reported as withheld, not silently dropped", () => {
31
+ // Simulates the real loop: GRANTED_OK decrypts, GRANTED_BROKEN throws and
32
+ // never reaches `granted`. Both were granted; only one is usable.
33
+ const rows = [
34
+ { id: "1", name: "GRANTED_OK", value: "plain", encrypted: false, allow_skill_access: true },
35
+ { id: "2", name: "GRANTED_BROKEN", value: "cipher", encrypted: true, allow_skill_access: true },
36
+ { id: "3", name: "UNGRANTED", value: "plain", encrypted: false, allow_skill_access: false },
37
+ ];
38
+ const decrypted = [rows[0]]; // GRANTED_BROKEN was skipped by the catch
39
+ const granted = selectGrantedVariables(decrypted as any);
40
+ expect(deriveWithheldNames(rows as any, granted)).toEqual(["GRANTED_BROKEN", "UNGRANTED"]);
41
+ });
42
+
43
+ test("withheld names are derived from the unfiltered list and deduplicated", () => {
44
+ const rows = [
45
+ { id: "1", name: "DUPE", value: "a", encrypted: false, allow_skill_access: false },
46
+ { id: "2", name: "DUPE", value: "b", encrypted: false, allow_skill_access: false },
47
+ { id: "3", name: "_HIDDEN", value: "c", encrypted: false, allow_skill_access: true },
48
+ ];
49
+ // `_HIDDEN` was granted but rejected by the name-shape filter, so it is
50
+ // withheld too — exactly the diagnosis an operator needs.
51
+ expect(deriveWithheldNames(rows as any, {})).toEqual(["DUPE", "_HIDDEN"]);
52
+ });
@@ -0,0 +1,59 @@
1
+ import type { Variable } from "@EXULU_TYPES/models/variable";
2
+
3
+ /**
4
+ * Select the subset of configured `variables` rows that an administrator has
5
+ * explicitly granted to the skill sandbox, keyed by name -> decrypted value.
6
+ *
7
+ * Pure and side-effect free so it can be unit tested without pulling in
8
+ * create-sandbox.ts's import graph (S3, postgres, sandbox-runtime). Callers
9
+ * are responsible for decrypting `row.value` before calling this — rows here
10
+ * are expected to already carry plaintext values.
11
+ *
12
+ * `allow_skill_access !== true` excludes anything but an explicit true,
13
+ * including a legacy `null` from rows written before this column existed.
14
+ *
15
+ * Names starting with `_` or containing `=` stay excluded regardless of the
16
+ * grant — those shapes corrupt POSIX env parsing or shadow shell internals.
17
+ */
18
+ /**
19
+ * The rows whose ciphertext is worth touching: an explicit grant and nothing
20
+ * else. Decryption is the expensive, failure-prone half of loading variables,
21
+ * and §1 of the design says filter first — so an ungranted row's value is never
22
+ * decrypted at all.
23
+ */
24
+ export const selectRowsToDecrypt = (rows: Variable[]): Variable[] =>
25
+ rows.filter((row) => Boolean(row?.name) && row.allow_skill_access === true);
26
+
27
+ /**
28
+ * The withheld NAMES — never values — derived from the UNFILTERED row list.
29
+ *
30
+ * Deriving from the full list rather than from the decrypted subset is what
31
+ * makes a failed decrypt visible: such a row is absent from `granted`, so it
32
+ * is reported as withheld, which is what an operator needs to diagnose a
33
+ * broken skill. Derived from the same list, a row with a bad name shape
34
+ * (leading `_`, embedded `=`) is reported too.
35
+ */
36
+ export const deriveWithheldNames = (
37
+ rows: Variable[],
38
+ granted: Record<string, string>,
39
+ ): string[] => [
40
+ ...new Set(
41
+ rows
42
+ .filter((row) => Boolean(row?.name))
43
+ .map((row) => row.name)
44
+ .filter((name) => !(name in granted)),
45
+ ),
46
+ ].sort();
47
+
48
+ export const selectGrantedVariables = (rows: Variable[]): Record<string, string> => {
49
+ const out: Record<string, string> = {};
50
+ for (const row of rows) {
51
+ if (!row?.name) continue;
52
+ if (row.allow_skill_access !== true) continue;
53
+ if (row.name.startsWith("_")) continue;
54
+ if (row.name.includes("=")) continue;
55
+ if (typeof row.value !== "string") continue;
56
+ out[row.name] = row.value;
57
+ }
58
+ return out;
59
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exulu/backend",
3
3
  "author": "Qventu B.V.",
4
- "version": "4.3.2",
4
+ "version": "5.0.0",
5
5
  "main": "./dist/index.js",
6
6
  "private": false,
7
7
  "publishConfig": {
@@ -161,6 +161,7 @@
161
161
  "safe-regex": "^2.1.1",
162
162
  "tiktoken": "^1.0.21",
163
163
  "turndown": "^7.2.2",
164
+ "umap-js": "^1.4.0",
164
165
  "uuid": "^11.1.0",
165
166
  "wink-nlp": "^2.4.0",
166
167
  "winston": "^3.17.0",