| 1 | import { ConfigService } from '@nestjs/config'; |
| 2 | import { describe, expect |
| 3 | |
| 4 | import { signLfsToken } |
| 5 | import { |
| 6 | AuthenticationRequiredError, |
| 7 | RepositoryForbiddenError, |
| 8 | } from '../../../lib/repositories/access/repository-access.errors.js'; |
| 9 | import { RepositoryNotFoundError } |
| 10 | import { GitBasicAuthMiddleware } |
| 11 | |
| 12 | const basic = ( |
| 13 | `Basic ${Buffer.from |
| 14 | const SECRET = ' |
| 15 | const bearer = ( |
| 16 | `Bearer ${signLfsToken(SECRET |
| 17 | const LFS_BATCH = ' |
| 18 | |
| 19 | function harness({ |
| 20 | verify = { valid |
| 21 | authorize = vi |
| 22 | .fn() |
| 23 | .mockResolvedValue({ id: |
| 24 | blocked = false, |
| 25 | }: { |
| 26 | verify?: unknown |
| 27 | authorize?: ReturnType |
| 28 | blocked?: boolean |
| 29 | } = {}) { |
| 30 | // oauthAppBlocked reads one row, there only while the organization keeps the app out. |
| 31 | const db = |
| 32 | select: () => |
| 33 | from: () => |
| 34 | }), |
| 35 | }; |
| 36 | const auth = |
| 37 | const middleware = |
| 38 | auth as never, |
| 39 | { authorize } as |
| 40 | db as never, |
| 41 | new ConfigService({ |
| 42 | ); |
| 43 | |
| 44 | const res = |
| 45 | setHeader: vi.fn |
| 46 | status: vi.fn |
| 47 | end: vi.fn |
| 48 | }; |
| 49 | const next = |
| 50 | |
| 51 | const run = |
| 52 | const req = |
| 53 | params: { username: ' |
| 54 | headers: {}, |
| 55 | query: {}, |
| 56 | path: '/owner/repo/info/refs', |
| 57 | ...overrides, |
| 58 | }; |
| 59 | await middleware.use |
| 60 | return req as |
| 61 | }; |
| 62 | |
| 63 | return { run, |
| 64 | } |
| 65 | |
| 66 | describe('GitBasicAuthMiddleware', () |
| 67 | it('lets an authorized request through with the resolved actor', |
| 68 | const { run, |
| 69 | |
| 70 | const req = |
| 71 | headers: { authorization: basic |
| 72 | }); |
| 73 | |
| 74 | expect(next).toHaveBeenCalledWith |
| 75 | expect(req.actor |
| 76 | expect(req.apiKeyId |
| 77 | }); |
| 78 | |
| 79 | it('passes an anonymous actor through for a public fetch', |
| 80 | const { run, |
| 81 | |
| 82 | await run({}) |
| 83 | |
| 84 | expect(authorize).toHaveBeenCalledWith |
| 85 | expect.objectContaining({ |
| 86 | ); |
| 87 | expect(next).toHaveBeenCalledWith |
| 88 | }); |
| 89 | |
| 90 | it('challenges instead of failing when authorization needs an identity', |
| 91 | const { run, |
| 92 | authorize: vi.fn |
| 93 | }); |
| 94 | |
| 95 | await run({}) |
| 96 | |
| 97 | expect(res.setHeader |
| 98 | 'WWW-Authenticate', |
| 99 | 'Basic realm="Ghost"', |
| 100 | ); |
| 101 | expect(res.status |
| 102 | expect(next).not |
| 103 | }); |
| 104 | |
| 105 | it('forwards every other failure to the error filter', |
| 106 | const error = |
| 107 | const { run, |
| 108 | authorize: vi.fn |
| 109 | }); |
| 110 | |
| 111 | await run({}) |
| 112 | |
| 113 | expect(next).toHaveBeenCalledWith |
| 114 | expect(res.status |
| 115 | }); |
| 116 | |
| 117 | it('treats a push and its ref advertisement as a write', |
| 118 | for (const req |
| 119 | { path: '/owner/repo/git-receive-pack' |
| 120 | { query: { service: |
| 121 | ]) { |
| 122 | const { run, |
| 123 | await run(req |
| 124 | expect(authorize).toHaveBeenCalledWith |
| 125 | expect.objectContaining({ |
| 126 | ); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | it('keeps a colon in the key and ignores the username', |
| 131 | const { run, |
| 132 | |
| 133 | await run({ |
| 134 | |
| 135 | expect(auth.api |
| 136 | body: { key: ' |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | it('treats an invalid key as no credentials at all', |
| 141 | const { run, |
| 142 | |
| 143 | await run({ |
| 144 | |
| 145 | expect(authorize).toHaveBeenCalledWith |
| 146 | expect.objectContaining({ |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | it('strips the .git suffix clone URLs carry', |
| 151 | const { run, |
| 152 | |
| 153 | await run({ |
| 154 | |
| 155 | expect(authorize).toHaveBeenCalledWith |
| 156 | expect.objectContaining({ |
| 157 | ); |
| 158 | }); |
| 159 | |
| 160 | it('treats an LFS upload, PUT and lock change as a write', |
| 161 | for (const req |
| 162 | { path: LFS_BATCH, |
| 163 | { path: '/owner/repo/info/lfs/objects/abc', |
| 164 | { path: '/owner/repo/info/lfs/locks/verify', |
| 165 | ]) { |
| 166 | const { run, |
| 167 | await run(req |
| 168 | expect(authorize).toHaveBeenCalledWith |
| 169 | expect.objectContaining({ |
| 170 | ); |
| 171 | } |
| 172 | }); |
| 173 | |
| 174 | it('accepts a git-lfs-authenticate token on the LFS API of its repository', |
| 175 | const { run, |
| 176 | |
| 177 | const req = |
| 178 | path: LFS_BATCH, |
| 179 | method: 'POST', |
| 180 | body: { operation: ' |
| 181 | headers: { authorization: bearer |
| 182 | }); |
| 183 | |
| 184 | expect(next).toHaveBeenCalledWith |
| 185 | expect(req.actor |
| 186 | }); |
| 187 | |
| 188 | it('refuses a token outside its scope', |
| 189 | for (const req |
| 190 | { |
| 191 | path: '/owner/repo/git-receive-pack', |
| 192 | headers: { authorization: bearer |
| 193 | }, |
| 194 | { |
| 195 | path: LFS_BATCH, |
| 196 | headers: { authorization: bearer |
| 197 | }, |
| 198 | { |
| 199 | path: LFS_BATCH, |
| 200 | body: { operation: ' |
| 201 | headers: { authorization: bearer |
| 202 | }, |
| 203 | ]) { |
| 204 | const { run, |
| 205 | await run(req |
| 206 | expect(res.status |
| 207 | expect(next).not |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | it('treats a forged or expired token as no credentials at all', |
| 212 | const expired = |
| 213 | const forged = |
| 214 | for (const authorization |
| 215 | const { run, |
| 216 | await run({ |
| 217 | expect(authorize).toHaveBeenCalledWith |
| 218 | expect.objectContaining({ |
| 219 | ); |
| 220 | } |
| 221 | }); |
| 222 | it('refuses a key without repo on a private repository, and a read-only scope on a push', |
| 223 | const publicOnly = |
| 224 | verify: { |
| 225 | valid: true, |
| 226 | key: { |
| 227 | id: 'key_1', |
| 228 | referenceId: 'user_owner', |
| 229 | permissions: { scopes: [ |
| 230 | }, |
| 231 | }, |
| 232 | }); |
| 233 | await publicOnly.run |
| 234 | headers: { authorization: basic |
| 235 | }); |
| 236 | expect(publicOnly.next |
| 237 | expect.any(RepositoryNotFoundError |
| 238 | ); |
| 239 | |
| 240 | const readOrg = |
| 241 | verify: { |
| 242 | valid: true, |
| 243 | key: { |
| 244 | id: 'key_1', |
| 245 | referenceId: 'user_owner', |
| 246 | permissions: { scopes: [ |
| 247 | }, |
| 248 | }, |
| 249 | authorize: vi |
| 250 | .fn() |
| 251 | .mockResolvedValue({ id: |
| 252 | }); |
| 253 | await readOrg.run |
| 254 | headers: { authorization: basic |
| 255 | query: { service: ' |
| 256 | }); |
| 257 | expect(readOrg.next |
| 258 | expect.any(RepositoryForbiddenError |
| 259 | ); |
| 260 | }); |
| 261 | |
| 262 | it('lets public_repo push to a public repository, and a Ghost key do anything', |
| 263 | const publicOnly = |
| 264 | verify: { |
| 265 | valid: true, |
| 266 | key: { |
| 267 | id: 'key_1', |
| 268 | referenceId: 'user_owner', |
| 269 | permissions: { scopes: [ |
| 270 | }, |
| 271 | }, |
| 272 | authorize: vi |
| 273 | .fn() |
| 274 | .mockResolvedValue({ id: |
| 275 | }); |
| 276 | await publicOnly.run |
| 277 | headers: { authorization: basic |
| 278 | query: { service: ' |
| 279 | }); |
| 280 | expect(publicOnly.next |
| 281 | |
| 282 | const ghost = |
| 283 | await ghost.run |
| 284 | headers: { authorization: basic |
| 285 | query: { service: ' |
| 286 | }); |
| 287 | expect(ghost.next |
| 288 | }); |
| 289 | |
| 290 | it('hides an organization repository from an OAuth app the organization keeps out, and lets an approved one through', |
| 291 | const appKey = |
| 292 | valid: true, |
| 293 | key: { |
| 294 | id: 'key_1', |
| 295 | referenceId: 'user_owner', |
| 296 | metadata: { oauthClientId: ' |
| 297 | }, |
| 298 | }; |
| 299 | const organizationRepository = |
| 300 | .fn() |
| 301 | .mockResolvedValue({ |
| 302 | id: 'repo_1', |
| 303 | visibility: 'public', |
| 304 | organizationId: 'org_1', |
| 305 | }); |
| 306 | |
| 307 | const kept = |
| 308 | verify: appKey, |
| 309 | authorize: organizationRepository, |
| 310 | blocked: true, |
| 311 | }); |
| 312 | await kept.run |
| 313 | expect(kept.next |
| 314 | |
| 315 | const approved = |
| 316 | verify: appKey, |
| 317 | authorize: organizationRepository, |
| 318 | }); |
| 319 | await approved.run |
| 320 | headers: { authorization: basic |
| 321 | }); |
| 322 | expect(approved.next |
| 323 | }); |
| 324 | }); |