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: improve error handling for invalid URL submission #73

Merged
merged 3 commits into from
Sep 24, 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
2 changes: 1 addition & 1 deletion frontend/src/components/Menu/MenuDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
DialogProps,
MenuItem,
} from "@mui/material";
import { isAbsoluteUrl } from "next/dist/shared/lib/utils";
import { useEffect, FC } from "react";
import { useForm, Controller } from "react-hook-form";
import { useTranslation } from "react-i18next";
Expand All @@ -26,6 +25,7 @@ import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem";
import { Input } from "@/app-components/inputs/Input";
import { ToggleableInput } from "@/app-components/inputs/ToggleableInput";
import { IMenuItem, IMenuItemAttributes, MenuType } from "@/types/menu.types";
import { isAbsoluteUrl } from "@/utils/URL";

export type MenuDialogProps = DialogProps & {
open: boolean;
Expand Down
16 changes: 15 additions & 1 deletion frontend/src/utils/URL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,25 @@ export const getFromQuery = ({

export const buildURL = (baseUrl: string, relativePath: string): string => {
try {

const url = new URL(relativePath, baseUrl);

return url.toString();
} catch {
throw new Error(`Invalid base URL: ${baseUrl}`);
}
};

export const isAbsoluteUrl = (value: string = ""): boolean => {
try {
const url = new URL(value);
const hostnameParts = url.hostname.split(".");

return (
(url.protocol === "http:" || url.protocol === "https:") &&
hostnameParts.length > 1 &&
hostnameParts[hostnameParts.length - 1].length > 1
);
} catch (error) {
return false;
}
};