Skip to content
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

fix: Secure cookies in staging/test/prod #1765

Merged
merged 1 commit into from
Jan 31, 2025
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
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ services:
REDIS_CONNECTION_STRING: redis://:mysecretpassword@redis:6379/0
PORT: 80
NODE_ENV: production
COOKIE_SECURE: false
APPLICATIONINSIGHTS_CONNECTION_STRING: ${APPLICATIONINSIGHTS_CONNECTION_STRING}
APPLICATIONINSIGHTS_ENABLED: ${APPLICATIONINSIGHTS_ENABLED}
labels:
Expand Down
7 changes: 7 additions & 0 deletions packages/bff/src/graphql/fastifyHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const plugin: FastifyPluginAsync = async (fastify) => {
'X-XSS-Protection': '1; mode=block',
});
});
// Middleware to set secure cookies based on X-Forwarded-Proto header
fastify.addHook('onRequest', (request, reply, done) => {
if (request.headers['x-forwarded-proto'] === 'https') {
request.session.cookie.secure = true;
}
done();
});
} catch (e) {
logger.error(e, 'Error setting headers');
request.tokenIsValid = false;
Expand Down
10 changes: 2 additions & 8 deletions packages/bff/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ const { version, port, host, oidc_url, hostname, client_id, client_secret, redis

const startServer = async (): Promise<void> => {
const { secret, cookie: cookieConfig, enableGraphiql } = config;

const server = Fastify({
ignoreTrailingSlash: true,
ignoreDuplicateSlashes: true,
// The application gateway will terminate https and forward request as http
// This ensures that the proxy aka application gateway is trusted
// Setting cookie to secure will make it expect https requests without this
trustProxy: cookieConfig.secure,
trustProxy: true,
});

const { dataSource } = await connectToDB();
Expand All @@ -46,16 +42,14 @@ const startServer = async (): Promise<void> => {
server.register(cookie);

// Session setup

const cookieSessionConfig: FastifySessionOptions = {
secret,
cookie: {
secure: cookieConfig.secure,
httpOnly: cookieConfig.httpOnly,
httpOnly: true,
maxAge: cookieConfig.maxAge,
},
};

if (redisConnectionString) {
const store = new RedisStore({
client: redisClient,
Expand Down
Loading