Repository navigation
Coordinate booru auth quotas per key #137
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
4 commits
Select commit
Hold shift + click to select a range
25ce0ec
Coordinate booru auth quotas per key
AlejandroAkbal 66379dc
Address booru auth quota review feedback
AlejandroAkbal db10c4e
Fail fast on invalid booru auth config
AlejandroAkbal 48443ab
Use shared sensitive params in live auth tests
AlejandroAkbal 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,140 @@ | ||
| import type { TestingModule } from '@nestjs/testing' | ||
| import { Test } from '@nestjs/testing' | ||
| import { ConfigModule, ConfigService } from '@nestjs/config' | ||
| import { BooruTypesStringEnum } from '@alejandroakbal/universal-booru-wrapper' | ||
| import { BooruService, ManagedCredentialPoolUnavailableError } from './booru.service' | ||
| import type { booruQueryValuesPostsDTO } from './dto/booru-queries.dto' | ||
| import { BooruAuthManagerService } from './services/booru-auth-manager.service' | ||
| import { SENSITIVE_AUTH_PARAMS } from './constants/sensitive-auth-params' | ||
|
|
||
| const runLiveTests = process.env['RUN_BOORU_AUTH_LIVE_TESTS'] === 'true' | ||
| const describeLive = runLiveTests ? describe : describe.skip | ||
| const sensitiveAuthParams = SENSITIVE_AUTH_PARAMS | ||
|
|
||
| const liveBoorus: { | ||
| domain: string | ||
| booruType: BooruTypesStringEnum | ||
| initialPageID: number | ||
| }[] = [ | ||
| { | ||
| domain: 'gelbooru.com', | ||
| booruType: BooruTypesStringEnum.GELBOORU_COM, | ||
| initialPageID: 0 | ||
| }, | ||
| { | ||
| domain: 'rule34.xxx', | ||
| booruType: BooruTypesStringEnum.RULE_34_XXX, | ||
| initialPageID: 0 | ||
| } | ||
| ] | ||
|
|
||
| function sanitizeLiveSmokeErrorMessage(message: string): string { | ||
| let sanitizedMessage = message | ||
|
|
||
| for (const key of sensitiveAuthParams) { | ||
| const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
| sanitizedMessage = sanitizedMessage.replace(new RegExp(`([?&\\s]${escapedKey}=)[^\\s&#]+`, 'gi'), '$1REDACTED') | ||
| } | ||
|
|
||
| return sanitizedMessage | ||
| } | ||
|
|
||
| describe('live smoke error sanitization', () => { | ||
| it('redacts auth query params before rethrowing non-quota live errors', () => { | ||
| const message = | ||
| 'HTTP 403 https://gelbooru.com/index.php?page=dapi&api_key=secret&user_id=123 auth_user=user auth_pass=pass' | ||
|
|
||
| const sanitizedMessage = sanitizeLiveSmokeErrorMessage(message) | ||
|
|
||
| expect(sanitizedMessage).toContain('api_key=REDACTED') | ||
| expect(sanitizedMessage).toContain('user_id=REDACTED') | ||
| expect(sanitizedMessage).toContain('auth_user=REDACTED') | ||
| expect(sanitizedMessage).toContain('auth_pass=REDACTED') | ||
| expect(sanitizedMessage).not.toContain('secret') | ||
| expect(sanitizedMessage).not.toContain('user_id=123') | ||
| expect(sanitizedMessage).not.toContain('auth_pass=pass') | ||
| }) | ||
| }) | ||
|
|
||
| describeLive('authenticated booru live smoke tests', () => { | ||
| let module: TestingModule | ||
| let service: BooruService | ||
|
|
||
| beforeAll(async () => { | ||
| module = await Test.createTestingModule({ | ||
| imports: [ConfigModule.forRoot({ isGlobal: true, cache: false })], | ||
| providers: [BooruService, BooruAuthManagerService, ConfigService] | ||
| }).compile() | ||
|
|
||
| service = module.get(BooruService) | ||
| module.get(BooruAuthManagerService).onModuleInit() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await module.close() | ||
| }) | ||
|
|
||
| it.each(liveBoorus)( | ||
| 'returns normalized posts for $domain with configured auth', | ||
| async (booru: (typeof liveBoorus)[number]) => { | ||
| const queries = { | ||
| baseEndpoint: booru.domain, | ||
| limit: 1, | ||
| pageID: booru.initialPageID | ||
| } as booruQueryValuesPostsDTO | ||
|
|
||
| let posts: { id?: unknown }[] | ||
|
|
||
| try { | ||
| posts = await service.executeWithAuthStrategy<{ id?: unknown }[]>( | ||
| { booruType: booru.booruType }, | ||
| queries, | ||
| (api) => | ||
| api.getPosts({ | ||
| limit: 1, | ||
| pageID: booru.initialPageID | ||
| }) | ||
| ) | ||
| } catch (error) { | ||
| if (error instanceof ManagedCredentialPoolUnavailableError && error.reason === 'cooldown_exhausted') { | ||
| console.warn( | ||
| JSON.stringify({ | ||
| domain: booru.domain, | ||
| booruType: booru.booruType, | ||
| status: 'quota_exhausted', | ||
| retryAfterSeconds: error.retryAfterSeconds | ||
| }) | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| const message = error instanceof Error ? error.message : String(error) | ||
| const sanitizedMessage = sanitizeLiveSmokeErrorMessage(message) | ||
|
|
||
| if (error instanceof Error) { | ||
| error.message = sanitizedMessage | ||
|
|
||
| if (error.stack !== undefined) { | ||
| error.stack = sanitizeLiveSmokeErrorMessage(error.stack) | ||
| } | ||
| } | ||
|
|
||
| throw new Error(sanitizedMessage, { cause: error }) | ||
| } | ||
|
|
||
| expect(posts.length).toBeGreaterThan(0) | ||
|
|
||
| const firstPost = posts[0] | ||
|
|
||
| expect(firstPost?.id).toBeDefined() | ||
|
|
||
| console.log( | ||
| JSON.stringify({ | ||
| domain: booru.domain, | ||
| booruType: booru.booruType, | ||
| itemCount: posts.length | ||
| }) | ||
| ) | ||
| } | ||
| ) | ||
| }) | ||
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.