Skip to content

Commit

Permalink
fix: validate user exists for /user/<username>/card page (#3933)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua authored Aug 13, 2024
1 parent 67c9feb commit 9976453
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/validation-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { parse, pipe, string, uuid } from "valibot";
import { parse, pipe, string, regex, uuid } from "valibot";

export const parseSchema = parse;

export const UuidSchema = pipe(string(), uuid("The UUID is badly formatted."));

export const GitHubUserNameSchema = pipe(
string(),
regex(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i, "The GitHub + username is invalid.")
);
17 changes: 17 additions & 0 deletions pages/u/[username]/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTransition, animated } from "@react-spring/web";
import Image from "next/image";
import { usePostHog } from "posthog-js/react";
import { captureException } from "@sentry/nextjs";
import { safeParse } from "valibot";
import Button from "components/shared/Button/button";
import HeaderLogo from "components/molecules/HeaderLogo/header-logo";
import DevCardCarousel from "components/organisms/DevCardCarousel/dev-card-carousel";
Expand All @@ -12,6 +13,8 @@ import useSupabaseAuth from "lib/hooks/useSupabaseAuth";
import { linkedinCardShareUrl, siteUrl, twitterCardShareUrl } from "lib/utils/urls";
import FullHeightContainer from "components/atoms/FullHeightContainer/full-height-container";
import { isValidUrlSlug } from "lib/utils/url-validators";
import { fetchApiData } from "helpers/fetchApiData";
import { GitHubUserNameSchema } from "lib/validation-schemas";
import TwitterIcon from "../../../public/twitter-x-logo.svg";
import LinkinIcon from "../../../img/icons/social-linkedin.svg";
import BubbleBG from "../../../img/bubble-bg.svg";
Expand Down Expand Up @@ -55,6 +58,20 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
};
}

const { data: userData, error } = await fetchApiData({
path: `users/${username}`,
pathValidator: () => safeParse(GitHubUserNameSchema, username).success,
});

if (error || !userData) {
if (error?.status === 404 || error?.status === 401) {
return { notFound: true };
}
const exception = new Error(`Error in fetching user data`);
captureException(exception);
throw exception;
}

const uniqueUsernames = [...new Set([username, ...ADDITIONAL_PROFILES_TO_LOAD])];
const cards = await Promise.all(uniqueUsernames.map(fetchUserData));

Expand Down

0 comments on commit 9976453

Please sign in to comment.