-
Notifications
You must be signed in to change notification settings - Fork 7
feat: exclude bot traffic from egress quotas #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7fb8341
feat: exclude bots from egress quotas
bajtos 69cf789
Merge branch 'main' into detect_known_bots
juliangruber 7ffb657
change schema to JSON
juliangruber a65ba5e
fix and simplify test
juliangruber 602337d
fix test
juliangruber 3631a7e
fmt
juliangruber ed9f08f
`npm run build:types`
juliangruber 79356a3
add dev env, build types
juliangruber 306877b
Merge branch 'main' into detect_known_bots
juliangruber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE retrieval_logs ADD COLUMN bot_name TEXT; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,104 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
| import { parseRequest } from '../lib/request.js' | ||
| import { parseRequest, checkBotAuthorization } from '../lib/request.js' | ||
|
|
||
| const DNS_ROOT = '.filbeam.io' | ||
| const TEST_WALLET = 'abc123' | ||
| const TEST_CID = 'baga123' | ||
| const BOT_TOKENS = JSON.stringify({ secret: 'bot1' }) | ||
|
|
||
| describe('parseRequest', () => { | ||
| it('should parse payerWalletAddress and pieceCid from a URL with both params', () => { | ||
| const request = { url: `https://${TEST_WALLET}${DNS_ROOT}/${TEST_CID}` } | ||
| const result = parseRequest(request, { DNS_ROOT }) | ||
| const request = new Request(`https://${TEST_WALLET}${DNS_ROOT}/${TEST_CID}`) | ||
| const result = parseRequest(request, { DNS_ROOT, BOT_TOKENS }) | ||
| expect(result).toEqual({ | ||
| payerWalletAddress: TEST_WALLET, | ||
| pieceCid: TEST_CID, | ||
| }) | ||
| }) | ||
|
|
||
| it('should parse payerWalletAddress and pieceCid from a URL with leading slash', () => { | ||
| const request = { url: `https://${TEST_WALLET}${DNS_ROOT}//${TEST_CID}` } | ||
| const result = parseRequest(request, { DNS_ROOT }) | ||
| const request = new Request( | ||
| `https://${TEST_WALLET}${DNS_ROOT}//${TEST_CID}`, | ||
| ) | ||
| const result = parseRequest(request, { DNS_ROOT, BOT_TOKENS }) | ||
| expect(result).toEqual({ | ||
| payerWalletAddress: TEST_WALLET, | ||
| pieceCid: TEST_CID, | ||
| }) | ||
| }) | ||
|
|
||
| it('should return descriptive error for missing pieceCid', () => { | ||
| const request = { url: `https://${TEST_WALLET}${DNS_ROOT}/` } | ||
| expect(() => parseRequest(request, { DNS_ROOT })).toThrowError( | ||
| const request = new Request(`https://${TEST_WALLET}${DNS_ROOT}/`) | ||
| expect(() => parseRequest(request, { DNS_ROOT, BOT_TOKENS })).toThrowError( | ||
| 'Missing required path element: `/{CID}`', | ||
| ) | ||
| }) | ||
|
|
||
| it('should return undefined for both if no params in path', () => { | ||
| const request = { url: 'https://filbeam.io' } | ||
| expect(() => parseRequest(request, { DNS_ROOT })).toThrowError( | ||
| const request = new Request('https://filbeam.io') | ||
| expect(() => parseRequest(request, { DNS_ROOT, BOT_TOKENS })).toThrowError( | ||
| 'Invalid hostname: filbeam.io. It must end with .filbeam.io.', | ||
| ) | ||
| }) | ||
|
|
||
| it('should ignore query parameters', () => { | ||
| const request = { | ||
| url: `https://${TEST_WALLET}${DNS_ROOT}/${TEST_CID}?foo=bar`, | ||
| } | ||
| const result = parseRequest(request, { DNS_ROOT }) | ||
| const request = new Request( | ||
| `https://${TEST_WALLET}${DNS_ROOT}/${TEST_CID}?foo=bar`, | ||
| ) | ||
| const result = parseRequest(request, { DNS_ROOT, BOT_TOKENS }) | ||
| expect(result).toEqual({ | ||
| payerWalletAddress: TEST_WALLET, | ||
| pieceCid: TEST_CID, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('checkBotAuthorization', () => { | ||
| it('should return undefined when no authorization header is present', () => { | ||
| const request = new Request('https://example.com', { | ||
| headers: {}, | ||
| }) | ||
| const result = checkBotAuthorization(request, { BOT_TOKENS }) | ||
| expect(result).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should throw 401 error when authorization header is not Bearer format', () => { | ||
| const request = new Request('https://example.com', { | ||
| headers: { authorization: 'Basic sometoken' }, | ||
| }) | ||
| expect(() => checkBotAuthorization(request, { BOT_TOKENS })).toThrowError( | ||
| 'Unauthorized: Authorization header must use Bearer scheme', | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw 401 error when authorization header has no token after Bearer', () => { | ||
| const request = new Request('https://example.com', { | ||
| headers: { authorization: 'Bearer' }, | ||
| }) | ||
| expect(() => checkBotAuthorization(request, { BOT_TOKENS })).toThrowError( | ||
| 'Unauthorized: Authorization header must use Bearer scheme', | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw 401 error when token is not in BOT_TOKENS list', () => { | ||
| const request = new Request('https://example.com', { | ||
| headers: { authorization: 'Bearer invalid_token' }, | ||
| }) | ||
| expect(() => checkBotAuthorization(request, { BOT_TOKENS })).toThrowError( | ||
| 'Unauthorized: Invalid Access Token', | ||
| ) | ||
| }) | ||
|
|
||
| it('should return token prefix when valid token is provided', () => { | ||
| const request = new Request('https://example.com', { | ||
| headers: { authorization: 'Bearer secret' }, | ||
| }) | ||
| const result = checkBotAuthorization(request, { | ||
| BOT_TOKENS: JSON.stringify({ | ||
| secret: 'bot1', | ||
| secret_2: 'bot2', | ||
| }), | ||
| }) | ||
| expect(result).toBe('bot1') | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.