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.

API: Validate URL in processLink

Steven Tey268e789d8641
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 4ae6d7a2d8..46c74d2310 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -12,6 +12,7 @@ import {
DUB_PROJECT_ID,
getDomainWithoutWWW,
getParamsFromURL,
+ getUrlFromString,
isDubDomain,
nanoid,
truncate,
@@ -221,6 +222,7 @@ export async function processLink({
}) {
let { domain, key, url, image, rewrite, geo } = payload;
+ // url checks
if (!url) {
return {
link: payload,
@@ -228,6 +230,16 @@ export async function processLink({
status: 400,
};
}
+ const processedUrl = getUrlFromString(url);
+ if (!processedUrl) {
+ return {
+ link: payload,
+ error: "Invalid destination url.",
+ status: 422,
+ };
+ }
+
+ // domain checks
if (!domain) {
return {
link: payload,
@@ -235,8 +247,9 @@ export async function processLink({
status: 400,
};
}
+
+ // expire date checks
if (payload.expiresAt) {
- // check if expiresAt is a valid
const date = new Date(payload.expiresAt);
if (isNaN(date.getTime())) {
return {
@@ -379,6 +392,7 @@ export async function processLink({
link: {
...payload,
key,
+ url: processedUrl,
// make sure projectId is set to the current project (or Dub's if there's no project)
projectId: project?.id || DUB_PROJECT_ID,
// if session is passed, set userId to the current user's id (we don't change the userId if it's already set, e.g. when editing a link)
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index 3ee8378f1b..ddd22178d5 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -6,7 +6,6 @@ import { getServerSession } from "next-auth/next";
import { createHash } from "crypto";
import {
API_DOMAIN,
- DUB_DOMAINS,
DUB_PROJECT_ID,
getSearchParams,
isDubDomain,
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 09932a91e4..c83059c3a0 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -318,7 +318,7 @@ export const isValidUrl = (url: string) => {
};
export const getUrlFromString = (str: string) => {
- if (isValidUrl(str)) return str;
+ if (isValidUrl(str)) return new URL(str).toString();
try {
if (str.includes(".") && !str.includes(" ")) {
return new URL(`https://${str}`).toString();