-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(apps/legacy-api): reject with 404 for queries with invalid networ…
…k url param (#1089) * fix(apps/legacy-api): protect against invalid networks - refactor 'network' param to use NetworkName type def - add test guarding all endpoints * fix(apps/legacy-api): remove regtest from supported networks * chore(apps/legacy-api): change inline doc to jsdoc format * chore(apps/legacy-api): move supported networks into NetworkValidationPipe
- Loading branch information
Showing
9 changed files
with
119 additions
and
21 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
apps/legacy-api/__tests__/controllers/NetworkParamValidation.test.ts
This file contains 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,38 @@ | ||
import { LegacyApiTesting } from '../../testing/LegacyApiTesting' | ||
|
||
const apiTesting = LegacyApiTesting.create() | ||
|
||
describe('NetworkParamValidation', () => { | ||
beforeAll(async () => { | ||
await apiTesting.start() | ||
}) | ||
|
||
afterAll(async () => { | ||
await apiTesting.stop() | ||
}) | ||
|
||
it('all registered routes are guarded against invalid network param', async () => { | ||
// dummy call to trigger fastify hooks so that all routes are registered | ||
await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/v1' | ||
}).catch() | ||
|
||
const routes = apiTesting.getAllRoutes() | ||
for (const { url } of routes) { | ||
if (url === '*') { | ||
continue | ||
} | ||
|
||
const result = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: url + '?network=abc' // Query with some invalid network | ||
}) | ||
|
||
expect(result.json()).toStrictEqual({ | ||
message: 'Not Found', | ||
statusCode: 404 | ||
}) | ||
} | ||
}) | ||
}) |
This file contains 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 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 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 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 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,33 @@ | ||
import { | ||
ArgumentMetadata, | ||
HttpException, | ||
Injectable, | ||
PipeTransform | ||
} from '@nestjs/common' | ||
|
||
export type SupportedNetwork = 'mainnet' | 'testnet' | ||
|
||
@Injectable() | ||
export class NetworkValidationPipe implements PipeTransform { | ||
private static readonly VALID_NETWORKS: Set<undefined | SupportedNetwork> = new Set([ | ||
undefined, // defaults to 'mainnet' | ||
'mainnet', | ||
'testnet' | ||
]) | ||
|
||
transform (value: any, metadata: ArgumentMetadata): any { | ||
if (NetworkValidationPipe.VALID_NETWORKS.has(value)) { | ||
return value | ||
} | ||
throw new InvalidNetworkException() | ||
} | ||
} | ||
|
||
export class InvalidNetworkException extends HttpException { | ||
constructor () { | ||
super({ | ||
statusCode: 404, | ||
message: 'Not Found' | ||
}, 404) | ||
} | ||
} |
This file contains 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 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 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