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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
import { describe, it, expect } from "bun:test";
import {
csvCell,
csvRow,
csvDocument,
formatAuditCsv,
auditCsvFilename,
AUDIT_CSV_COLUMNS,
CSV_INJECTION_CHARS,
__internal,
type AuditCsvRow,
} from "../lib/audit-csv";
describe("audit-csv — csvCell", () => {
it("emits empty string for null/undefined", () => {
expect(csvCell(null)).toBe("");
expect(csvCell(undefined)).toBe("");
});
it("passes through simple strings unchanged", () => {
expect(csvCell("hello")).toBe("hello");
expect(csvCell("with spaces")).toBe("with spaces");
expect(csvCell("a/b_c")).toBe("a/b_c");
});
it("coerces numbers and booleans via String()", () => {
expect(csvCell(42)).toBe("42");
expect(csvCell(0)).toBe("0");
expect(csvCell(true)).toBe("true");
expect(csvCell(false)).toBe("false");
});
it("ISO-serialises Date objects", () => {
const d = new Date("2025-04-01T12:34:56.789Z");
expect(csvCell(d)).toBe("2025-04-01T12:34:56.789Z");
});
it("treats invalid Date as empty", () => {
expect(csvCell(new Date("not-a-date"))).toBe("");
});
it("quotes cells that contain commas", () => {
expect(csvCell("a,b")).toBe('"a,b"');
});
it("quotes cells that contain newlines", () => {
expect(csvCell("a\nb")).toBe('"a\nb"');
expect(csvCell("a\r\nb")).toBe('"a\r\nb"');
});
it("quotes cells that contain a CR on its own (the injection char still leads, gets prefixed)", () => {
expect(csvCell("\rboom")).toBe(`"'\rboom"`);
});
it("escapes embedded double quotes", () => {
expect(csvCell('a"b')).toBe('"a""b"');
expect(csvCell('""')).toBe('""""""');
});
it("CSV-injection guard prefixes leading =", () => {
expect(csvCell("=SUM(A1)")).toBe("'=SUM(A1)");
});
it("CSV-injection guard + quote trigger combine correctly", () => {
expect(csvCell("=1,2")).toBe(`"'=1,2"`);
});
it("CSV-injection guard prefixes leading +", () => {
expect(csvCell("+1234")).toBe("'+1234");
});
it("CSV-injection guard prefixes leading -", () => {
expect(csvCell("-5")).toBe("'-5");
});
it("CSV-injection guard prefixes leading @", () => {
expect(csvCell("@name")).toBe("'@name");
});
it("CSV-injection guard prefixes leading tab", () => {
expect(csvCell("\tdata")).toBe("'\tdata");
});
it("does NOT prefix for benign leading chars", () => {
expect(csvCell("!bang")).toBe("!bang");
expect(csvCell("#hash")).toBe("#hash");
expect(csvCell("a=1")).toBe("a=1");
expect(csvCell(" leading-space")).toBe(" leading-space");
});
it("handles Unicode without mangling", () => {
expect(csvCell("héllo 🚀")).toBe("héllo 🚀");
});
it("empty string stays empty", () => {
expect(csvCell("")).toBe("");
});
it("exposes the trigger set for documentation/testing", () => {
expect(CSV_INJECTION_CHARS.has("=")).toBe(true);
expect(CSV_INJECTION_CHARS.has("+")).toBe(true);
expect(CSV_INJECTION_CHARS.has("-")).toBe(true);
expect(CSV_INJECTION_CHARS.has("@")).toBe(true);
expect(CSV_INJECTION_CHARS.has("\t")).toBe(true);
expect(CSV_INJECTION_CHARS.has("\r")).toBe(true);
expect(CSV_INJECTION_CHARS.has("a")).toBe(false);
expect(CSV_INJECTION_CHARS.has("=")).toBe(true);
});
});
describe("audit-csv — csvRow", () => {
it("joins cells with comma and terminates with CRLF", () => {
expect(csvRow(["a", "b", "c"])).toBe("a,b,c\r\n");
});
it("empty array yields just the CRLF", () => {
expect(csvRow([])).toBe("\r\n");
});
it("nulls become empty cells", () => {
expect(csvRow([null, "x", undefined])).toBe(",x,\r\n");
});
it("quotes are applied per-cell", () => {
expect(csvRow(["a,b", 'c"d', "e"])).toBe('"a,b","c""d",e\r\n');
});
});
describe("audit-csv — csvDocument", () => {
it("concatenates rows into a full document", () => {
expect(
csvDocument([
["a", "b"],
["1", "2"],
])
).toBe("a,b\r\n1,2\r\n");
});
it("empty input yields empty string", () => {
expect(csvDocument([])).toBe("");
});
});
describe("audit-csv — formatAuditCsv", () => {
const sampleRow: AuditCsvRow = {
id: "aud_1",
action: "branch.rename",
targetType: "branch",
targetId: "refs/heads/old",
ip: "203.0.113.1",
userAgent: "Mozilla/5.0",
metadata: '{"from":"old","to":"new"}',
createdAt: new Date("2025-04-01T12:00:00.000Z"),
actor: "alice",
};
it("writes header row exactly once", () => {
const csv = formatAuditCsv([sampleRow]);
const lines = csv.split("\r\n");
expect(lines[0]).toBe(
"id,when,actor,action,targetType,targetId,ip,userAgent,metadata"
);
});
it("writes a single data row after the header", () => {
const csv = formatAuditCsv([sampleRow]);
const lines = csv.split("\r\n").filter(Boolean);
expect(lines).toHaveLength(2);
expect(lines[1]).toBe(
'aud_1,2025-04-01T12:00:00.000Z,alice,branch.rename,branch,refs/heads/old,203.0.113.1,Mozilla/5.0,"{""from"":""old"",""to"":""new""}"'
);
});
it("empty rows array still writes the header", () => {
const csv = formatAuditCsv([]);
expect(csv).toBe(
"id,when,actor,action,targetType,targetId,ip,userAgent,metadata\r\n"
);
});
it("nullable fields become empty cells", () => {
const row: AuditCsvRow = {
id: "x",
action: "login",
targetType: null,
targetId: null,
ip: null,
userAgent: null,
metadata: null,
createdAt: new Date("2025-01-01T00:00:00Z"),
actor: null,
};
const csv = formatAuditCsv([row]);
const dataLine = csv.split("\r\n")[1]!;
expect(dataLine).toBe("x,2025-01-01T00:00:00.000Z,,login,,,,,");
});
it("accepts string dates and normalises to ISO", () => {
const row: AuditCsvRow = {
...sampleRow,
createdAt: "2025-04-01T12:00:00Z",
};
const csv = formatAuditCsv([row]);
expect(csv).toContain("2025-04-01T12:00:00.000Z");
});
it("keeps unparseable string createdAt verbatim", () => {
const row: AuditCsvRow = {
...sampleRow,
createdAt: "not-a-date",
};
const csv = formatAuditCsv([row]);
const lines = csv.split("\r\n").filter(Boolean);
expect(lines[1]).toContain(",not-a-date,");
});
it("CSV-injection-guards formula-like actor names", () => {
const row: AuditCsvRow = { ...sampleRow, actor: "=cmd|evil" };
const csv = formatAuditCsv([row]);
expect(csv).toContain(",'=cmd|evil,");
});
it("exposes canonical column list", () => {
expect(AUDIT_CSV_COLUMNS).toEqual([
"id",
"when",
"actor",
"action",
"targetType",
"targetId",
"ip",
"userAgent",
"metadata",
]);
});
it("each row has exactly 9 cells (commas outside quoted regions)", () => {
const tricky: AuditCsvRow = {
id: "aud_tricky",
action: "x",
targetType: "t",
targetId: "id",
ip: "1.1.1.1",
userAgent: "mx, browser",
metadata: '{"a":"b,c"}',
createdAt: new Date("2025-04-01T00:00:00Z"),
actor: "name,with,commas",
};
const csv = formatAuditCsv([tricky]);
const dataLine = csv.split("\r\n")[1]!;
const stripped = dataLine.replace(/"[^"]*"/g, "X");
expect(stripped.match(/,/g)?.length).toBe(8);
});
});
describe("audit-csv — auditCsvFilename", () => {
it("slug + ISO timestamp form", () => {
const fn = auditCsvFilename("personal", new Date("2025-04-01T12:00:00Z"));
expect(fn).toBe("audit-personal-2025-04-01T12-00-00-000Z.csv");
});
it("sanitises arbitrary scope strings", () => {
const fn = auditCsvFilename(
"alice/repo?bad",
new Date("2025-04-01T00:00:00Z")
);
expect(fn).toMatch(/^audit-alice-repo-bad-2025-04-01T00-00-00-000Z\.csv$/);
});
it("falls back to 'audit' on empty or all-bad scope", () => {
const fn = auditCsvFilename("///", new Date("2025-04-01T00:00:00Z"));
expect(fn).toBe("audit-audit-2025-04-01T00-00-00-000Z.csv");
});
it("uses current time when `now` omitted", () => {
const fn = auditCsvFilename("personal");
expect(fn).toMatch(
/^audit-personal-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z\.csv$/
);
});
});
describe("audit-csv — routes", () => {
it("GET /settings/audit.csv returns 302 when unauthenticated", async () => {
const { default: app } = await import("../app");
const res = await app.request("/settings/audit.csv");
expect([302, 401, 403]).toContain(res.status);
});
it("GET /:owner/:repo/settings/audit.csv is guarded (never 500)", async () => {
const { default: app } = await import("../app");
const res = await app.request("/alice/repo/settings/audit.csv");
expect([302, 401, 403, 404]).toContain(res.status);
});
});
describe("audit-csv — __internal parity", () => {
it("re-exports helpers", () => {
expect(__internal.csvCell).toBe(csvCell);
expect(__internal.csvRow).toBe(csvRow);
expect(__internal.csvDocument).toBe(csvDocument);
expect(__internal.formatAuditCsv).toBe(formatAuditCsv);
expect(__internal.auditCsvFilename).toBe(auditCsvFilename);
expect(__internal.AUDIT_CSV_COLUMNS).toBe(AUDIT_CSV_COLUMNS);
expect(__internal.CSV_INJECTION_CHARS).toBe(CSV_INJECTION_CHARS);
expect(typeof __internal.normaliseCreated).toBe("function");
});
});
|