| 1 | import path from 'node:path'; |
| 2 | import { Readable } |
| 3 | import { buffer } |
| 4 | import { createDatabase, type |
| 5 | import { ConfigService } |
| 6 | import { eq, inArray |
| 7 | import { migrate } |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { InMemoryS3 } |
| 11 | import { |
| 12 | ContentLengthRequiredError, |
| 13 | StorageQuotaExceededError, |
| 14 | } from '../../lib/storage/storage.errors.js'; |
| 15 | import { RepositoryAccessService } |
| 16 | import type { S3Service |
| 17 | import { StorageQuotaService } |
| 18 | import { RepositoryNotFoundError } |
| 19 | import { InvalidAssetNameError } |
| 20 | import { |
| 21 | AttachmentNotFoundError, |
| 22 | AttachmentNotOctetStreamError, |
| 23 | AttachmentTooLargeError, |
| 24 | AttachmentTypeNotAllowedError, |
| 25 | } from '../../lib/attachments/attachments.errors.js'; |
| 26 | import { AttachmentsService } |
| 27 | |
| 28 | const CONNECTION = process |
| 29 | const MIGRATIONS = path |
| 30 | import.meta.dirname, |
| 31 | '../../../../../packages/db/drizzle', |
| 32 | ); |
| 33 | |
| 34 | const OWNER = ' |
| 35 | const VISITOR = ' |
| 36 | |
| 37 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 38 | describe.skipIf(!CONNECTION |
| 39 | let db: |
| 40 | let pool: |
| 41 | let s3: |
| 42 | let quota: |
| 43 | let attachments: |
| 44 | let repository: |
| 45 | |
| 46 | function limits(env |
| 47 | quota = new |
| 48 | attachments = new |
| 49 | db, |
| 50 | new RepositoryAccessService(db |
| 51 | s3 as unknown |
| 52 | quota, |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | function upload( |
| 57 | name: string, |
| 58 | bytes: Buffer |
| 59 | { |
| 60 | requesterId = VISITOR, |
| 61 | repo = 'app' |
| 62 | bodyType = 'application/octet-stream' |
| 63 | contentLength = String( |
| 64 | }: Partial |
| 65 | requesterId: string |
| 66 | repo: string |
| 67 | bodyType: string |
| 68 | contentLength: string |
| 69 | }> = {} |
| 70 | ) { |
| 71 | return attachments.upload |
| 72 | username: 'attachment-owner', |
| 73 | repo, |
| 74 | requesterId, |
| 75 | name, |
| 76 | bodyType, |
| 77 | contentLength, |
| 78 | body: Readable.from |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | /** Makes an attachment old enough for the sweep to consider. */ |
| 83 | function age(id |
| 84 | return db |
| 85 | .update(schema.attachment |
| 86 | .set({ createdAt: |
| 87 | .where(eq(schema |
| 88 | } |
| 89 | |
| 90 | beforeAll(async () |
| 91 | ({ db, pool |
| 92 | await migrate(db |
| 93 | }); |
| 94 | |
| 95 | beforeEach(async () |
| 96 | s3 = new |
| 97 | limits({}); |
| 98 | await db |
| 99 | .delete(schema.user |
| 100 | .where(inArray(schema |
| 101 | await db.insert |
| 102 | { |
| 103 | id: OWNER, |
| 104 | name: 'Owner', |
| 105 | email: 'attachment-owner@example.com', |
| 106 | username: 'attachment-owner', |
| 107 | }, |
| 108 | { |
| 109 | id: VISITOR, |
| 110 | name: 'Visitor', |
| 111 | email: 'attachment-visitor@example.com', |
| 112 | username: 'attachment-visitor', |
| 113 | }, |
| 114 | ]); |
| 115 | [repository] = await |
| 116 | .insert(schema.repository |
| 117 | .values({ |
| 118 | name: 'app', |
| 119 | slug: 'app', |
| 120 | ownerId: OWNER, |
| 121 | visibility: 'public', |
| 122 | }) |
| 123 | .returning(); |
| 124 | }); |
| 125 | |
| 126 | afterAll(async () |
| 127 | await db |
| 128 | .delete(schema.user |
| 129 | .where(inArray(schema |
| 130 | await pool?.end |
| 131 | }); |
| 132 | |
| 133 | it('stores a file anyone who can read the repository may attach, and serves it back by id', |
| 134 | const attachment = |
| 135 | |
| 136 | expect(attachment).toMatchObject |
| 137 | name: 'Screen Shot.png', |
| 138 | contentType: 'image/png', |
| 139 | size: 3, |
| 140 | }); |
| 141 | const served = |
| 142 | expect(served.contentType |
| 143 | expect((await buffer |
| 144 | }); |
| 145 | |
| 146 | it("keeps a private repository's attachments to those who can read it", |
| 147 | const [hidden] |
| 148 | .insert(schema.repository |
| 149 | .values({ |
| 150 | name: 'hidden', |
| 151 | slug: 'hidden', |
| 152 | ownerId: OWNER, |
| 153 | visibility: 'private', |
| 154 | }) |
| 155 | .returning(); |
| 156 | const attachment = |
| 157 | requesterId: OWNER, |
| 158 | repo: hidden.slug |
| 159 | }); |
| 160 | |
| 161 | await expect( |
| 162 | upload('notes.txt', |
| 163 | ).rejects.toBeInstanceOf( |
| 164 | await expect( |
| 165 | attachments.download({ |
| 166 | attachmentId: attachment.id |
| 167 | requesterId: VISITOR, |
| 168 | }), |
| 169 | ).rejects.toBeInstanceOf( |
| 170 | await expect( |
| 171 | attachments.download({ |
| 172 | ).rejects.toThrow( |
| 173 | await expect( |
| 174 | attachments.download({ |
| 175 | ).resolves.toMatchObject( |
| 176 | }); |
| 177 | |
| 178 | it('refuses files it would serve as something runnable, paths, bodies it cannot stream and oversized files', |
| 179 | await expect( |
| 180 | upload('page.html', |
| 181 | ).rejects.toBeInstanceOf( |
| 182 | await expect( |
| 183 | upload('logo.svg', |
| 184 | ).rejects.toBeInstanceOf( |
| 185 | await expect(upload |
| 186 | InvalidAssetNameError, |
| 187 | ); |
| 188 | await expect( |
| 189 | upload('a.png', |
| 190 | ).rejects.toBeInstanceOf( |
| 191 | await expect( |
| 192 | upload('a.png', |
| 193 | ).rejects.toBeInstanceOf( |
| 194 | await expect( |
| 195 | upload('a.png', |
| 196 | contentLength: String(26 |
| 197 | }), |
| 198 | ).rejects.toBeInstanceOf( |
| 199 | expect( |
| 200 | await db |
| 201 | .select() |
| 202 | .from(schema.attachment |
| 203 | .where(eq(schema |
| 204 | ).toEqual([]); |
| 205 | }); |
| 206 | |
| 207 | it("bills the uploader's asset quota, not the repository owner's", |
| 208 | limits({ ASSET_STORAGE_QUOTA_BYTES: |
| 209 | |
| 210 | await upload('a.txt |
| 211 | await expect(upload |
| 212 | StorageQuotaExceededError, |
| 213 | ); |
| 214 | expect(await quota |
| 215 | expect(await quota |
| 216 | await expect( |
| 217 | upload('c.txt', |
| 218 | ).resolves.toMatchObject( |
| 219 | }); |
| 220 | |
| 221 | it('frees the reservation when the bytes never arrive', |
| 222 | s3.failNextPut = |
| 223 | await expect(upload |
| 224 | 'connection reset', |
| 225 | ); |
| 226 | expect(await quota |
| 227 | }); |
| 228 | |
| 229 | it('sweeps day-old attachments no text in their repository mentions, and keeps the rest', |
| 230 | const [issue] |
| 231 | .insert(schema.issue |
| 232 | .values({ |
| 233 | repositoryId: repository.id |
| 234 | number: 1, |
| 235 | title: 'Bug', |
| 236 | authorId: VISITOR, |
| 237 | }) |
| 238 | .returning(); |
| 239 | const inBody = |
| 240 | const inComment = |
| 241 | const inRelease = |
| 242 | const orphan = |
| 243 | const fresh = |
| 244 | |
| 245 | await db |
| 246 | .update(schema.issue |
| 247 | .set({ |
| 248 | body: ` |
| 250 | .where(eq(schema |
| 251 | await db.insert |
| 252 | issueId: issue.id |
| 253 | authorId: VISITOR, |
| 254 | body: `[log](/api/attachments/${inComment |
| 255 | }); |
| 256 | await db.insert |
| 257 | repositoryId: repository.id |
| 258 | tagName: 'v1', |
| 259 | body: `/api/attachments/${inRelease |
| 260 | }); |
| 261 | for (const { |
| 262 | |
| 263 | expect(await attachments |
| 264 | |
| 265 | const left = |
| 266 | .select({ id: |
| 267 | .from(schema.attachment |
| 268 | .where(eq(schema |
| 269 | expect(left.map |
| 270 | [inBody.id, |
| 271 | ); |
| 272 | await expect( |
| 273 | attachments.download({ |
| 274 | ).rejects.toBeInstanceOf( |
| 275 | expect([...s3. |
| 276 | false, |
| 277 | ); |
| 278 | }); |
| 279 | |
| 280 | it('does not count an attachment in another repository as a mention', |
| 281 | const [other] |
| 282 | .insert(schema.repository |
| 283 | .values({ |
| 284 | name: 'other', |
| 285 | slug: 'other', |
| 286 | ownerId: OWNER, |
| 287 | visibility: 'public', |
| 288 | }) |
| 289 | .returning(); |
| 290 | const attachment = |
| 291 | await db.insert |
| 292 | repositoryId: other.id |
| 293 | number: 1, |
| 294 | title: 'Elsewhere', |
| 295 | body: `/api/attachments/${attachment |
| 296 | authorId: VISITOR, |
| 297 | }); |
| 298 | await age(attachment |
| 299 | |
| 300 | expect(await attachments |
| 301 | }); |
| 302 | }); |