-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.mjs
More file actions
392 lines (374 loc) · 12.2 KB
/
Copy pathgenerate.mjs
File metadata and controls
392 lines (374 loc) · 12.2 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Generates a suite of example PDFs that each exercise one redaction-failure
// mode (plus a correctly-redacted control). Writes the PDFs and a machine-
// readable manifest.json that the test suite asserts against.
//
// Hand-built with no dependencies so the fixtures stay tiny and reviewable.
// Run: node examples/generate.mjs
import {writeFileSync, mkdirSync} from 'node:fs'
import {dirname, join} from 'node:path'
import {fileURLToPath} from 'node:url'
const OUT = dirname(fileURLToPath(import.meta.url))
// --- Adobe Helvetica advance widths (per 1000 em) so boxes land on words ----
// prettier-ignore
const HELV = {
' ':278,'!':278,'"':355,'#':556,'$':556,'%':889,'&':667,"'":191,'(':333,')':333,
'*':389,'+':584,',':278,'-':333,'.':278,'/':278,'0':556,'1':556,'2':556,'3':556,
'4':556,'5':556,'6':556,'7':556,'8':556,'9':556,':':278,';':278,'<':584,'=':584,
'>':584,'?':556,'@':1015,'A':667,'B':667,'C':722,'D':722,'E':667,'F':611,'G':778,
'H':722,'I':278,'J':500,'K':667,'L':556,'M':833,'N':722,'O':778,'P':667,'Q':778,
'R':722,'S':667,'T':611,'U':722,'V':667,'W':944,'X':667,'Y':667,'Z':611,'[':278,
'\\':278,']':278,'^':469,'_':556,'`':333,'a':556,'b':556,'c':500,'d':556,'e':556,
'f':278,'g':556,'h':556,'i':222,'j':222,'k':500,'l':222,'m':833,'n':556,'o':556,
'p':556,'q':556,'r':333,'s':500,'t':278,'u':556,'v':500,'w':722,'x':500,'y':500,
'z':500,'{':334,'|':260,'}':334,'~':584,
}
const tw = (s, size) =>
[...s].reduce((a, c) => a + (HELV[c] ?? 556), 0) * (size / 1000)
const esc = (s) => s.replace(/([\\()])/g, '\\$1')
/** Serialize an array of object bodies (obj N = index N-1) into a valid PDF. */
function serialize(objects, {info = null} = {}) {
let pdf = '%PDF-1.4\n'
const offsets = []
objects.forEach((body, i) => {
offsets.push(pdf.length)
pdf += `${i + 1} 0 obj\n${body}\nendobj\n`
})
const xref = pdf.length
pdf += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`
for (const o of offsets) pdf += `${String(o).padStart(10, '0')} 00000 n \n`
pdf += `trailer\n<< /Size ${objects.length + 1} /Root 1 0 R${
info ? ` /Info ${info} 0 R` : ''
} >>\nstartxref\n${xref}\n%%EOF`
return Buffer.from(pdf, 'latin1')
}
const SIZE = 13
const X = 72 // left margin
/** A line of absolutely-positioned text pieces; returns the content + x cursor. */
function textAt(x, y, s) {
return `BT /F1 ${SIZE} Tf ${x.toFixed(1)} ${y} Td (${esc(s)}) Tj ET\n`
}
function blackBox(x, y, w) {
return `${x.toFixed(1)} ${(y - 3).toFixed(1)} ${w.toFixed(1)} 15 re`
}
const page = (extra, contentsRef = 4, fontRef = 5, xobjRef = null) =>
`<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792]${extra} /Resources << /Font << /F1 ${fontRef} 0 R >>${
xobjRef ? ` /XObject << /Im0 ${xobjRef} 0 R >>` : ''
} >> /Contents ${contentsRef} 0 R >>`
const FONT = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>'
const stream = (s) => `<< /Length ${s.length} >>\nstream\n${s}endstream`
const fixtures = []
const add = (file, title, bytes, expect) => {
writeFileSync(join(OUT, file), bytes)
fixtures.push({file, title, ...expect})
}
// 1) Superficial mark: black box over still-present text -----------------
{
const y = 700
const pre = 'Patient note: '
const secret = 'Jordan Hayes'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
let c = textAt(X, y, pre)
c += textAt(xS, y, secret)
c += textAt(xS + wS, y, ' was admitted on the 3rd.')
c += `0 0 0 rg\n${blackBox(xS, y, wS)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
]
add(
'01-superficial-mark.pdf',
'Black box over selectable text',
serialize(objs),
{
grade: 'F',
expectChecks: ['superficial-mark'],
minLeaks: 1,
recoveredIncludes: ['Jordan', 'Hayes'],
}
)
}
// 2) Word-size leak: clean inline box, no text underneath ----------------
{
const y = 700
const pre = 'Authorized by '
const post = ' on the date shown above.'
const xEnd = X + tw(pre, SIZE)
const boxW = tw('Patricia', SIZE) // a ~8-char name
let c = textAt(X, y, pre)
c += textAt(xEnd + boxW + 4, y, post)
c += `0 0 0 rg\n${blackBox(xEnd, y, boxW)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
]
add(
'02-word-size-leak.pdf',
'Box width leaks the hidden length',
serialize(objs),
{
grade: 'B',
expectChecks: ['word-size'],
minWarnings: 1,
}
)
}
// 3) Multiple boxes in a single content-stream path ----------------------
{
const y = 700
const a = 'Northwind'
const b = 'Globex'
const pre = 'Vendors '
const mid = ' and '
const xA = X + tw(pre, SIZE)
const wA = tw(a, SIZE)
const xB = xA + wA + tw(mid, SIZE)
const wB = tw(b, SIZE)
let c = textAt(X, y, pre)
c += textAt(xA, y, a)
c += textAt(xA + wA, y, mid)
c += textAt(xB, y, b)
c += textAt(xB + wB, y, ' were both shortlisted.')
// Two rectangles, ONE path, ONE fill.
c += `0 0 0 rg\n${blackBox(xA, y, wA)} ${blackBox(xB, y, wB)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
]
add(
'03-multi-box-one-path.pdf',
'Two redactions drawn as one path',
serialize(objs),
{
grade: 'F',
expectChecks: ['superficial-mark'],
minLeaks: 2,
recoveredIncludes: ['Northwind', 'Globex'],
}
)
}
// 4) Removable Redact annotation over text -------------------------------
{
const y = 700
const pre = 'Wire funds to account '
const secret = '4471902338'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
let c = textAt(X, y, pre)
c += textAt(xS, y, secret)
c += textAt(xS + wS, y, ' before noon.')
const rect = `[${xS.toFixed(1)} ${y - 3} ${(xS + wS).toFixed(1)} ${y + 12}]`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
`<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [6 0 R] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>`,
stream(c),
FONT,
`<< /Type /Annot /Subtype /Redact /Rect ${rect} /IC [0 0 0] >>`,
]
add(
'04-removable-redact-annotation.pdf',
'Un-applied Redact annotation',
serialize(objs),
{
grade: 'F',
expectChecks: ['removable-annotation'],
minLeaks: 1,
}
)
}
// 5) Removable Square annotation (black) over text -----------------------
{
const y = 700
const pre = 'SSN on file: '
const secret = '987-65-4320'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
let c = textAt(X, y, pre)
c += textAt(xS, y, secret)
c += textAt(xS + wS, y, ' (verified).')
const rect = `[${xS.toFixed(1)} ${y - 3} ${(xS + wS).toFixed(1)} ${y + 12}]`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
`<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [6 0 R] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>`,
stream(c),
FONT,
`<< /Type /Annot /Subtype /Square /Rect ${rect} /C [0 0 0] /IC [0 0 0] /CA 1 >>`,
]
add(
'05-removable-square-annotation.pdf',
'Draggable black square over text',
serialize(objs),
{
grade: 'F',
expectChecks: ['removable-annotation'],
minLeaks: 1,
}
)
}
// 6) (removed) The metadata-leak fixture was retired when document-level
// metadata/outline checks were disabled. The numbering gap is intentional so
// the remaining fixtures keep their existing filenames.
// 7) Rotated page (/Rotate 90) with a superficial mark -------------------
{
const y = 700
const pre = 'Codename: '
const secret = 'Cardinal'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
let c = textAt(X, y, pre)
c += textAt(xS, y, secret)
c += textAt(xS + wS, y, ' (do not circulate).')
c += `0 0 0 rg\n${blackBox(xS, y, wS)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(' /Rotate 90'),
stream(c),
FONT,
]
add(
'07-rotated-superficial.pdf',
'Superficial mark on a rotated page',
serialize(objs),
{
grade: 'F',
expectChecks: ['superficial-mark'],
minLeaks: 1,
}
)
}
// 8) Hidden OCR-style text layer under a full-page image -----------------
{
const y = 700
const pre = 'Scanned record: '
const secret = 'Marcus Cole'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
// Full-page 1x1 white image (makes the page look like a scan), then text,
// then a black box over the name.
let c = 'q 612 0 0 792 0 0 cm /Im0 Do Q\n'
c += textAt(X, y, pre)
c += textAt(xS, y, secret)
c += textAt(xS + wS, y, ' — file 22A.')
c += `0 0 0 rg\n${blackBox(xS, y, wS)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page('', 4, 5, 6),
stream(c),
FONT,
'<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent 8 /ColorSpace /DeviceGray /Length 1 >>\nstream\n\xff\nendstream',
]
add(
'08-ocr-layer-leak.pdf',
'Invisible text under a scanned page',
serialize(objs),
{
grade: 'F',
expectChecks: ['ocr-layer-leak'],
minLeaks: 1,
}
)
}
// 9) Properly redacted control — should produce NO findings --------------
{
const y = 700
// Text with the secret simply absent (truly removed), clean metadata.
let c = textAt(X, y, 'Authorized by on the date shown above.')
c += textAt(X, y - 30, 'All sensitive fields were removed before export.')
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
'<< /Producer (Acme PDF 1.0) >>',
]
add(
'09-clean-control.pdf',
'Correctly redacted (no leaks)',
serialize(objs, {info: 6}),
{
grade: 'A',
expectNoFindings: true,
}
)
}
// 10) Visible exemption label stamped ON TOP of an applied redaction -----
// Mirrors what redaction tools export when "show exemption reason" is on:
// the text under the box was genuinely removed, the black box was drawn, and
// the reason was painted over the box in white. The label is visible by
// design and must not be reported as text hidden under the mark.
{
const y = 700
const label = 'Redacted - personal privacy'
const labelW = tw(label, SIZE)
const boxW = labelW / 0.75 // label tiles 75% of the box (over MIN_COVERAGE)
const labelX = X + (boxW - labelW) / 2
let c = textAt(X, y + 30, 'Disciplinary review, prepared by HR.')
c += textAt(X, y - 30, 'Resolution recorded and filed by the clerk.')
c += `0 0 0 rg\n${blackBox(X, y, boxW)} f\n`
c += `1 1 1 rg\n${textAt(labelX, y, label)}`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
]
add(
'10-exemption-overlay.pdf',
'Visible exemption label over an applied redaction',
serialize(objs),
{
grade: 'A',
expectNoFindings: true,
}
)
}
// 11) White text painted BEFORE the box — hidden, despite the label colour
// Control for the overlay-label exemption: z-order is what separates a label
// from a leak. This text could be copied out from under the box, so it must
// still be flagged even though it is white like an exemption label.
{
const y = 700
const pre = 'Approved settlement amount: '
const secret = '$2,750,000'
const xS = X + tw(pre, SIZE)
const wS = tw(secret, SIZE)
let c = textAt(X, y, pre)
c += `1 1 1 rg\n${textAt(xS, y, secret)}`
c += `0 0 0 rg\n${blackBox(xS, y, wS)} f\n`
const objs = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
page(''),
stream(c),
FONT,
]
add(
'11-white-text-under-box.pdf',
'White text painted before the box',
serialize(objs),
{
grade: 'F',
expectChecks: ['superficial-mark'],
minLeaks: 1,
recoveredIncludes: ['2,750,000'],
}
)
}
writeFileSync(join(OUT, 'manifest.json'), JSON.stringify(fixtures, null, 2))
console.log(`Wrote ${fixtures.length} example PDFs + manifest.json to ${OUT}`)
for (const f of fixtures) console.log(` ${f.file} — grade ${f.grade}`)