Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

implement ip check

Jean-Baptiste Kaisere56de6cc854b
diff --git a/supabase/functions/.env.development b/supabase/functions/.env.development
index de091bc8..8351f844 100644
--- a/supabase/functions/.env.development
+++ b/supabase/functions/.env.development
@@ -1,2 +1,7 @@
POSTMARK_WEBHOOK_USER=testuser
POSTMARK_WEBHOOK_PASSWORD=testpwd
+
+# IPs can be found at https://postmarkapp.com/support/article/800-ips-for-firewalls#webhooks
+# 172.18.0.1 is for local dev only
+POSTMARK_WEBHOOK_AUTHORIZED_IPS=3.134.147.250,50.31.156.6,50.31.156.77,18.217.206.57,172.18.0.1
+
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
index 9291dc3f..eea065d5 100644
--- a/supabase/functions/postmark/index.ts
+++ b/supabase/functions/postmark/index.ts
@@ -10,12 +10,30 @@ const webhookUser = Deno.env.get('POSTMARK_WEBHOOK_USER');
const webhookPassword = Deno.env.get('POSTMARK_WEBHOOK_PASSWORD');
if (!webhookUser || !webhookPassword) {
throw new Error(
- 'Missing POSTMARK_WEBHOOK_USER or POSTMARK_WEBHOOK_PASSWORD'
+ 'Missing POSTMARK_WEBHOOK_USER or POSTMARK_WEBHOOK_PASSWORD env variable'
);
}
const expectedAuthorization = `Basic ${btoa(`${webhookUser}:${webhookPassword}`)}`;
+const rawAuthorizedIPs = Deno.env.get('POSTMARK_WEBHOOK_AUTHORIZED_IPS');
+if (!rawAuthorizedIPs) {
+ throw new Error('Missing POSTMARK_WEBHOOK_AUTHORIZED_IPS env variable');
+}
+const authorizedIPs = rawAuthorizedIPs.split(',').map(ip => ip.trim());
+
Deno.serve(async req => {
+ // Only allow known IP addresses
+ // We can use the x-forwarded-for header as it is populated by Supabase
+ // https://supabase.com/docs/guides/api/securing-your-api#accessing-request-information
+ const forwardedFor = req.headers.get('x-forwarded-for');
+ if (!forwardedFor) {
+ return new Response('Unauthorized', { status: 401 });
+ }
+ const ips = forwardedFor.split(',').map(ip => ip.trim());
+ if (!ips.some(ip => authorizedIPs.includes(ip))) {
+ return new Response('Unauthorized', { status: 401 });
+ }
+
// Only allow POST requests
if (req.method !== 'POST') {
return new Response(null, { status: 405 });
@@ -24,7 +42,7 @@ Deno.serve(async req => {
// Check the Authorization header
const authorization = req.headers.get('Authorization');
if (authorization !== expectedAuthorization) {
- return new Response('Unauthorized', { status: 403 });
+ return new Response('Unauthorized', { status: 401 });
}
const { ToFull, FromFull, Subject, TextBody, StrippedTextReply } =
@@ -213,6 +231,7 @@ ${strippedText}`;
5. In another terminal, make an HTTP request:
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/postmark' \
--header 'Content-Type: application/json' \
+ --header 'Authorization: Basic dGVzdHVzZXI6dGVzdHB3ZA==' \
--data '{
"FromName": "Postmarkapp Support",
"From": "[email protected]",