| 1 | import { describe, expect, it, vi } from 'vitest'; |
| 2 | |
| 3 | import { AuthenticationRequiredError } |
| 4 | import { GitBasicAuthMiddleware } |
| 5 | |
| 6 | const basic = ( |
| 7 | `Basic ${Buffer.from |
| 8 | |
| 9 | function harness({ |
| 10 | verify = { valid |
| 11 | authorize = vi. |
| 12 | }: { |
| 13 | verify?: unknown |
| 14 | authorize?: ReturnType |
| 15 | } = {}) { |
| 16 | const auth = |
| 17 | const middleware = |
| 18 | auth as never, |
| 19 | { authorize } as |
| 20 | ); |
| 21 | |
| 22 | const res = |
| 23 | setHeader: vi.fn |
| 24 | status: vi.fn |
| 25 | end: vi.fn |
| 26 | }; |
| 27 | const next = |
| 28 | |
| 29 | const run = |
| 30 | const req = |
| 31 | params: { username: ' |
| 32 | headers: {}, |
| 33 | query: {}, |
| 34 | path: '/owner/repo/info/refs', |
| 35 | ...overrides, |
| 36 | }; |
| 37 | await middleware.use |
| 38 | return req as |
| 39 | }; |
| 40 | |
| 41 | return { run, |
| 42 | } |
| 43 | |
| 44 | describe('GitBasicAuthMiddleware', () |
| 45 | it('lets an authorized request through with the resolved actor', |
| 46 | const { run, |
| 47 | |
| 48 | const req = |
| 49 | headers: { authorization: basic |
| 50 | }); |
| 51 | |
| 52 | expect(next).toHaveBeenCalledWith |
| 53 | expect(req.actor |
| 54 | }); |
| 55 | |
| 56 | it('passes an anonymous actor through for a public fetch', |
| 57 | const { run, |
| 58 | |
| 59 | await run({}) |
| 60 | |
| 61 | expect(authorize).toHaveBeenCalledWith |
| 62 | expect.objectContaining({ |
| 63 | ); |
| 64 | expect(next).toHaveBeenCalledWith |
| 65 | }); |
| 66 | |
| 67 | it('challenges instead of failing when authorization needs an identity', |
| 68 | const { run, |
| 69 | authorize: vi.fn |
| 70 | }); |
| 71 | |
| 72 | await run({}) |
| 73 | |
| 74 | expect(res.setHeader |
| 75 | 'WWW-Authenticate', |
| 76 | 'Basic realm="Ghost"', |
| 77 | ); |
| 78 | expect(res.status |
| 79 | expect(next).not |
| 80 | }); |
| 81 | |
| 82 | it('forwards every other failure to the error filter', |
| 83 | const error = |
| 84 | const { run, |
| 85 | authorize: vi.fn |
| 86 | }); |
| 87 | |
| 88 | await run({}) |
| 89 | |
| 90 | expect(next).toHaveBeenCalledWith |
| 91 | expect(res.status |
| 92 | }); |
| 93 | |
| 94 | it('treats a push and its ref advertisement as a write', |
| 95 | for (const req |
| 96 | { path: '/owner/repo/git-receive-pack' |
| 97 | { query: { service: |
| 98 | ]) { |
| 99 | const { run, |
| 100 | await run(req |
| 101 | expect(authorize).toHaveBeenCalledWith |
| 102 | expect.objectContaining({ |
| 103 | ); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | it('keeps a colon in the key and ignores the username', |
| 108 | const { run, |
| 109 | |
| 110 | await run({ |
| 111 | |
| 112 | expect(auth.api |
| 113 | body: { key: ' |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | it('treats an invalid key as no credentials at all', |
| 118 | const { run, |
| 119 | |
| 120 | await run({ |
| 121 | |
| 122 | expect(authorize).toHaveBeenCalledWith |
| 123 | expect.objectContaining({ |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | it('strips the .git suffix clone URLs carry', |
| 128 | const { run, |
| 129 | |
| 130 | await run({ |
| 131 | |
| 132 | expect(authorize).toHaveBeenCalledWith |
| 133 | expect.objectContaining({ |
| 134 | ); |
| 135 | }); |
| 136 | }); |