Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
fix: try...catch trap
Browse files Browse the repository at this point in the history
  • Loading branch information
piquark6046 committed Sep 23, 2024
1 parent d4c9347 commit c0d7c0c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 75 deletions.
59 changes: 0 additions & 59 deletions .github/workflows/purgecache.yml

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@list-kr/microshield",
"version": "4.10.7",
"version": "4.11.0",
"description": "",
"type": "module",
"scripts": {
Expand All @@ -14,7 +14,9 @@
"lint": "tsc --noEmit && eslint sources"
},
"keywords": [],
"files": ["dist"],
"files": [
"dist"
],
"author": {
"name": "PiQuark6046",
"email": "[email protected]",
Expand Down Expand Up @@ -44,6 +46,7 @@
"@list-kr/microshield-token-parser": "^2.0.3",
"@types/node": "^22.5.4",
"crypto-random-string": "^5.0.0",
"error-stack-parser": "^2.1.4",
"parse-domain": "^8.2.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion sources/banner-ios-blocker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @downloadURL https://cdn.jsdelivr.net/npm/@list-kr/microshield@latest/dist/microShield-ios-blocker.user.js
// @license Apache-2.0
//
// @version 4.10.7
// @version 4.11.0
// @author PiQuark6046 and contributors
//
// @match *://ygosu.com/*
Expand Down
2 changes: 1 addition & 1 deletion sources/banner-ios-recovery.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @downloadURL https://cdn.jsdelivr.net/npm/@list-kr/microshield@latest/dist/microShield-ios-recovery.user.js
// @license Apache-2.0
//
// @version 4.10.7
// @version 4.11.0
// @author PiQuark6046 and contributors
//
// @match *://ygosu.com/*
Expand Down
2 changes: 1 addition & 1 deletion sources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @downloadURL https://cdn.jsdelivr.net/npm/@list-kr/microshield@latest/dist/microShield.user.js
// @license Apache-2.0
//
// @version 4.10.7
// @version 4.11.0
// @author PiQuark6046 and contributors
//
// @match *://ygosu.com/*
Expand Down
3 changes: 2 additions & 1 deletion sources/src/adshield/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export const AdshieldDomains = [
export const AdshieldKeywords = [
...AdshieldDomains,
'failed to load website',
'blocking software'
'blocking software',
'bad filtering rule'
]

const AdshieldDomainsize = AdshieldHostableDomains.length
Expand Down
9 changes: 5 additions & 4 deletions sources/src/index-ios-blocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ const Hook = () => {
ProtectFunctionDescriptors(Win.document, 'createElement')
ProtectFunctionDescriptors(Win.document, 'createElementNS')
ProtectFunctionDescriptors(Win, 'alert', {CheckArguments: true})
ProtectFunctionDescriptors(Win, 'confirm', {CheckArguments: true})
ProtectFunctionDescriptors(Win, 'atob', {CheckOutputs: true})
ProtectFunctionDescriptors(Win, 'decodeURI')
ProtectFunctionDescriptors(Win, 'decodeURIComponent')
ProtectFunctionDescriptors(Win, 'confirm', {CheckArguments: true, ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win, 'atob', {CheckOutputs: true, ReturnAs: 'error-report.com'})
ProtectFunctionDescriptors(Win.location, 'reload')
ProtectFunctionDescriptors(Win, 'decodeURI', {ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win, 'decodeURIComponent', {ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win.Promise.prototype, 'catch')
ProtectFunctionDescriptors(Win.Promise, 'resolve')
ProtectFunctionDescriptors(Win.console, 'log', {CheckErrorStack: ['jjang0u.com']})
Expand Down
9 changes: 5 additions & 4 deletions sources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ const Hook = () => {
ProtectFunctionDescriptors(Win.document, 'createElement')
ProtectFunctionDescriptors(Win.document, 'createElementNS')
ProtectFunctionDescriptors(Win, 'alert', {CheckArguments: true})
ProtectFunctionDescriptors(Win, 'confirm', {CheckArguments: true})
ProtectFunctionDescriptors(Win, 'atob', {CheckOutputs: true})
ProtectFunctionDescriptors(Win, 'decodeURI')
ProtectFunctionDescriptors(Win, 'decodeURIComponent')
ProtectFunctionDescriptors(Win, 'confirm', {CheckArguments: true, ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win, 'atob', {CheckOutputs: true, ReturnAs: 'error-report.com'})
ProtectFunctionDescriptors(Win.location, 'reload')
ProtectFunctionDescriptors(Win, 'decodeURI', {ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win, 'decodeURIComponent', {ReturnAs: 'Undefined'})
ProtectFunctionDescriptors(Win.Promise.prototype, 'catch')
ProtectFunctionDescriptors(Win.Promise, 'resolve')
ProtectFunctionDescriptors(Win.console, 'log', {CheckErrorStack: ['jjang0u.com']})
Expand Down
30 changes: 28 additions & 2 deletions sources/src/utils/secret.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import cryptoRandomString from 'crypto-random-string'
import * as ErrorStackParser from 'error-stack-parser'
import {AdshieldKeywords, IsAdShieldCall} from '../adshield/validators.js'
import {Config} from '../config.js'
import {GenerateCallStack} from './call-stack.js'
import {CreateDebug} from './logger.js'
import {HasSubstringSetsInString} from './string.js'
import {MatchSpecificSeq} from './sequence.js'

type unsafeWindow = typeof window
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -27,24 +29,42 @@ export type ProtectedFunctionCreationOptions = Partial<{
CheckOutputs: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
CheckArgumentFunctions: Array<(argArray: any[]) => boolean>;
ReturnAs: 'Banned' | 'Undefined' | string
}>

const PprintCall = (Name?: string, WasBlocked?: boolean, V?: unknown) => {
Debug(WasBlocked ? '-' : '+', 'name=' + Name, 'v=', V, 'stack=', GenerateCallStack())
}

let StackTraces: ReturnType<typeof ErrorStackParser.parse>[] = []

export const ProtectFunction = <F extends Fomulate>(F: F, Options: ProtectedFunctionCreationOptions) => new Proxy(F, {
apply(Target, ThisArg, ArgArray) {
let ReturnAs: 'Banned' | 'Undefined' | 'Normal' | string = 'Normal'
const ErrorInstance = new Error()
const E = () => {
PprintCall(Options.Name, true, ArgArray)

throw new Error()
if (typeof Options.ReturnAs !== 'undefined' &&
MatchSpecificSeq(ErrorStackParser.parse(ErrorInstance), [/[A-Za-z]{1,3}/, undefined, /[A-Za-z]{1,3}/, /Generator\./, /Generator\./])) {
ReturnAs = Options.ReturnAs
} else {
ReturnAs = 'Banned'
}

if (ReturnAs === 'Banned') {
throw new Error()
}
}

if (IsAdShieldCall()) {
E()
}

if (StackTraces.some(StackTrace => JSON.stringify(ErrorStackParser.parse(ErrorInstance)) === JSON.stringify(StackTrace))) {
E()
}

if (Options.CheckArguments) {
for (const Arg of ArgArray.filter(Arg => typeof Arg === 'string') as string[]) {
if (HasSubstringSetsInString(Arg, AdshieldKeywords)) {
Expand Down Expand Up @@ -93,7 +113,13 @@ export const ProtectFunction = <F extends Fomulate>(F: F, Options: ProtectedFunc
PprintCall(Options.Name, false, ArgArray)
}

return Reflect.apply(Target, ThisArg, ArgArray) as unknown
if (ReturnAs === 'Normal') {
return Reflect.apply(Target, ThisArg, ArgArray)
} else if (ReturnAs === 'Undefined') {
return undefined
} else {
return ReturnAs
}
},
setPrototypeOf(Target, V) {
PprintCall(Options.Name, true, V)
Expand Down
10 changes: 10 additions & 0 deletions sources/src/utils/sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type * as ErrorStackParser from 'error-stack-parser'

export function MatchSpecificSeq(StackFrames: ReturnType<typeof ErrorStackParser.parse>, Patterns: Array<RegExp | undefined>): boolean {
return StackFrames.some((StackFrame, I) =>
Patterns.every((Pattern, J) => {
if (I + J >= StackFrames.length) return false
return Pattern === undefined || Pattern.test(typeof StackFrames[I + J].functionName !== 'undefined' ? StackFrames[I + J].functionName as string : '')
})
)
}

0 comments on commit c0d7c0c

Please sign in to comment.