@wirecat/cli-core 0.616.0 → 0.617.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,23 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
5
5
  minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
6
6
  shape of this file.
7
7
 
8
+ ## Unreleased
9
+
10
+ ## 0.617.0 — 08.11.2026
11
+
12
+ ### Security
13
+
14
+ - **Messenger bot tokens are masked in the store.** A Telegram bot token (`<bot id>:<secret>`, as BotFather sends
15
+ it) in a message's text or caption, in the quote of a reply, or in an edit is saved as `<bot id>:<bot token
16
+ hidden>`, before the search copy and the vectors are made, for every messenger. Messages already stored, and those
17
+ an older build saves later, are masked the next time a store is opened for writing, from the newest message checked
18
+ last time (`store_settings.bot_tokens_masked_through`): the text, the search copy, the quote and the edit history,
19
+ and the vectors of the conversation pieces they were in are dropped, to be made again. No store version.
20
+ - **`Chat.searchable: false`**: a messenger saves a chat whose messages hold credentials as not searchable. Search
21
+ over all chats, search by meaning and context evidence leave it out (as for any chat with `chats.searchable = 0`);
22
+ a read that names the chat still sees it. A later save without the
23
+ field keeps the mark, and nothing a messenger saves unhides a chat.
24
+
8
25
  ## 0.616.0 — 08.11.2026
9
26
 
10
27
  ### Added
@@ -22,6 +22,12 @@ export interface Chat {
22
22
  /** Saving a chat: absent keeps the stored parent, `null` clears it. */
23
23
  parentChatId?: Id | null;
24
24
  scope?: "personal" | "work";
25
+ /**
26
+ * Saving a chat: `false` keeps it out of search over all chats, as a chat whose messages hold credentials
27
+ * (Telegram's BotFather) should be; a read that names the chat still sees it. Absent keeps the stored
28
+ * mark, and nothing a messenger saves unhides a chat.
29
+ */
30
+ searchable?: false;
25
31
  id: Id;
26
32
  title: string | null;
27
33
  kind: ChatKind;
@@ -0,0 +1,11 @@
1
+ import type { Message } from "../../domain/models.js";
2
+ import type { StoreContext } from "./open.js";
3
+ /** A messenger bot's token keeps the bot's id, which is public, and loses its secret half. */
4
+ export declare const withoutBotTokens: (text: string) => string;
5
+ export declare const withoutBotTokensIn: (message: Message) => Message;
6
+ /**
7
+ * Masks the tokens in messages this build did not write: those stored before it, and those an older build saves
8
+ * later. On every open, from the newest message checked last time; a store with nothing new costs two index probes.
9
+ * The scan reads outside a write, so the first one, over the whole archive, does not hold other writers back.
10
+ */
11
+ export declare const maskStoredBotTokens: (context: StoreContext) => number;
@@ -0,0 +1,57 @@
1
+ import { NORMALIZER_VERSION, normalize } from "../normalize.js";
2
+ import { write } from "./transaction.js";
3
+ import { purgeVectorsOf } from "./vectors.js";
4
+ // Only a digit may not come before: `bot<id>:<secret>` in an API URL is the shape pasted most often.
5
+ const BOT_TOKEN = /(?<!\d)(\d{6,12}):[A-Za-z0-9_-]{30,40}(?![A-Za-z0-9_-])/g;
6
+ const MAY_HOLD = "*[0-9][0-9][0-9][0-9][0-9][0-9]:*";
7
+ const MASKED_THROUGH = "bot_tokens_masked_through";
8
+ /** A messenger bot's token keeps the bot's id, which is public, and loses its secret half. */
9
+ export const withoutBotTokens = (text) => text.replace(BOT_TOKEN, "$1:<bot token hidden>");
10
+ export const withoutBotTokensIn = (message) => {
11
+ const { replyTo } = message;
12
+ return {
13
+ ...message,
14
+ text: withoutBotTokens(message.text),
15
+ ...(replyTo ? { replyTo: { ...replyTo, text: withoutBotTokens(replyTo.text) } } : {}),
16
+ };
17
+ };
18
+ /**
19
+ * Masks the tokens in messages this build did not write: those stored before it, and those an older build saves
20
+ * later. On every open, from the newest message checked last time; a store with nothing new costs two index probes.
21
+ * The scan reads outside a write, so the first one, over the whole archive, does not hold other writers back.
22
+ */
23
+ export const maskStoredBotTokens = (context) => {
24
+ const { database, now } = context;
25
+ const through = Number(database.prepare("SELECT value FROM store_settings WHERE key = ?").get(MASKED_THROUGH)?.value ?? 0);
26
+ const newest = Number(database.prepare("SELECT max(id) AS id FROM messages").get()?.id ?? 0);
27
+ if (newest <= through)
28
+ return 0;
29
+ const messages = database
30
+ .prepare("SELECT id, text, reply_to FROM messages WHERE id > ? AND id <= ? AND (text GLOB ? OR reply_to GLOB ?)")
31
+ .all(through, newest, MAY_HOLD, MAY_HOLD)
32
+ .filter((row) => masks(row.text) || masks(row.reply_to));
33
+ const revisions = database
34
+ .prepare("SELECT id, text FROM message_revisions WHERE message_id > ? AND message_id <= ? AND text GLOB ?")
35
+ .all(through, newest, MAY_HOLD)
36
+ .filter((row) => masks(row.text));
37
+ write(database, () => {
38
+ // Only the text read above: a message another process changed since then was written by a build that masks.
39
+ const message = database.prepare(`UPDATE messages SET text = ?, normalized_text = ?, normalizer_version = ?, reply_to = ?
40
+ WHERE id = ? AND text = ? AND reply_to IS ?`);
41
+ for (const row of messages) {
42
+ const text = withoutBotTokens(String(row.text));
43
+ const quote = row.reply_to == null ? null : withoutBotTokens(String(row.reply_to));
44
+ const { changes } = message.run(text, normalize(text), NORMALIZER_VERSION, quote, Number(row.id), row.text, row.reply_to);
45
+ if (Number(changes) > 0)
46
+ purgeVectorsOf(context, Number(row.id));
47
+ }
48
+ const revision = database.prepare("UPDATE message_revisions SET text = ? WHERE id = ? AND text = ?");
49
+ for (const row of revisions)
50
+ revision.run(withoutBotTokens(String(row.text)), Number(row.id), row.text);
51
+ database
52
+ .prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
53
+ .run(MASKED_THROUGH, String(newest), now());
54
+ });
55
+ return messages.length + revisions.length;
56
+ };
57
+ const masks = (value) => value != null && withoutBotTokens(String(value)) !== value;
@@ -150,6 +150,7 @@ export const upsertChat = (context, accountKey, chat) => {
150
150
  metadata: mergedMetadata(stored?.metadata ?? null, chat.providerMetadata),
151
151
  membershipState: chat.membershipState === "member" ? null : (chat.membershipState ?? null),
152
152
  ...stateOf(chat, now()),
153
+ ...(chat.searchable === false ? { searchable: 0 } : {}),
153
154
  createdAt: now(),
154
155
  updatedAt: now(),
155
156
  })
@@ -157,6 +158,7 @@ export const upsertChat = (context, accountKey, chat) => {
157
158
  target: [chats.accountId, chats.externalId],
158
159
  set: {
159
160
  kind: sql `excluded.kind`,
161
+ ...(chat.searchable === false ? { searchable: 0 } : {}),
160
162
  ...(chat.parentChatId === undefined ? {} : { parentChatId: sql `excluded.parent_chat_id` }),
161
163
  scope: sql `coalesce(excluded.scope, ${chats.scope})`,
162
164
  ...(chat.title === undefined ? {} : { title: sql `excluded.title`, titleFolded: sql `excluded.title_folded` }),
@@ -1,7 +1,7 @@
1
1
  import type { Delivery, Id, Message, PersonReaction, PollVote, Provider, Reactions, Reactor } from "../../domain/models.js";
2
2
  import type { AccountKey, DeletionScope } from "../types.js";
3
3
  import type { StoreContext } from "./open.js";
4
- export declare const upsertMessage: (context: StoreContext, key: AccountKey, accountKey: number, chatKey: number, message: Message, via: string, seenAt?: number) => boolean;
4
+ export declare const upsertMessage: (context: StoreContext, key: AccountKey, accountKey: number, chatKey: number, received: Message, via: string, seenAt?: number) => boolean;
5
5
  /**
6
6
  * One more unread for a message from someone else that the store first saw now, sent after the messenger's count was
7
7
  * taken. A chat the messenger never gave a count for is left alone: the store would count its whole history.
@@ -1,5 +1,6 @@
1
1
  import { validCounter } from "../../domain/counters.js";
2
2
  import { NORMALIZER_VERSION, normalize } from "../normalize.js";
3
+ import { withoutBotTokensIn } from "./bot-tokens.js";
3
4
  import { findChatPk } from "./chats.js";
4
5
  import { applyCounterObservations } from "./counters.js";
5
6
  import { and, eq, isNull, sql } from "./drizzle/core.js";
@@ -113,8 +114,9 @@ const fieldsOf = (message, sender) => ({
113
114
  normalizedText: normalize(message.text),
114
115
  normalizerVersion: NORMALIZER_VERSION,
115
116
  });
116
- export const upsertMessage = (context, key, accountKey, chatKey, message, via, seenAt) => {
117
+ export const upsertMessage = (context, key, accountKey, chatKey, received, via, seenAt) => {
117
118
  const { orm, now } = context;
119
+ const message = withoutBotTokensIn(received);
118
120
  const statements = statementsOf(orm);
119
121
  const sender = message.senderId === null || message.senderIsChat
120
122
  ? null
@@ -8,6 +8,7 @@ import { storeCapable } from "../open.js";
8
8
  import { refuseOwnerStoreInTests, storePath } from "../path.js";
9
9
  import { sharedConfigPath } from "../shared-config.js";
10
10
  import { backfillNormalized, pendingNormalization } from "./backfill.js";
11
+ import { maskStoredBotTokens } from "./bot-tokens.js";
11
12
  import { fillFiles } from "./files.js";
12
13
  import { fillFolded } from "./folded.js";
13
14
  import { fillJoinKeys } from "./join-keys.js";
@@ -102,6 +103,7 @@ export const openStoreContext = async (options = {}) => {
102
103
  moveSuggestedLinks(database, now);
103
104
  movePaymentFacts({ ...opened, now, decisions });
104
105
  purgeTestAccounts({ ...opened, now, decisions }, warn);
106
+ maskStoredBotTokens({ ...opened, now, decisions });
105
107
  const pending = pendingNormalization(database);
106
108
  if (pending > 0 && pending <= BACKFILL_ON_OPEN)
107
109
  backfillNormalized(database);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.616.0",
3
+ "version": "0.617.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",