Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions indexer/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SYNC_NETWORK="mainnet01"
KADENA_GRAPHQL_API_URL=localhost
KADENA_GRAPHQL_API_PORT=3001

API_GATEWAY_URL=https://api.mainnet.kadindexer.io

DB_USERNAME=postgres
DB_PASSWORD=password
DB_NAME=indexer
Expand Down
42 changes: 41 additions & 1 deletion indexer/src/kadena-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ const typeDefs = readFileSync(join(__dirname, './config/schema.graphql'), 'utf-8

const KADENA_GRAPHQL_API_PORT = getRequiredEnvString('KADENA_GRAPHQL_API_PORT');

const ALLOWED_ORIGINS = [
getRequiredEnvString('API_GATEWAY_URL'),
`http://localhost:${KADENA_GRAPHQL_API_PORT}`,
];

const validatePaginationParamsPlugin: ApolloServerPlugin = {
requestDidStart: async () => ({
didResolveOperation: async ({ request, document }) => {
const variables = { ...request.variables }; // External variables
// prettier-ignore
const inlineArguments: Record<string, any> = {};

// Helper function to extract inline arguments
Expand Down Expand Up @@ -136,6 +142,20 @@ export async function useKadenaGraphqlServer() {
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
verifyClient: ({ origin }, callback) => {
if (!origin || origin === 'null') {
return callback(false, 400, 'No origin');
}
try {
const url = new URL(origin);
if (ALLOWED_ORIGINS.includes(url.origin)) {
return callback(true);
}
return callback(false, 403, 'Forbidden');
} catch {
return callback(false, 400, 'Invalid origin');
}
},
});

const serverCleanup = useServer(
Expand All @@ -160,7 +180,27 @@ export async function useKadenaGraphqlServer() {

app.use(
'/graphql',
cors<cors.CorsRequest>(),
cors<cors.CorsRequest>({
origin: (origin, callback) => {
if (!origin || origin === 'null') {
return callback(null, false);
}

try {
const url = new URL(origin);
if (ALLOWED_ORIGINS.includes(url.origin)) {
return callback(null, true);
}
return callback(new Error(`Origin ${origin} not allowed by CORS`));
} catch (error) {
return callback(null, false);
}
},
methods: ['POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
// When using credentials: true, you cannot use * for Access-Control-Allow-Origin. You must specify exact origins.
credentials: true,
}),
expressMiddleware(server, {
context: createGraphqlContext,
}),
Expand Down