-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
22,552 additions
and
9,931 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,53 @@ | ||
export interface CorsConfiguration { | ||
/** | ||
* Configures the Access-Control-Allow-Origin CORS header. | ||
*/ | ||
origin?: string | string[]; | ||
/** | ||
* Configures the Access-Control-Allow-Credentials CORS header. | ||
* Set to true to pass the header, otherwise it is omitted. | ||
*/ | ||
credentials?: boolean; | ||
/** | ||
* Configures the Access-Control-Expose-Headers CORS header. | ||
* Expects a comma-delimited string (ex: 'Content-Range,X-Content-Range') | ||
* or an array (ex: ['Content-Range', 'X-Content-Range']). | ||
* If not specified, no custom headers are exposed. | ||
*/ | ||
exposedHeaders?: string | string[]; | ||
/** | ||
* Configures the Access-Control-Allow-Headers CORS header. | ||
* Expects a comma-delimited string (ex: 'Content-Type,Authorization') | ||
* or an array (ex: ['Content-Type', 'Authorization']). If not | ||
* specified, defaults to reflecting the headers specified in the | ||
* request's Access-Control-Request-Headers header. | ||
*/ | ||
allowedHeaders?: string | string[]; | ||
/** | ||
* Configures the Access-Control-Allow-Methods CORS header. | ||
* Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: ['GET', 'PUT', 'POST']). | ||
*/ | ||
methods?: string | string[]; | ||
/** | ||
* Configures the Access-Control-Max-Age CORS header. | ||
* Set to an integer to pass the header, otherwise it is omitted. | ||
*/ | ||
maxAge?: number; | ||
/** | ||
* Pass the CORS preflight response to the route handler (default: false). | ||
*/ | ||
preflightContinue?: boolean; | ||
/** | ||
* Provides a status code to use for successful OPTIONS requests, | ||
* since some legacy browsers (IE11, various SmartTVs) choke on 204. | ||
*/ | ||
optionsSuccessStatus?: number; | ||
/** | ||
* Pass the CORS preflight response to the route handler (default: false). | ||
*/ | ||
preflight?: boolean; | ||
/** | ||
* Hide options route from the documentation built using fastify-swagger (default: true). | ||
*/ | ||
hideOptionsRoute?: boolean; | ||
} |
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,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CORS Preflight request from valid origin 1`] = ` | ||
Object { | ||
"access-control-allow-headers": "x-api-client, authorization", | ||
"access-control-allow-methods": "GET, POST", | ||
"access-control-allow-origin": "http://localhost", | ||
} | ||
`; | ||
|
||
exports[`CORS reflight request from invalid origin 1`] = ` | ||
Object { | ||
"access-control-allow-headers": "x-api-client, authorization", | ||
"access-control-allow-methods": "GET, POST", | ||
"access-control-allow-origin": false, | ||
} | ||
`; |
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,63 @@ | ||
import * as fs from 'fs/promises'; | ||
import { FastifyInstance } from 'fastify'; | ||
import { createServer as createGateway } from '../../src/gateway'; | ||
import { ResourceGroup } from '../../src/modules/resource-repository'; | ||
|
||
describe('CORS', () => { | ||
let app: FastifyInstance; | ||
let dispose: () => Promise<void>; | ||
|
||
beforeAll(async () => { | ||
const resources: ResourceGroup = { | ||
schemas: [], | ||
upstreams: [], | ||
upstreamClientCredentials: [], | ||
policies: [], | ||
}; | ||
|
||
await fs.writeFile(process.env.FS_RESOURCE_REPOSITORY_PATH!, JSON.stringify(resources)); | ||
|
||
({ app, dispose } = await createGateway()); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dispose(); | ||
await fs.unlink(process.env.FS_RESOURCE_REPOSITORY_PATH!); | ||
}); | ||
|
||
test('Preflight request from valid origin', async () => { | ||
const response = await app.inject({ | ||
method: 'OPTIONS', | ||
url: '/graphql', | ||
headers: { | ||
origin: 'http://localhost', | ||
'access-control-request-method': 'POST', | ||
'access-control-request-headers': 'x-api-client,authorization', | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toEqual(204); | ||
const headers = Object.fromEntries( | ||
Object.entries(response.headers).filter(([k]) => k.startsWith('access-control-allow')) | ||
); | ||
expect(headers).toMatchSnapshot(); | ||
}); | ||
|
||
test('reflight request from invalid origin', async () => { | ||
const response = await app.inject({ | ||
method: 'OPTIONS', | ||
url: '/graphql', | ||
headers: { | ||
origin: 'http://unknown-host', | ||
'access-control-request-method': 'PUT', | ||
'access-control-request-headers': 'x-api-client,authorization,unknown-header', | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toEqual(204); | ||
const headers = Object.fromEntries( | ||
Object.entries(response.headers).filter(([k]) => k.startsWith('access-control-allow')) | ||
); | ||
expect(headers).toMatchSnapshot(); | ||
}); | ||
}); |
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