Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue project: When using router to dynamically load. vue files, an error is reported after obscured #1169

Open
80477286 opened this issue Apr 25, 2023 · 1 comment

Comments

@80477286
Copy link

80477286 commented Apr 25, 2023

Current Behavior

if router configuration like :

  const router = createRouter({
      ......
      routes:[{
          name: 'Home',
          path: '/',
          **component: () => import('@/views/Home.vue'),**
      }]
  })

then:javascript-obfuscator to obfuscate and build the code,the browser will report the error,this error is all caused by dynamic loading (component: () => import('@/views/Home.vue'))

if router configuration like :

import Home from "@/views/Home.vue";
const router = createRouter({
    ...
    routes:[{
        name: 'Home',
        path: '/',
        component: Home,
    }]
})

This way, the configuration error disappears, but the page cannot be dynamically loaded

Environment

vite 4.3.x
vue 3.2.x
element-plus 2.3.4
vue-router 4.1.6
javascript-obfuscator 4.0.2
nodejs 18.15.0

Stack trace

dex-cb3a319f.js:1 Uncaught (in promise) TypeError: _0x3f0e01[_0x47a038(...)] is not a function
    at Ee (index-cb3a319f.js:1:15316)
    at component (index-cb3a319f.js:1:16688)
    at Ge (vue-router-5778e0ed.js:6:31994)
    at vue-router-5778e0ed.js:6:46741

Browser tracking information
...
 Ee = function(_0xdb2f96, _0x3f0e01, _0x5495ea) {
    const _0x47a038 = _0x33ee0e;
    if (!_0x3f0e01 || 0x0 === _0x3f0e01[_0x47a038(0x2de)])
        return _0xdb2f96();
    const _0x33d07e = document[_0x47a038(0x30a)](_0x47a038(0x189));
    return Promise[_0x47a038(0x1f6)](_0x3f0e01[_0x47a038(0x16e)](_0x3686ec=>{}   **Error reported here**
         ) : void 0x0;
    }

........
 {
    'name': 'Home',
    'path': '/',
    'alias': [_0x33ee0e(0x2f1), _0x33ee0e(0x1a9)],
    'component': ()=>Ee(()=>import(_0x33ee0e(0x2e5)), _0x33ee0e(0x23d)),      **Call Ee here**
    'meta': {
        'icon': _0x33ee0e(0x18a)
    }
.......

Minimal working example that will help to reproduce issue

router.ts

import {createRouter, createWebHistory} from 'vue-router'
import Home from "@/views/Home.vue";
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes:[{
        name: 'Home',
        path: '/',
        component: Home,
    }]
})
export default router

custom obfuscator plugin:
obfuscator.ts

import type {ObfuscatorOptions} from 'javascript-obfuscator';
import type {FilterPattern} from '@rollup/pluginutils';
import Obfuscator from 'javascript-obfuscator';
import type {Plugin} from 'rollup';

interface ObfuscatorPluginOptions extends ObfuscatorOptions {
    global?: boolean;
    include?: FilterPattern;
    exclude?: FilterPattern;
}

export function obfuscator(options: ObfuscatorPluginOptions = {global: true}): Plugin {
    if (options.global != true && options.global != false) {
        options.global = true;
    }
    return {
        name: 'obfuscator',
        renderChunk: options.global ? (code, {fileName}) => {
                const obfuscated = Obfuscator.obfuscate(code, {...options, inputFileName: fileName,});
                return {
                    code: obfuscated.getObfuscatedCode(),
                    map: options.sourceMap ? obfuscated.getSourceMap() : undefined
                };
            }
            : undefined,
    };
}

vite.config.ts

import {obfuscator} from "./obfuscator";
export default defineConfig({
  build: {
        rollupOptions: {
            output: {
                plugins: [obfuscator({
                    global: true,
                    compact: true,
                    controlFlowFlattening: false,
                    deadCodeInjection: false,
                    debugProtection: false,
                    debugProtectionInterval: 500,
                    disableConsoleOutput: true,
                    identifierNamesGenerator: 'hexadecimal',
                    log: false,
                    numbersToExpressions: false,
                    renameGlobals: false,
                    selfDefending: true,
                    simplify: true,
                    splitStrings: false,
                    stringArray: true,
                    stringArrayCallsTransform: false,
                    stringArrayEncoding: ['base64'],
                    stringArrayIndexShift: true,
                    stringArrayRotate: true,
                    stringArrayShuffle: true,
                    stringArrayWrappersCount: 1,
                    stringArrayWrappersChainedCalls: true,
                    stringArrayWrappersParametersMaxCount: 2,
                    stringArrayWrappersType: 'variable',
                    stringArrayThreshold: 0.75,
                    unicodeEscapeSequence: false
                })],
            }
        }
    }
})

Translation software may not be very accurate if it is not easy to use. Sorry!

@FeiFanLiang
Copy link

vitejs/vite#13023

I saw this fix, but the 4.3.5 version of Vite still seems to have problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants