Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

small improvement

daniel31x130f00109c0ecf
diff --git a/.env.sample b/.env.sample
index 03e3d079..f81777eb 100644
--- a/.env.sample
+++ b/.env.sample
@@ -120,7 +120,7 @@ APPLE_CUSTOM_NAME=
APPLE_CLIENT_ID=
APPLE_TEAM_ID=
APPLE_KEY_ID=
-APPLE_PRIVATE_KEY_PATH=
+APPLE_PRIVATE_KEY=
APPLE_CLIENT_SECRET=
# Atlassian
diff --git a/apps/web/lib/api/apple.ts b/apps/web/lib/api/apple.ts
index 15dc8e3d..a617b13c 100644
--- a/apps/web/lib/api/apple.ts
+++ b/apps/web/lib/api/apple.ts
@@ -27,7 +27,7 @@ export function getAppleClientId() {
function getApplePrivateKey() {
if (applePrivateKey) return applePrivateKey;
- const privateKey = readSecret(process.env.APPLE_PRIVATE_KEY_PATH);
+ const privateKey = readSecret(process.env.APPLE_PRIVATE_KEY);
if (!privateKey) throw Error("Apple private key is not configured.");
diff --git a/apps/web/pages/api/v1/webhooks/app-store.ts b/apps/web/pages/api/v1/webhooks/app-store.ts
index d70d15f6..0e85e32c 100644
--- a/apps/web/pages/api/v1/webhooks/app-store.ts
+++ b/apps/web/pages/api/v1/webhooks/app-store.ts
@@ -1,4 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
+import { timingSafeEqual } from "crypto";
import { prisma } from "@linkwarden/prisma";
import getAppleSubscriptionState, {
decodeJws,
@@ -9,6 +10,18 @@ import { writeAppleSubscription } from "@/lib/api/billing/syncStoreSubscription"
const UUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
+const isAuthorized = (req: NextApiRequest) => {
+ const secret = process.env.APP_STORE_WEBHOOK_TOKEN;
+ if (!secret) return true;
+
+ const token = req.query.token;
+ if (typeof token !== "string") return false;
+
+ const a = new TextEncoder().encode(token);
+ const b = new TextEncoder().encode(secret);
+ return a.length === b.length && timingSafeEqual(a, b);
+};
+
type NotificationPayload = {
notificationType?: string;
data?: {
@@ -41,6 +54,10 @@ export default async function appStoreWebhook(
});
}
+ if (!isAuthorized(req)) {
+ return res.status(401).json({ response: "Invalid token." });
+ }
+
const payload = decodeJws<NotificationPayload>(req.body?.signedPayload);
if (!payload?.notificationType) {
@@ -51,7 +68,10 @@ export default async function appStoreWebhook(
payload.data?.signedTransactionInfo
);
- if (payload.notificationType === "TEST" || !transactionInfo?.originalTransactionId) {
+ if (
+ payload.notificationType === "TEST" ||
+ !transactionInfo?.originalTransactionId
+ ) {
return res.status(200).json({ response: "Event ignored." });
}
@@ -78,7 +98,11 @@ export default async function appStoreWebhook(
if (subscription) userId = subscription.userId;
}
- if (!userId && state.appAccountToken && UUID_REGEX.test(state.appAccountToken)) {
+ if (
+ !userId &&
+ state.appAccountToken &&
+ UUID_REGEX.test(state.appAccountToken)
+ ) {
const user = await prisma.user.findUnique({
where: { uuid: state.appAccountToken },
select: { id: true },