Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix qr code

Steven Tey912894f44dda
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index 68e9ec4253..f1d9e653b7 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -1,8 +1,10 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { ratelimitOrThrow } from "@/lib/api/utils";
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { extractPublishableKey, ratelimitOrThrow } from "@/lib/api/utils";
+import { getWorkspaceByPublishableKey } from "@/lib/planetscale";
import { QRCodeSVG } from "@/lib/qr/utils";
import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
import { getSearchParams } from "@dub/utils";
+import { DUB_QR_LOGO } from "@dub/utils/src/constants";
import { ImageResponse } from "next/og";
import { NextRequest } from "next/server";
@@ -10,12 +12,25 @@ export const runtime = "edge";
export async function GET(req: NextRequest) {
try {
- await ratelimitOrThrow(req, "qr");
-
const params = getSearchParams(req.url);
+
const { url, logo, size, level, fgColor, bgColor, includeMargin } =
getQRCodeQuerySchema.parse(params);
+ if (logo !== DUB_QR_LOGO) {
+ const publishableKey = extractPublishableKey(req);
+ await ratelimitOrThrow(req, "qr"); // rate limit in case of DDoS
+ const workspace = await getWorkspaceByPublishableKey(publishableKey);
+
+ if (!workspace || workspace.plan === "free") {
+ throw new DubApiError({
+ code: "unauthorized",
+ message:
+ "You can only use a logo with a paid plan and by authenticating via a publishable key. Learn more: https://d.to/pk",
+ });
+ }
+ }
+
return new ImageResponse(
QRCodeSVG({
value: url,
diff --git a/apps/web/lib/api/utils.ts b/apps/web/lib/api/utils.ts
index 7e85f5df78..03be300a6c 100644
--- a/apps/web/lib/api/utils.ts
+++ b/apps/web/lib/api/utils.ts
@@ -55,10 +55,16 @@ export const extractPublishableKey = (req: Request) => {
const authorizationHeader = req.headers.get("Authorization");
if (!authorizationHeader) {
- throw new DubApiError({
- code: "bad_request",
- message: "Missing authorization header",
- });
+ const searchParams = new URL(req.url).searchParams;
+ const token = searchParams.get("token");
+ if (!token) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Missing publishable key. Please pass it either as a Authorization Bearer token or as a `token` query parameter.",
+ });
+ }
+ return token;
}
return authorizationHeader.replace("Bearer ", "");
diff --git a/apps/web/lib/openapi/qr/get-qr.ts b/apps/web/lib/openapi/qr/get-qr.ts
deleted file mode 100644
index ef97c8fb62..0000000000
--- a/apps/web/lib/openapi/qr/get-qr.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z from "@/lib/zod";
-import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
-import { ZodOpenApiOperationObject } from "zod-openapi";
-
-export const getQRCode: ZodOpenApiOperationObject = {
- operationId: "getQRCode",
- "x-speakeasy-name-override": "get",
- summary: "Retrieve a QR code",
- description: "Retrieve a QR code for a link.",
- requestParams: {
- query: getQRCodeQuerySchema,
- },
- responses: {
- "200": {
- description: "The QR code",
- content: {
- "image/png": {
- schema: z.string(),
- },
- },
- },
- ...openApiErrorResponses,
- },
- tags: ["QR Codes"],
-};
diff --git a/apps/web/lib/openapi/qr/index.ts b/apps/web/lib/openapi/qr/index.ts
index edd5db3142..da6eb1cc27 100644
--- a/apps/web/lib/openapi/qr/index.ts
+++ b/apps/web/lib/openapi/qr/index.ts
@@ -1,6 +1,30 @@
-import { ZodOpenApiPathsObject } from "zod-openapi";
+import { ZodOpenApiOperationObject, ZodOpenApiPathsObject } from "zod-openapi";
-import { getQRCode } from "./get-qr";
+import z from "@/lib/zod";
+import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
+import { openApiErrorResponses } from "../responses";
+
+export const getQRCode: ZodOpenApiOperationObject = {
+ operationId: "getQRCode",
+ "x-speakeasy-name-override": "get",
+ summary: "Retrieve a QR code",
+ description: "Retrieve a QR code for a link.",
+ requestParams: {
+ query: getQRCodeQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The QR code",
+ content: {
+ "image/png": {
+ schema: z.string(),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["QR Codes"],
+};
export const qrCodePaths: ZodOpenApiPathsObject = {
"/qr": {
diff --git a/apps/web/lib/qr/index.tsx b/apps/web/lib/qr/index.tsx
index 16104e607c..4588647bb1 100644
--- a/apps/web/lib/qr/index.tsx
+++ b/apps/web/lib/qr/index.tsx
@@ -3,6 +3,7 @@
* Copyright (c) Paul O'Shannessy
* SPDX-License-Identifier: ISC
*/
+import { DUB_QR_LOGO } from "@dub/utils/src/constants";
import { useEffect, useRef, useState } from "react";
import qrcodegen from "./codegen";
import {
@@ -363,7 +364,7 @@ export function getQRData({
includeMargin: false,
...(showLogo && {
imageSettings: {
- src: logo || "https://assets.dub.co/logo.png",
+ src: logo || DUB_QR_LOGO,
height: 256,
width: 256,
excavate: true,
diff --git a/apps/web/lib/zod/schemas/qr.ts b/apps/web/lib/zod/schemas/qr.ts
index 8462af07d8..2e6e6c2cde 100644
--- a/apps/web/lib/zod/schemas/qr.ts
+++ b/apps/web/lib/zod/schemas/qr.ts
@@ -5,6 +5,7 @@ import {
QR_LEVELS,
} from "@/lib/qr/constants";
import z from "@/lib/zod";
+import { DUB_QR_LOGO } from "@dub/utils/src/constants";
import { booleanQuerySchema } from "./misc";
import { parseUrlSchema } from "./utils";
@@ -13,9 +14,9 @@ export const getQRCodeQuerySchema = z.object({
logo: z
.string()
.optional()
- .default("https://assets.dub.co/logo.png")
+ .default(DUB_QR_LOGO)
.describe(
- "The logo to include in the QR code. Defaults to `https://assets.dub.co/logo.png` if not provided.",
+ "The logo to include in the QR code. Can only be used with a paid plan – please authenticate via a publishable key. Learn more: https://d.to/pk",
),
size: z.coerce
.number()
diff --git a/apps/web/ui/modals/link-builder/qr-code-preview.tsx b/apps/web/ui/modals/link-builder/qr-code-preview.tsx
index e61f7c7ad9..cec6f27ebe 100644
--- a/apps/web/ui/modals/link-builder/qr-code-preview.tsx
+++ b/apps/web/ui/modals/link-builder/qr-code-preview.tsx
@@ -8,6 +8,7 @@ import {
} from "@dub/ui";
import { Pen2, QRCode as QRCodeIcon } from "@dub/ui/src/icons";
import { linkConstructor } from "@dub/utils";
+import { DUB_QR_LOGO } from "@dub/utils/src/constants";
import { AnimatePresence, motion } from "framer-motion";
import { useContext, useMemo } from "react";
import { useFormContext } from "react-hook-form";
@@ -40,9 +41,7 @@ export function QRCodePreview() {
const showLogo = data.showLogo || workspacePlan === "free";
const logo =
- workspaceLogo && workspacePlan !== "free"
- ? workspaceLogo
- : "https://assets.dub.co/logo.png";
+ workspaceLogo && workspacePlan !== "free" ? workspaceLogo : DUB_QR_LOGO;
const { LinkQRModal, setShowLinkQRModal } = useLinkQRModal({
props: {
diff --git a/apps/web/ui/modals/link-qr-modal.tsx b/apps/web/ui/modals/link-qr-modal.tsx
index 58afa60a56..d8c329f8c9 100644
--- a/apps/web/ui/modals/link-qr-modal.tsx
+++ b/apps/web/ui/modals/link-qr-modal.tsx
@@ -26,6 +26,7 @@ import {
Photo,
} from "@dub/ui/src/icons";
import { API_DOMAIN, cn, linkConstructor } from "@dub/utils";
+import { DUB_QR_LOGO } from "@dub/utils/src/constants";
import { AnimatePresence, motion } from "framer-motion";
import {
Dispatch,
@@ -106,10 +107,7 @@ function LinkQRModalInner({
);
const [data, setData] = useState(dataPersisted);
- const logo =
- workspaceLogo && plan !== "free"
- ? workspaceLogo
- : "https://assets.dub.co/logo.png";
+ const logo = workspaceLogo && plan !== "free" ? workspaceLogo : DUB_QR_LOGO;
const showLogo = data.showLogo || plan === "free";
diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.ts
index ecd45977f6..47ddd8117f 100644
--- a/packages/utils/src/constants/main.ts
+++ b/packages/utils/src/constants/main.ts
@@ -46,6 +46,7 @@ export const ADMIN_HOSTNAMES = new Set([
]);
export const DUB_LOGO = "https://assets.dub.co/logo.png";
+export const DUB_QR_LOGO = "https://assets.dub.co/logo.png";
export const DUB_WORDMARK = "https://assets.dub.co/wordmark.png";
export const DUB_THUMBNAIL = "https://assets.dub.co/thumbnail.jpg";