Skip to content

Commit

Permalink
Merge pull request #73 from devamitranjan/FIX_ISSUE_72
Browse files Browse the repository at this point in the history
fix: improve error handling for invalid URL submission
  • Loading branch information
marrouchi authored Sep 24, 2024
2 parents 7d5962d + 276943e commit 3110c6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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;
}
};

0 comments on commit 3110c6a

Please sign in to comment.