-
Notifications
You must be signed in to change notification settings - Fork 171
fix: validate cow-fi CMS inputs and revalidation flows #7618
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
Open
fairlighteth
wants to merge
4
commits into
develop
Choose a base branch
from
deepsec/medium-02-cowfi-cms-boundaries
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d7ffdf6
docs: open cow-fi CMS boundary plan for medium findings
fairlighteth 2223170
fix: harden cow-fi CMS query boundaries
fairlighteth 5138ef9
fix: reject malformed cow-fi CMS validator inputs
fairlighteth 0920e11
fix: return not-found metadata for missing cow-fi topics
fairlighteth 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
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { | ||
| CMS_REVALIDATE_TAG, | ||
| isAllowedRevalidatePath, | ||
| isValidCmsSlug, | ||
| normalizeRevalidateRequest, | ||
| normalizeSearchArticlesInput, | ||
| } from './cmsValidation' | ||
|
|
||
| describe('cmsValidation', () => { | ||
| describe('isValidCmsSlug', () => { | ||
| it('accepts canonical lowercase hyphenated slugs', () => { | ||
| expect(isValidCmsSlug('aave-trade-breakdown')).toBe(true) | ||
| expect(isValidCmsSlug('ens')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects malformed or delimiter-based slugs', () => { | ||
| expect(isValidCmsSlug('')).toBe(false) | ||
| expect(isValidCmsSlug('Aave')).toBe(false) | ||
| expect(isValidCmsSlug('../preview')).toBe(false) | ||
| expect(isValidCmsSlug('aave&publicationState=preview')).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('normalizeSearchArticlesInput', () => { | ||
| it('trims valid input and clamps pagination to safe limits', () => { | ||
| expect( | ||
| normalizeSearchArticlesInput({ | ||
| searchTerm: ' cowswap ', | ||
| page: 500, | ||
| pageSize: 1_000, | ||
| }), | ||
| ).toEqual({ | ||
| searchTerm: 'cowswap', | ||
| page: 100, | ||
| pageSize: 100, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns an empty search safely', () => { | ||
| expect( | ||
| normalizeSearchArticlesInput({ | ||
| searchTerm: ' ', | ||
| }), | ||
| ).toEqual({ | ||
| searchTerm: '', | ||
| page: 0, | ||
| pageSize: 10, | ||
| }) | ||
| }) | ||
|
|
||
| it('rejects invalid pagination even for blank searches', () => { | ||
| expect(() => normalizeSearchArticlesInput({ searchTerm: ' ', page: -1 })).toThrow( | ||
| 'Pagination parameters must be non-negative integers', | ||
| ) | ||
| expect(() => normalizeSearchArticlesInput({ searchTerm: ' ', page: 'oops' })).toThrow( | ||
| 'Pagination parameters must be non-negative integers', | ||
| ) | ||
| }) | ||
|
|
||
| it('rejects invalid search payloads', () => { | ||
| expect(() => normalizeSearchArticlesInput('cowswap')).toThrow('Search input must be an object') | ||
| expect(() => normalizeSearchArticlesInput({ searchTerm: 1 })).toThrow('Search term must be a string') | ||
| expect(() => normalizeSearchArticlesInput({ searchTerm: 'cow', page: -1 })).toThrow( | ||
| 'Pagination parameters must be non-negative integers', | ||
| ) | ||
| expect(() => normalizeSearchArticlesInput({ searchTerm: 'a'.repeat(101) })).toThrow( | ||
| 'Search term must be at most 100 characters', | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('revalidation allowlist', () => { | ||
| it('accepts only approved learn paths', () => { | ||
| expect(isAllowedRevalidatePath('/learn')).toBe(true) | ||
| expect(isAllowedRevalidatePath('/learn/articles')).toBe(true) | ||
| expect(isAllowedRevalidatePath('/learn/articles/2')).toBe(true) | ||
| expect(isAllowedRevalidatePath('/learn/topic/amm')).toBe(true) | ||
| expect(isAllowedRevalidatePath('/learn/aave-trade-breakdown')).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects paths outside the learn allowlist', () => { | ||
| expect(isAllowedRevalidatePath('/')).toBe(false) | ||
| expect(isAllowedRevalidatePath('/api/revalidate')).toBe(false) | ||
| expect(isAllowedRevalidatePath('/learn//double-slash')).toBe(false) | ||
| expect(isAllowedRevalidatePath('/learn/topic/Bad-Slug')).toBe(false) | ||
| }) | ||
|
|
||
| it('normalizes and validates revalidation payloads', () => { | ||
| expect( | ||
| normalizeRevalidateRequest({ | ||
| tag: CMS_REVALIDATE_TAG, | ||
| path: 'learn/topic/amm', | ||
| }), | ||
| ).toEqual({ | ||
| tag: CMS_REVALIDATE_TAG, | ||
| path: '/learn/topic/amm', | ||
| }) | ||
| }) | ||
|
|
||
| it('rejects invalid revalidation payloads', () => { | ||
| expect(() => normalizeRevalidateRequest(null)).toThrow('Revalidation body must be an object') | ||
| expect(() => normalizeRevalidateRequest([])).toThrow('Revalidation body must be an object') | ||
| expect(() => normalizeRevalidateRequest(new Date())).toThrow('Revalidation body must be an object') | ||
| expect(() => normalizeRevalidateRequest({ tag: 'preview' })).toThrow('Unsupported revalidation tag "preview"') | ||
| expect(() => normalizeRevalidateRequest({ tag: CMS_REVALIDATE_TAG, path: '/admin' })).toThrow( | ||
| 'Unsupported revalidation path "/admin"', | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall where this is coming from (invalidating cache manually and so forth). But isn't there a middleware or similar thing built-in in Nextjs for handling authentication?
I don't want to over complicate this, though. Only if it makes sense and it's a small change.