From 58105d12b8c70ff66021fe06e6d1703e2e8d179f Mon Sep 17 00:00:00 2001 From: Mythic Date: Thu, 4 Jan 2024 18:39:02 +0200 Subject: [PATCH] Make the data prefix optional in profile imports Make it possible to import base64 encoded profiles which don't have the data prefix as the data prefix does not provide much practical value but it does make debugging profile issues more difficult as decoding them can't be done as easily with 3rd party tooling. This is a forward looking change and does not impact anything in the immediate future. Once this change has been rolled out, it's possible to remove the prefix from the profile generation logic. --- src/r2mm/mods/ProfileImportExport.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/r2mm/mods/ProfileImportExport.ts b/src/r2mm/mods/ProfileImportExport.ts index 9edab7829..d3435dbc9 100644 --- a/src/r2mm/mods/ProfileImportExport.ts +++ b/src/r2mm/mods/ProfileImportExport.ts @@ -25,17 +25,26 @@ const getDownloadDestination = async (): Promise => { return path.join(cacheDir, "import.r2z"); } -const isProfileDataValid = (profileData: string): boolean => { - return profileData.startsWith(PROFILE_DATA_PREFIX) + +const B64_REGEX = /[A-Za-z0-9+/=]/; + +const isValidBase64 = (profileData: string): boolean => { + return B64_REGEX.test(profileData); } -async function saveDownloadedProfile(profileData: string): Promise { - if (!isProfileDataValid(profileData)) { +const normalizeProfileData = (profileData: string): string => { + if (profileData.startsWith(PROFILE_DATA_PREFIX)) { + profileData = profileData.substring(PROFILE_DATA_PREFIX.length).trim(); + } + if (!isValidBase64(profileData)) { throw new Error("Invalid profile data"); } + return profileData; +} +async function saveDownloadedProfile(profileData: string): Promise { const fs = FsProvider.instance; - const b64 = profileData.substring(PROFILE_DATA_PREFIX.length).trim(); + const b64 = normalizeProfileData(profileData); const decoded = Buffer.from(b64, "base64"); const destination = await getDownloadDestination(); await fs.writeFile(destination, decoded);