Skip to content

Commit 3164b9e

Browse files
committed
refactor: removed CORS origin handler
1 parent 5a89c1c commit 3164b9e

File tree

3 files changed

+0
-88
lines changed

3 files changed

+0
-88
lines changed

indexer/.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ DB_SSL_REJECT_UNAUTHORIZED=false
1111

1212
KADENA_GRAPHQL_API_PORT=3001 #optional
1313
SENTRY_DSN="http://sentryurl" #optional
14-
ALLOWED_ORIGINS=http://localhost:3001,http://localhost:3002,http://localhost:3003 #optional
1514
PRICE_CACHE_TTL=300 #optional

indexer/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ cp indexer/.env.template indexer/.env
5757
| `DB_SSL_ENABLED` | Enable/disable SSL for database | `true` or `false` |
5858
| `KADENA_GRAPHQL_API_PORT` | GraphQL API port | `3000` |
5959
| `SENTRY_DSN` | Sentry url to monitor indexer usage | `https://123.ingest.us.sentry.io/123` |
60-
| `ALLOWED_ORIGINS` | Allowed origins for CORS | `http://abcde:3001,http://abcde:3002` |
6160
| `PRICE_CACHE_TTL` | Time-to-live for price cache in seconds | `300` |
6261

6362
**NOTE:** The example Kadena node API from chainweb will not work for the indexer purpose. You will need to run your own Kadena node and set the `NODE_API_URL` to your node's API URL.

indexer/src/kadena-server/server.ts

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ const typeDefs = readFileSync(join(__dirname, './config/schema.graphql'), 'utf-8
8484
*/
8585
const KADENA_GRAPHQL_API_PORT = process.env.KADENA_GRAPHQL_API_PORT ?? '3001';
8686

87-
/**
88-
* Array of domains allowed to access the GraphQL API
89-
*/
90-
91-
const ALLOWED_ORIGINS = getArrayEnvString('ALLOWED_ORIGINS');
92-
9387
/**
9488
* Apollo Server plugin that validates pagination parameters in GraphQL requests
9589
*
@@ -214,36 +208,6 @@ const securitySanitizationPlugin: ApolloServerPlugin = {
214208
}),
215209
};
216210

217-
/**
218-
* Checks if an origin is allowed to access the GraphQL API
219-
*
220-
* Implements CORS policy by validating origin domains against the allowed list.
221-
* Permits localhost for development and allows both exact matches and subdomains
222-
* of kadena.io.
223-
*
224-
* @param origin - The origin domain requesting access
225-
* @returns Boolean indicating if the origin is allowed
226-
*/
227-
const isAllowedOrigin = (origin: string): boolean => {
228-
try {
229-
const originUrl = new URL(origin);
230-
if (originUrl.hostname === 'localhost') return true;
231-
232-
return ALLOWED_ORIGINS.some(allowed => {
233-
const allowedUrl = new URL(allowed);
234-
// Check if it's an exact match
235-
if (originUrl.origin === allowedUrl.origin) return true;
236-
// Check if it's a subdomain (only for kadena.io)
237-
if (allowedUrl.hostname === 'kadena.io' && originUrl.hostname.endsWith('.kadena.io')) {
238-
return true;
239-
}
240-
return false;
241-
});
242-
} catch {
243-
return false;
244-
}
245-
};
246-
247211
/**
248212
* Initializes and starts the GraphQL server
249213
*
@@ -494,20 +458,6 @@ export async function startGraphqlServer() {
494458
app.use(
495459
'/graphql',
496460
cors<cors.CorsRequest>({
497-
origin: (origin, callback) => {
498-
if (!origin || origin === 'null') {
499-
return callback(null, false);
500-
}
501-
502-
try {
503-
if (isAllowedOrigin(origin)) {
504-
return callback(null, true);
505-
}
506-
return callback(new Error(`[ERROR][CORS][ORIGIN] Origin ${origin} not allowed by CORS`));
507-
} catch (error) {
508-
return callback(null, false);
509-
}
510-
},
511461
methods: ['POST', 'OPTIONS'],
512462
allowedHeaders: [
513463
'Content-Type',
@@ -527,42 +477,6 @@ export async function startGraphqlServer() {
527477
}),
528478
);
529479

530-
/**
531-
* Handle CORS preflight OPTIONS requests explicitly
532-
*
533-
* This endpoint manages the CORS preflight requests that browsers send before making
534-
* actual API requests. It's a critical security component that:
535-
*
536-
* 1. Validates the origin against the allowed domains list
537-
* 2. Sets appropriate CORS headers when origins are allowed:
538-
* - Access-Control-Allow-Origin: Reflects the allowed origin
539-
* - Access-Control-Allow-Credentials: Enables authenticated requests
540-
* - Access-Control-Allow-Methods: Limits to POST and OPTIONS methods
541-
* - Access-Control-Allow-Headers: Specifies allowed request headers
542-
* - Access-Control-Max-Age: Caches preflight result for 24 hours (86400s)
543-
* 3. Returns 204 No Content for allowed origins or 403 Forbidden for disallowed ones
544-
*
545-
* This explicit handling ensures precise control over cross-origin security,
546-
* preventing unauthorized domains from accessing the API while allowing
547-
* legitimate client applications to function properly.
548-
*/
549-
app.options('*', (req: Request, res: Response) => {
550-
const origin = req.headers.origin;
551-
if (origin && isAllowedOrigin(origin)) {
552-
res.setHeader('Access-Control-Allow-Origin', origin);
553-
res.setHeader('Access-Control-Allow-Credentials', 'true');
554-
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
555-
res.setHeader(
556-
'Access-Control-Allow-Headers',
557-
'Content-Type, Authorization, Accept, Origin, X-Requested-With, Cache-Control, Pragma',
558-
);
559-
res.setHeader('Access-Control-Max-Age', '86400');
560-
res.status(204).end();
561-
} else {
562-
res.status(403).end();
563-
}
564-
});
565-
566480
// Initialize cache and start the server
567481
await initCache(context);
568482
await new Promise<void>(resolve => httpServer.listen({ port: KADENA_GRAPHQL_API_PORT }, resolve));

0 commit comments

Comments
 (0)