@celljs/ta-secops 4.33.0 → 4.35.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,3 +1,46 @@
1
+ // ../ta-protocol/esm/common/ask-protocol.js
2
+ var TA_ASK_ENTRY = "ta-ask";
3
+ var TaAskUtil;
4
+ (function(TaAskUtil2) {
5
+ const LENGTH = 120;
6
+ function entry(question) {
7
+ const line = question.replace(/\s+/g, " ").trim();
8
+ return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
9
+ }
10
+ TaAskUtil2.entry = entry;
11
+ function asking(entries) {
12
+ var _a;
13
+ for (let i = entries.length - 1;i >= 0; i--) {
14
+ const entry2 = entries[i];
15
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
16
+ return;
17
+ }
18
+ if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
19
+ return read(entry2);
20
+ }
21
+ }
22
+ return;
23
+ }
24
+ TaAskUtil2.asking = asking;
25
+ function next(asked, entry2) {
26
+ var _a;
27
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
28
+ return;
29
+ }
30
+ return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
31
+ }
32
+ TaAskUtil2.next = next;
33
+ function read(found) {
34
+ var _a;
35
+ const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
36
+ if (typeof question !== "string" || !question.trim()) {
37
+ return;
38
+ }
39
+ const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
40
+ return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
41
+ }
42
+ })(TaAskUtil || (TaAskUtil = {}));
43
+
1
44
  // ../ta-protocol/esm/common/ta-extension-host.js
2
45
  var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
3
46
  var TA_HOST_EVENT = "ta:host";
@@ -66,47 +109,6 @@ var TaDoubleLookUtil;
66
109
  }
67
110
  TaDoubleLookUtil2.given = given;
68
111
  })(TaDoubleLookUtil || (TaDoubleLookUtil = {}));
69
- var TA_ASK_ENTRY = "ta-ask";
70
- var TaAskUtil;
71
- (function(TaAskUtil2) {
72
- const LENGTH = 120;
73
- function entry(question) {
74
- const line = question.replace(/\s+/g, " ").trim();
75
- return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
76
- }
77
- TaAskUtil2.entry = entry;
78
- function asking(entries) {
79
- var _a;
80
- for (let i = entries.length - 1;i >= 0; i--) {
81
- const entry2 = entries[i];
82
- if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
83
- return;
84
- }
85
- if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
86
- return read(entry2);
87
- }
88
- }
89
- return;
90
- }
91
- TaAskUtil2.asking = asking;
92
- function next(asked, entry2) {
93
- var _a;
94
- if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
95
- return;
96
- }
97
- return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
98
- }
99
- TaAskUtil2.next = next;
100
- function read(found) {
101
- var _a;
102
- const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
103
- if (typeof question !== "string" || !question.trim()) {
104
- return;
105
- }
106
- const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
107
- return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
108
- }
109
- })(TaAskUtil || (TaAskUtil = {}));
110
112
 
111
113
  // web-src/secops-protocol.ts
112
114
  var NAME = "@celljs/ta-secops";
package/host/index.mjs CHANGED
@@ -1,11 +1,74 @@
1
1
  // web-src/host.ts
2
2
  import { readFile as readFile6 } from "node:fs/promises";
3
- import { join as join4 } from "node:path";
3
+ import { join as join5 } from "node:path";
4
4
 
5
5
  // web-src/secops-desk.ts
6
- import { appendFile as appendFile3, mkdir as mkdir4, readFile as readFile5, rename as rename2, stat as stat2 } from "node:fs/promises";
7
- import { join as join3 } from "node:path";
6
+ import { appendFile as appendFile3, mkdir as mkdir4, readFile as readFile5, stat as stat2 } from "node:fs/promises";
7
+ import { join as join4 } from "node:path";
8
8
 
