From 052a1e5b604348ae266f99b9c03832e01d22ca0b Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Wed, 9 Mar 2022 15:01:46 +0200 Subject: [PATCH 1/5] Prevent special characters on filename --- components/EditorNavigation.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/EditorNavigation.tsx b/components/EditorNavigation.tsx index 1d99998..c969819 100644 --- a/components/EditorNavigation.tsx +++ b/components/EditorNavigation.tsx @@ -91,13 +91,18 @@ const EditorNavigation = ({ showWat }: { showWat?: boolean }) => { const validateFilename = useCallback( (filename: string): { error: string | null } => { // check if filename already exists - if (snap.files.find(file => file.name === filename)) { + if (snap.files.find((file) => file.name === filename)) { return { error: "Filename already exists." }; } // check for illegal characters const ILLEGAL_REGEX = /[/]/gi; - if (filename.match(ILLEGAL_REGEX)) { + const ALPHA_NUMERICAL_REGEX = /[^A-Za-z0-9._-]+/g; + if ( + filename.match(ILLEGAL_REGEX) || + // Allow only a-z, A-Z, 0-9, ".", "-" and "_" + filename.match(ALPHA_NUMERICAL_REGEX) + ) { return { error: "Filename contains illegal characters" }; } // More checks in future From 5490e7205a6bc06f699c4f6e2b42480e4e25d3e3 Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Wed, 9 Mar 2022 16:24:23 +0200 Subject: [PATCH 2/5] Improved the filename validation --- components/EditorNavigation.tsx | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/components/EditorNavigation.tsx b/components/EditorNavigation.tsx index c969819..edaf636 100644 --- a/components/EditorNavigation.tsx +++ b/components/EditorNavigation.tsx @@ -96,33 +96,26 @@ const EditorNavigation = ({ showWat }: { showWat?: boolean }) => { } // check for illegal characters - const ILLEGAL_REGEX = /[/]/gi; - const ALPHA_NUMERICAL_REGEX = /[^A-Za-z0-9._-]+/g; - if ( - filename.match(ILLEGAL_REGEX) || - // Allow only a-z, A-Z, 0-9, ".", "-" and "_" - filename.match(ALPHA_NUMERICAL_REGEX) - ) { - return { error: "Filename contains illegal characters" }; + const ALPHA_NUMERICAL_REGEX = /^[\w,\s-]+\.[A-Za-z0-9]{1,4}$/g; + if (!filename.match(ALPHA_NUMERICAL_REGEX)) { + return { + error: `Filename can contain only characters from a-z, A-Z, 0-9, "_" and "-" and it needs to have file extension (e.g. ".c")`, + }; } - // More checks in future return { error: null }; }, [snap.files] ); const handleConfirm = useCallback(() => { // add default extension in case omitted - let _filename = filename.includes(".") - ? filename - : filename + DEFAULT_EXTENSION; - const chk = validateFilename(_filename); - if (chk.error) { + const chk = validateFilename(filename); + if (chk && chk.error) { setNewfileError(`Error: ${chk.error}`); return; } setIsNewfileDialogOpen(false); - createNewFile(_filename); + createNewFile(filename); setFilename(""); }, [filename, setIsNewfileDialogOpen, setFilename, validateFilename]); From 0c6c60ed2900f30c4f82dbd316da5009c4d3d115 Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Wed, 9 Mar 2022 16:26:10 +0200 Subject: [PATCH 3/5] Removed unused variable --- components/EditorNavigation.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/EditorNavigation.tsx b/components/EditorNavigation.tsx index edaf636..87d1338 100644 --- a/components/EditorNavigation.tsx +++ b/components/EditorNavigation.tsx @@ -59,8 +59,6 @@ import { } from "./AlertDialog"; import { styled } from "../stitches.config"; -const DEFAULT_EXTENSION = ".c"; - const ErrorText = styled(Text, { color: "$error", mt: "$1", From 5a598cb091c9ccb995820aaa994a8c30c54e8c95 Mon Sep 17 00:00:00 2001 From: Vaclav Barta Date: Thu, 17 Mar 2022 08:50:40 +0100 Subject: [PATCH 4/5] alternative TimeAgo setup --- pages/_app.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pages/_app.tsx b/pages/_app.tsx index 1b17ecc..8cae2d9 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -16,7 +16,9 @@ import state from "../state"; import TimeAgo from "javascript-time-ago"; import en from "javascript-time-ago/locale/en.json"; import { useSnapshot } from "valtio"; -TimeAgo.addDefaultLocale(en); + +TimeAgo.setDefaultLocale(en.locale); +TimeAgo.addLocale(en) function MyApp({ Component, pageProps: { session, ...pageProps } }: AppProps) { const router = useRouter(); From 3e7c7b1969309e6e7540608f957a23894d018007 Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Thu, 24 Mar 2022 14:49:30 +0200 Subject: [PATCH 5/5] Updated regex --- components/EditorNavigation.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/EditorNavigation.tsx b/components/EditorNavigation.tsx index 87d1338..b41b314 100644 --- a/components/EditorNavigation.tsx +++ b/components/EditorNavigation.tsx @@ -89,12 +89,19 @@ const EditorNavigation = ({ showWat }: { showWat?: boolean }) => { const validateFilename = useCallback( (filename: string): { error: string | null } => { // check if filename already exists + if (!filename) { + return { error: "You need to add filename" }; + } if (snap.files.find((file) => file.name === filename)) { return { error: "Filename already exists." }; } + if (!filename.includes(".") || filename[filename.length - 1] === ".") { + return { error: "Filename should include file extension" }; + } + // check for illegal characters - const ALPHA_NUMERICAL_REGEX = /^[\w,\s-]+\.[A-Za-z0-9]{1,4}$/g; + const ALPHA_NUMERICAL_REGEX = /^[A-Za-z0-9_-]+[.][A-Za-z0-9]{1,4}$/g; if (!filename.match(ALPHA_NUMERICAL_REGEX)) { return { error: `Filename can contain only characters from a-z, A-Z, 0-9, "_" and "-" and it needs to have file extension (e.g. ".c")`,