File size: 7,361 Bytes
5c2a829 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | /**
* Tests allow-from parsing and normalization helpers.
*/
import { describe, expect, it } from "vitest";
import {
formatAllowFromLowercase,
formatNormalizedAllowFromEntries,
isAllowedParsedChatSender,
isNormalizedSenderAllowed,
mapAllowlistResolutionInputs,
parseAllowFromEntries,
resolveBasicAllowFromEntries,
} from "./allow-from.js";
function parseAllowTarget(
entry: string,
):
| { kind: "chat_id"; chatId: number }
| { kind: "chat_guid"; chatGuid: string }
| { kind: "chat_identifier"; chatIdentifier: string }
| { kind: "handle"; handle: string } {
const trimmed = entry.trim();
const lower = trimmed.toLowerCase();
if (lower.startsWith("chat_id:")) {
return { kind: "chat_id", chatId: Number.parseInt(trimmed.slice("chat_id:".length), 10) };
}
if (lower.startsWith("chat_guid:")) {
return { kind: "chat_guid", chatGuid: trimmed.slice("chat_guid:".length) };
}
if (lower.startsWith("chat_identifier:")) {
return {
kind: "chat_identifier",
chatIdentifier: trimmed.slice("chat_identifier:".length),
};
}
return { kind: "handle", handle: lower };
}
describe("isAllowedParsedChatSender", () => {
it.each([
{
name: "denies when allowFrom is empty",
input: {
allowFrom: [],
sender: "+15551234567",
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
expected: false,
},
{
name: "allows wildcard entries",
input: {
allowFrom: ["*"],
sender: "user@example.com",
normalizeSender: (sender: string) => sender.toLowerCase(),
parseAllowTarget,
},
expected: true,
},
{
name: "matches normalized handles",
input: {
allowFrom: ["User@Example.com"],
sender: "user@example.com",
normalizeSender: (sender: string) => sender.toLowerCase(),
parseAllowTarget,
},
expected: true,
},
{
name: "does not match conversation targets by default",
input: [
{
allowFrom: ["chat_id:42"],
sender: "+15551234567",
chatId: 42,
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
{
allowFrom: ["chat_guid:thread-42"],
sender: "+15551234567",
chatGuid: "thread-42",
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
{
allowFrom: ["chat_identifier:team"],
sender: "+15551234567",
chatIdentifier: "team",
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
],
expected: [false, false, false],
},
{
name: "matches conversation targets when they are enabled",
input: [
{
allowFrom: ["chat_id:42"],
sender: "+15551234567",
chatId: 42,
allowConversationTargets: true,
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
{
allowFrom: ["chat_guid:thread-42"],
sender: "+15551234567",
chatGuid: "thread-42",
allowConversationTargets: true,
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
{
allowFrom: ["chat_identifier:team"],
sender: "+15551234567",
chatIdentifier: "team",
allowConversationTargets: true,
normalizeSender: (sender: string) => sender,
parseAllowTarget,
},
],
expected: [true, true, true],
},
])("$name", ({ input, expected }) => {
if (Array.isArray(input)) {
expect(input.map((entry) => isAllowedParsedChatSender(entry))).toEqual(expected);
return;
}
expect(isAllowedParsedChatSender(input)).toBe(expected);
});
});
describe("isNormalizedSenderAllowed", () => {
it.each([
{
name: "allows wildcard",
input: {
senderId: "attacker",
allowFrom: ["*"],
},
expected: true,
},
{
name: "normalizes case and strips prefixes",
input: {
senderId: "12345",
allowFrom: ["ZALO:12345", "zl:777"],
stripPrefixRe: /^(zalo|zl):/i,
},
expected: true,
},
{
name: "rejects when sender is missing",
input: {
senderId: "999",
allowFrom: ["zl:12345"],
stripPrefixRe: /^(zalo|zl):/i,
},
expected: false,
},
])("$name", ({ input, expected }) => {
expect(isNormalizedSenderAllowed(input)).toBe(expected);
});
});
describe("formatAllowFromLowercase", () => {
it("trims, strips prefixes, and lowercases entries", () => {
expect(
formatAllowFromLowercase({
allowFrom: [" Telegram:UserA ", "tg:UserB", " "],
stripPrefixRe: /^(telegram|tg):/i,
}),
).toEqual(["usera", "userb"]);
});
});
describe("formatNormalizedAllowFromEntries", () => {
it.each([
{
name: "applies custom normalization after trimming",
input: {
allowFrom: [" @Alice ", "", " @Bob "],
normalizeEntry: (entry: string) => entry.replace(/^@/, "").toLowerCase(),
},
expected: ["alice", "bob"],
},
{
name: "filters empty normalized entries",
input: {
allowFrom: ["@", "valid"],
normalizeEntry: (entry: string) => entry.replace(/^@$/, ""),
},
expected: ["valid"],
},
])("$name", ({ input, expected }) => {
expect(formatNormalizedAllowFromEntries(input)).toEqual(expected);
});
});
describe("parseAllowFromEntries", () => {
it("preserves wildcard entries and returns the first parser error", () => {
const parse = (raw: string) =>
parseAllowFromEntries(raw, (entry) =>
entry === "bad" ? { error: "invalid" } : { value: entry.toLowerCase() },
);
expect(parse(" Alice, *, alice ")).toEqual({ entries: ["alice", "*"] });
expect(parse("ok; bad; later")).toEqual({ entries: [], error: "invalid" });
});
});
describe("resolveBasicAllowFromEntries", () => {
it("uses unresolved records without a token and canonicalizes resolved ids", async () => {
const resolveEntries = async ({ entries }: { token: string; entries: string[] }) =>
entries.map((input) => ({
input,
resolved: true,
id: input === "missing" ? undefined : "1",
}));
await expect(
resolveBasicAllowFromEntries({ entries: ["alice"], resolveEntries }),
).resolves.toEqual([{ input: "alice", resolved: false, id: null }]);
await expect(
resolveBasicAllowFromEntries({
token: " token ",
entries: ["alice", "missing"],
resolveEntries,
}),
).resolves.toEqual([
{ input: "alice", resolved: true, id: "1" },
{ input: "missing", resolved: true, id: null },
]);
});
});
describe("mapAllowlistResolutionInputs", () => {
it("maps inputs sequentially and preserves order", async () => {
const visited: string[] = [];
const result = await mapAllowlistResolutionInputs({
inputs: ["one", "two", "three"],
mapInput: async (input) => {
visited.push(input);
return input.toUpperCase();
},
});
expect(visited).toEqual(["one", "two", "three"]);
expect(result).toEqual(["ONE", "TWO", "THREE"]);
});
});
|