From 7d989fed8f19ba6689c731733f9a933665234693 Mon Sep 17 00:00:00 2001 From: Savely Savianok <1986developer@gmail.com> Date: Mon, 30 Dec 2024 02:08:50 +0300 Subject: [PATCH] feat: added validation for TOTP link --- setting/index.js | 5 +++++ setting/utils/queryParser.js | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/setting/index.js b/setting/index.js index 5cebbe9..9c136f4 100644 --- a/setting/index.js +++ b/setting/index.js @@ -72,8 +72,13 @@ function GetTOTPList(storage){ placeholder: "otpauth://", label: "Change OTP link", onChange: (changes) => { + try{ storage[elementId] = getTOTPByLink(changes) updateStorage(storage) + } + catch(err){ + console.log(err) + } }, labelStyle: { backgroundColor: "#14213D", diff --git a/setting/utils/queryParser.js b/setting/utils/queryParser.js index f016b32..98cff2d 100644 --- a/setting/utils/queryParser.js +++ b/setting/utils/queryParser.js @@ -3,6 +3,7 @@ import { TOTP } from "../../lib/totp-quickjs"; const otpScheme = "otpauth:/"; export function getTOTPByLink(link){ + try{ let args = link.split("/", otpScheme.length) let type = args[2] //Returns 'hotp' or 'totp' let issuer = args[3].split(':')[0]?.split('?')[0] //Returns issuer @@ -11,12 +12,15 @@ export function getTOTPByLink(link){ let period = args[3].split('period=')[1]?.split('&')[0] //Returns period let digits = args[3].split('digits=')[1]?.split('&')[0] //Returns digits let algorithm = args[3].split('algorithm=')[1]?.split('&')[0] //Returns algorithm - + } + catch(err){ + throw new Error("Link is not valid") + } if(type.toLowerCase() != 'totp') - return Error("Type is not valid, requires 'TOTP'") + throw new Error("Type is not valid, requires 'TOTP'") if(secret === undefined) - return Error("Secret not defined") + throw new Error("Secret not defined") issuer = issuer.replace("%20", " ") client = client.replace("%20", " ")