Skip to content

Commit 0a258d8

Browse files
committed
fix: 增强Base64验证函数,添加类型检查和解码测试
1 parent f0d1c73 commit 0a258d8

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

_worker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,9 +1062,17 @@ async function 整理成数组(内容) {
10621062
}
10631063

10641064
function isValidBase64(str) {
1065+
if (typeof str !== 'string') return false;
10651066
const cleanStr = str.replace(/\s/g, '');
1066-
const base64Regex = /^[A-Za-z0-9+/=]+$/;
1067-
return base64Regex.test(cleanStr);
1067+
if (cleanStr.length === 0 || cleanStr.length % 4 !== 0) return false;
1068+
const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/;
1069+
if (!base64Regex.test(cleanStr)) return false;
1070+
try {
1071+
atob(cleanStr);
1072+
return true;
1073+
} catch {
1074+
return false;
1075+
}
10681076
}
10691077

10701078
function base64Decode(str) {

0 commit comments

Comments
 (0)