@@ -17,6 +17,7 @@ import CacheKey from "@App/pkg/utils/cache_key";
1717import { isText } from "@App/pkg/utils/istextorbinary" ;
1818import Manager from "../manager" ;
1919import { calculateHashFromArrayBuffer } from "@App/pkg/utils/crypto" ;
20+ import { base64ToHex , isBase64 } from "./utils" ;
2021
2122// 资源管理器,负责资源的更新获取等操作
2223
@@ -126,7 +127,7 @@ export class ResourceManager extends Manager {
126127 }
127128 const ret : { [ key : string ] : Resource } = { } ;
128129 await Promise . allSettled (
129- script . metadata . require . map ( async ( u ) => {
130+ script . metadata ! . require ! . map ( async ( u ) => {
130131 const res = await this . getResource ( script . id , u , "require-css" ) ;
131132 if ( res ) {
132133 ret [ u ] = res ;
@@ -188,7 +189,7 @@ export class ResourceManager extends Manager {
188189 }
189190 const ret : { [ key : string ] : Resource } = { } ;
190191 await Promise . allSettled (
191- script . metadata . require . map ( async ( u ) => {
192+ script . metadata ! . require ! . map ( async ( u ) => {
192193 const res = await this . checkResource ( script . id , u , "require-css" ) ;
193194 if ( res ) {
194195 ret [ u ] = res ;
@@ -339,13 +340,21 @@ export class ResourceManager extends Manager {
339340 if ( resource ) {
340341 // 校验hash
341342 if ( u . hash ) {
342- if (
343- ( u . hash . md5 && u . hash . md5 !== resource . hash . md5 ) ||
344- ( u . hash . sha1 && u . hash . sha1 !== resource . hash . sha1 ) ||
345- ( u . hash . sha256 && u . hash . sha256 !== resource . hash . sha256 ) ||
346- ( u . hash . sha384 && u . hash . sha384 !== resource . hash . sha384 ) ||
347- ( u . hash . sha512 && u . hash . sha512 !== resource . hash . sha512 )
348- ) {
343+ let flag = true ;
344+ Object . keys ( u . hash ) . forEach ( ( key ) => {
345+ if ( key in resource . hash ) {
346+ let hex = u . hash ! [ key ] ;
347+ if ( isBase64 ( hex ) ) {
348+ // 转换为hash进对比
349+ hex = base64ToHex ( u . hash ! [ key ] ) ;
350+ }
351+ // 对比普通的hash
352+ if ( resource . hash [ key as keyof ResourceHash ] !== hex ) {
353+ flag = false ;
354+ }
355+ }
356+ } ) ;
357+ if ( ! flag ) {
349358 resource . content = `console.warn("ScriptCat: couldn't load resource from URL ${ url } due to a SRI error ");` ;
350359 }
351360 }
@@ -406,7 +415,8 @@ export class ResourceManager extends Manager {
406415 if ( kv . length < 2 ) {
407416 return ;
408417 }
409- hash [ kv [ 0 ] ] = kv [ 1 ] . toLocaleLowerCase ( ) ;
418+ const [ key , value ] = kv ;
419+ hash [ key ] = value ;
410420 } ) ;
411421 return { url : urls [ 0 ] , hash } ;
412422 }
0 commit comments