9
+ // ../../packages/core/esm/node/fs/file-util.js
10
+ import { chmodSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
11
+ import { chmod, mkdir, rename, rm, writeFile } from "node:fs/promises";
12
+ import { basename, dirname, join } from "node:path";
13
+ var written = 0;
14
+ var FileUtil;
15
+ (function(FileUtil2) {
16
+ function temporary(file, hidden = false) {
17
+ const n = `${process.pid}.${++written}`;
18
+ return hidden ? join(dirname(file), `.${basename(file)}.${n}.tmp`) : `${file}.${n}.tmp`;
19
+ }
20
+ FileUtil2.temporary = temporary;
21
+ function ownTemporary(name) {
22
+ return new RegExp(`\\.${process.pid}\\.\\d+\\.tmp$`).test(name);
23
+ }
24
+ FileUtil2.ownTemporary = ownTemporary;
25
+ function write(file, data, options = {}) {
26
+ if (options.mkdir !== false) {
27
+ mkdirSync(dirname(file), { recursive: true });
28
+ }
29
+ const draft = temporary(file, options.hidden);
30
+ try {
31
+ writeFileSync(draft, data, options.mode === undefined ? undefined : { mode: options.mode });
32
+ if (options.mode !== undefined) {
33
+ chmodSync(draft, options.mode);
34
+ }
35
+ renameSync(draft, file);
36
+ } catch (error) {
37
+ rmSync(draft, { force: true });
38
+ throw error;
39
+ }
40
+ }
41
+ FileUtil2.write = write;
42
+ async function writeAsync(file, data, options = {}) {
43
+ if (options.mkdir !== false) {
44
+ await mkdir(dirname(file), { recursive: true });
45
+ }
46
+ const draft = temporary(file, options.hidden);
47
+ try {
48
+ await writeFile(draft, data, options.mode === undefined ? undefined : { mode: options.mode });
49
+ if (options.mode !== undefined) {
50
+ await chmod(draft, options.mode);
51
+ }
52
+ await rename(draft, file);
53
+ } catch (error) {
54
+ await rm(draft, { force: true });
55
+ throw error;
56
+ }
57
+ }
58
+ FileUtil2.writeAsync = writeAsync;
59
+ function writeJson(file, value, options = {}) {
60
+ write(file, json(value, options.pretty), options);
61
+ }
62
+ FileUtil2.writeJson = writeJson;
63
+ function writeJsonAsync(file, value, options = {}) {
64
+ return writeAsync(file, json(value, options.pretty), options);
65
+ }
66
+ FileUtil2.writeJsonAsync = writeJsonAsync;
67
+ function json(value, pretty = false) {
68
+ return pretty ? `${JSON.stringify(value, undefined, 2)}
69
+ ` : JSON.stringify(value);
70
+ }
71
+ })(FileUtil || (FileUtil = {}));
9
72
  // ../ta-protocol/esm/common/message-protocol.js
10
73
  var TaMessageContext;
11
74
  (function(TaMessageContext2) {
@@ -985,8 +1048,8 @@ class SecopsCases {
985
1048
  }
986
1049
 
987
1050
  // web-src/secops-learning.ts
988
- import { appendFile, mkdir, readFile as readFile2 } from "node:fs/promises";
989
- import { join } from "node:path";
1051
+ import { appendFile, mkdir as mkdir2, readFile as readFile2 } from "node:fs/promises";
1052
+ import { join as join2 } from "node:path";
990
1053
 
991
1054
  // web-src/secops-sources.ts
992
1055
  import { createHash } from "node:crypto";
@@ -1155,7 +1218,7 @@ class SecopsLearning {
1155
1218
  }
1156
1219
  async labels(field, chainField) {
1157
1220
  const learned = new Map;
1158
- const text = await readFile2(join(this.context.rules().dir, "labels.jsonl"), "utf8").catch(() => "");
1221
+ const text = await readFile2(join2(this.context.rules().dir, "labels.jsonl"), "utf8").catch(() => "");
1159
1222
  for (const line of text.split(`
1160
1223
  `)) {
1161
1224
  try {
@@ -1190,8 +1253,8 @@ class SecopsLearning {
1190
1253
  return;
1191
1254
  }
1192
1255
  const lines = ids.map((id) => JSON.stringify({ id: String(id), label, case: found.id, at: this.context.now() }));
1193
- await mkdir(this.context.rules().dir, { recursive: true });
1194
- await appendFile(join(this.context.rules().dir, "labels.jsonl"), `${lines.join(`
1256
+ await mkdir2(this.context.rules().dir, { recursive: true });
1257
+ await appendFile(join2(this.context.rules().dir, "labels.jsonl"), `${lines.join(`
1195
1258
  `)}
1196
1259
  `, "utf8");
1197
1260
  }
@@ -1213,8 +1276,8 @@ class SecopsLearning {
1213
1276
 
1214
1277
  // web-src/secops-ledger.ts
1215
1278
  import { createHash as createHash2 } from "node:crypto";
1216
- import { appendFile as appendFile2, mkdir as mkdir2, readFile as readFile3 } from "node:fs/promises";
1217
- import { dirname } from "node:path";
1279
+ import { appendFile as appendFile2, mkdir as mkdir3, readFile as readFile3 } from "node:fs/promises";
1280
+ import { dirname as dirname2 } from "node:path";
1218
1281
 
1219
1282
  class SecopsLedger {
1220
1283
  file;
@@ -1238,7 +1301,7 @@ class SecopsLedger {
1238
1301
  prev: last.hash
1239
1302
  };
1240
1303
  const line = { ...body, hash: SecopsLedger.hash(body) };
1241
- await mkdir2(dirname(this.file), { recursive: true });
1304
+ await mkdir3(dirname2(this.file), { recursive: true });
1242
1305
  await appendFile2(this.file, `${JSON.stringify(line)}
1243
1306
  `, "utf8");
1244
1307
  this.last = { seq: line.seq, hash: line.hash };
@@ -1745,49 +1808,49 @@ ${TaMessageSource.line(NAME)}`;
1745
1808
  // web-src/secops-rules.ts
1746
1809
  import { execFile } from "node:child_process";
1747
1810
  import { existsSync } from "node:fs";
1748
- import { mkdir as mkdir3, readdir, readFile as readFile4, rename, rm, writeFile } from "node:fs/promises";
1749
- import { join as join2 } from "node:path";
1811
+ import { readdir, readFile as readFile4, rm as rm2 } from "node:fs/promises";
1812
+ import { join as join3 } from "node:path";
1750
1813
  class SecopsRules {
1751
1814
  dir;
1752
1815
  constructor(dir) {
1753
1816
  this.dir = dir;
1754
1817
  }
1755
1818
  get ledgerFile() {
1756
- return join2(this.dir, "ledger.jsonl");
1819
+ return join3(this.dir, "ledger.jsonl");
1757
1820
  }
1758
1821
  async load() {
1759
- const rulesDir = join2(this.dir, "rules");
1822
+ const rulesDir = join3(this.dir, "rules");
1760
1823
  const names = (await readdir(rulesDir).catch(() => [])).filter((name) => name.endsWith(".json")).sort();
1761
1824
  const rules = [];
1762
1825
  for (const name of names) {
1763
- const rule = await this.json(join2(rulesDir, name));
1826
+ const rule = await this.json(join3(rulesDir, name));
1764
1827
  if (rule && !SecopsReplay.check(rule)) {
1765
1828
  rules.push(rule);
1766
1829
  }
1767
1830
  }
1768
- const scoring = { ...DEFAULT_SCORING, ...await this.json(join2(this.dir, "scoring.json")) ?? {} };
1769
- const version = (await this.json(join2(this.dir, "version.json")))?.version ?? 0;
1831
+ const scoring = { ...DEFAULT_SCORING, ...await this.json(join3(this.dir, "scoring.json")) ?? {} };
1832
+ const version = (await this.json(join3(this.dir, "version.json")))?.version ?? 0;
1770
1833
  return { rules, scoring, version };
1771
1834
  }
1772
1835
  async propose(id, body) {
1773
- const file = join2(this.dir, "proposed", `${id}.json`);
1836
+ const file = join3(this.dir, "proposed", `${id}.json`);
1774
1837
  await this.write(file, body);
1775
1838
  return file;
1776
1839
  }
1777
1840
  async release(change, note) {
1778
1841
  const current = await this.load();
1779
1842
  if (change.rule) {
1780
- await this.write(join2(this.dir, "rules", `${change.rule.id}.json`), change.rule);
1843
+ await this.write(join3(this.dir, "rules", `${change.rule.id}.json`), change.rule);
1781
1844
  }
1782
1845
  if (change.remove) {
1783
- await rm(join2(this.dir, "rules", `${change.remove}.json`), { force: true });
1846
+ await rm2(join3(this.dir, "rules", `${change.remove}.json`), { force: true });
1784
1847
  }
1785
1848
  if (change.scoring) {
1786
- await this.write(join2(this.dir, "scoring.json"), { ...current.scoring, ...change.scoring });
1849
+ await this.write(join3(this.dir, "scoring.json"), { ...current.scoring, ...change.scoring });
1787
1850
  }
1788
1851
  const version = current.version + 1;
1789
- await this.write(join2(this.dir, "version.json"), { version, at: new Date().toISOString(), proposal: note.proposal, ledger: note.ledger, by: note.by });
1790
- await rm(join2(this.dir, "proposed", `${note.proposal}.json`), { force: true });
1852
+ await this.write(join3(this.dir, "version.json"), { version, at: new Date().toISOString(), proposal: note.proposal, ledger: note.ledger, by: note.by });
1853
+ await rm2(join3(this.dir, "proposed", `${note.proposal}.json`), { force: true });
1791
1854
  await this.commit(`secops: release v${version}: ${change.why.slice(0, 72)}`, [
1792
1855
  `Proposal: ${note.proposal}`,
1793
1856
  `Approved-by: ${note.by}`,
@@ -1796,12 +1859,12 @@ class SecopsRules {
1796
1859
  return version;
1797
1860
  }
1798
1861
  async playbook() {
1799
- const text = await readFile4(join2(this.dir, "playbook.md"), "utf8").catch(() => "");
1862
+ const text = await readFile4(join3(this.dir, "playbook.md"), "utf8").catch(() => "");
1800
1863
  return text.split(`
1801
1864
  `).filter((line) => line.startsWith("- ")).map((line) => line.slice(2).replace(/\s*<!--.*?-->\s*$/, "").trim()).filter(Boolean);
1802
1865
  }
1803
1866
  async addLesson(id, text, note) {
1804
- const file = join2(this.dir, "playbook.md");
1867
+ const file = join3(this.dir, "playbook.md");
1805
1868
  const before = await readFile4(file, "utf8").catch(() => `# Playbook
1806
1869
 
1807
1870
  What investigations follow, as people approved it after reviews.
@@ -1812,14 +1875,14 @@ What investigations follow, as people approved it after reviews.
1812
1875
  await this.commit(`secops: lesson ${id}`, [`Approved-by: ${note.by}`, `Ledger: ${note.ledger}`]);
1813
1876
  }
1814
1877
  async reject(proposal) {
1815
- await rm(join2(this.dir, "proposed", `${proposal}.json`), { force: true });
1878
+ await rm2(join3(this.dir, "proposed", `${proposal}.json`), { force: true });
1816
1879
  }
1817
1880
  async commit(subject, trailers) {
1818
- if (!existsSync(join2(this.dir, ".git"))) {
1881
+ if (!existsSync(join3(this.dir, ".git"))) {
1819
1882
  return;
1820
1883
  }
1821
1884
  const git = (...args) => new Promise((resolve, reject) => execFile("git", args, { cwd: this.dir }, (error, stdout) => error ? reject(error) : resolve(String(stdout))));
1822
- const paths = ["rules", "scoring.json", "version.json", "ledger.jsonl", "labels.jsonl", "playbook.md"].filter((path) => existsSync(join2(this.dir, path)));
1885
+ const paths = ["rules", "scoring.json", "version.json", "ledger.jsonl", "labels.jsonl", "playbook.md"].filter((path) => existsSync(join3(this.dir, path)));
1823
1886
  const known = (await git("config", "user.email").catch(() => "")).trim();
1824
1887
  const author = known ? [] : ["-c", "user.name=Ta", "-c", "user.email=ta-secops@localhost"];
1825
1888
  try {
@@ -1838,11 +1901,8 @@ What investigations follow, as people approved it after reviews.
1838
1901
  }
1839
1902
  }
1840
1903
  async write(file, value, json = true) {
1841
- await mkdir3(join2(file, ".."), { recursive: true });
1842
- const draft = `${file}.${process.pid}.tmp`;
1843
- await writeFile(draft, json ? `${JSON.stringify(value, null, 2)}
1844
- ` : String(value), "utf8");
1845
- await rename(draft, file);
1904
+ await FileUtil.writeAsync(file, json ? `${JSON.stringify(value, null, 2)}
1905
+ ` : String(value));
1846
1906
  }
1847
1907
  }
1848
1908
 
@@ -1871,10 +1931,10 @@ class SecopsDesk {
1871
1931
  this.options = options;
1872
1932
  }
1873
1933
  get dir() {
1874
- return join3(this.host.agentDir, "secops");
1934
+ return join4(this.host.agentDir, "secops");
1875
1935
  }
1876
1936
  get rules() {
1877
- return new SecopsRules(this.settings.rulesDir ?? join3(this.dir, "rules"));
1937
+ return new SecopsRules(this.settings.rulesDir ?? join4(this.dir, "rules"));
1878
1938
  }
1879
1939
  get ledger() {
1880
1940
  return new SecopsLedger(this.rules.ledgerFile);
@@ -2275,16 +2335,15 @@ ${TaMessageSource.line(NAME)}`;
2275
2335
  }
2276
2336
  async out(record) {
2277
2337
  const line = { at: new Date(this.options.now()).toISOString(), ...record };
2278
- const file = join3(this.dir, "outbox.jsonl");
2338
+ const file = join4(this.dir, "outbox.jsonl");
2279
2339
  await mkdir4(this.dir, { recursive: true });
2280
2340
  if (((await stat2(file).catch(() => {
2281
2341
  return;
2282
2342
  }))?.size ?? 0) > OUTBOX_MAX) {
2283
2343
  const kept = (await readFile5(file, "utf8")).split(`
2284
2344
  `);
2285
- await appendFile3(`${file}.tmp`, kept.slice(Math.floor(kept.length / 2)).join(`
2286
- `), "utf8");
2287
- await rename2(`${file}.tmp`, file);
2345
+ await FileUtil.writeAsync(file, kept.slice(Math.floor(kept.length / 2)).join(`
2346
+ `), { mkdir: false });
2288
2347
  }
2289
2348
  await appendFile3(file, `${JSON.stringify(line)}
2290
2349
  `, "utf8");
@@ -2306,7 +2365,7 @@ ${TaMessageSource.line(NAME)}`;
2306
2365
  return kept?.day === day ? { day, count: kept.count ?? 0 } : { day, count: 0 };
2307
2366
  }
2308
2367
  async cwd() {
2309
- const cwd = this.settings.cwd ?? join3(this.dir, "work");
2368
+ const cwd = this.settings.cwd ?? join4(this.dir, "work");
2310
2369
  await mkdir4(cwd, { recursive: true }).catch(() => {
2311
2370
  return;
2312
2371
  });
@@ -2340,7 +2399,7 @@ async function activate(host) {
2340
2399
  const desk = new SecopsDesk(host, {
2341
2400
  now: () => Date.now(),
2342
2401
  serverUrl: async () => {
2343
- const info = await readFile6(join4(host.agentDir, "serve.json"), "utf8").catch(() => {
2402
+ const info = await readFile6(join5(host.agentDir, "serve.json"), "utf8").catch(() => {
2344
2403
  return;
2345
2404
  });
2346
2405
  const url = info && JSON.parse(info).url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-secops",
3
- "version": "4.33.0",
3
+ "version": "4.35.0",
4
4
  "description": "A pi package for Ta: security operations on the events of a stream of detections, each investigated with evidence it cites, actions and changes of the rules approved by a person, every step in a hash-chained ledger",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -28,8 +28,9 @@
28
28
  "watch": "tsc -b -w --preserveWatchOutput"
29
29
  },
30
30
  "devDependencies": {
31
- "@celljs/ta-protocol": "4.33.0",
32
- "@celljs/ta-web": "4.33.0",
31
+ "@celljs/core": "4.35.0",
32
+ "@celljs/ta-protocol": "4.35.0",
33
+ "@celljs/ta-web": "4.35.0",
33
34
  "@types/react": "^19.3.0"
34
35
  },
35
36
  "publishConfig": {
@@ -61,7 +62,6 @@
61
62
  "host": {
62
63
  "list": "task.view",
63
64
  "get": "task.view",
64
- "info": "task.view",
65
65
  "trace": "task.view",
66
66
  "verify": "task.view",
67
67
  "recheck": "task.view",