Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate user exists for /user/<username>/card page #3933

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
zeucapua marked this conversation as resolved.
Show resolved Hide resolved
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
Loading