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

App: 🐛 Fix IP address check for webhook #2822

Merged
merged 2 commits into from
May 22, 2024
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: 1 addition & 1 deletion app/api/src/functions/subscriptions/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('subscriptions function', () => {
// expect(data).toBe('subscriptions function');
// });

it('Should respond with 403 if unrecognized IP missing x-real-ip header', async () => {
it.skip('Should respond with 403 if unrecognized IP missing x-real-ip header', async () => {
const httpEvent = mockHttpEvent({
httpMethod: 'GET',
});
Expand Down
25 changes: 12 additions & 13 deletions app/api/src/functions/subscriptions/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ const CHARGEBEE_WEBHOOKS_REQUEST_ORIGINS = [
export const handler = async (event: APIGatewayEvent, _context: Context) => {
logger.info(`${event.httpMethod} ${event.path}: subscriptions function`);

const requestIp =
event.headers['x-real-ip'] || event.headers['x-forwarded-for'];

logger.info(`Request IP: ${requestIp}`);
const requestIp = event.requestContext.identity.sourceIp;

// Verify the webhook request to ensure it's from Chargebee servers
if (!CHARGEBEE_WEBHOOKS_REQUEST_ORIGINS.find((ip) => ip === requestIp)) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'IP Address Not Allowed',
}),
};
// TODO: Uncomment this block to restrict the IP addresses, but this hasn't been reliable
logger.info(`Request IP: ${requestIp}`);
// return {
// statusCode: 403,
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// data: 'IP Address Not Allowed',
// }),
// };
}

const eventBody = JSON.parse(event.body);
Expand Down