Skip to content

Commit 1a9bd08

Browse files
committed
feat: goja_nodejs libraries
Signed-off-by: Dwi Siswanto <[email protected]>
1 parent 160eab9 commit 1a9bd08

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed

pkg/js/compiler/pool.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/dop251/goja_nodejs/require"
1313
"github.com/kitabisa/go-ci"
1414
"github.com/projectdiscovery/gologger"
15+
"github.com/projectdiscovery/nuclei/v3/pkg/js"
1516
_ "github.com/projectdiscovery/nuclei/v3/pkg/js/generated/go/libbytes"
1617
_ "github.com/projectdiscovery/nuclei/v3/pkg/js/generated/go/libfs"
1718
_ "github.com/projectdiscovery/nuclei/v3/pkg/js/generated/go/libikev2"
@@ -214,6 +215,9 @@ func createNewRuntime() *goja.Runtime {
214215
if err := global.RegisterNativeScripts(runtime); err != nil {
215216
gologger.Error().Msgf("Could not register scripts: %s\n", err)
216217
}
218+
219+
js.RegisterNodeModules(runtime)
220+
217221
return runtime
218222
}
219223

pkg/js/js.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package js
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"path/filepath"
7+
8+
"github.com/dop251/goja"
9+
"github.com/dop251/goja_nodejs/require"
10+
)
11+
12+
//go:embed node_libraries
13+
var nodeLibraries embed.FS
14+
15+
func RegisterNodeModules(runtime *goja.Runtime) {
16+
registry := require.NewRegistry(
17+
require.WithGlobalFolders("."),
18+
require.WithLoader(func(path string) ([]byte, error) {
19+
path = filepath.Join("node_libraries", path, "index.js")
20+
21+
data, err := nodeLibraries.ReadFile(path)
22+
if err != nil {
23+
return nil, fmt.Errorf("could not load module %q: %w", path, err)
24+
}
25+
26+
return data, nil
27+
}),
28+
)
29+
30+
registry.Enable(runtime)
31+
}

pkg/js/node_libraries/.add.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
NPM_PREFIX=".modules"
4+
NPM_MODULE=$1
5+
MODULE_NAME=$2
6+
7+
if [[ -z "${MODULE_NAME}" ]]; then
8+
MODULE_NAME="${NPM_MODULE}"
9+
fi
10+
11+
npm i "${NPM_MODULE}" --prefix "${NPM_PREFIX}" --save-dev
12+
mkdir -p "@core/${MODULE_NAME}"
13+
echo "module.exports = require('${MODULE_NAME}');" > "@core/${MODULE_NAME}/lib.js"
14+
NODE_PATH="${NPM_PREFIX}/node_modules" esbuild \
15+
--minify \
16+
--format=cjs \
17+
--platform=browser \
18+
--outfile="@core/${MODULE_NAME}/index.js" \
19+
--bundle "@core/${MODULE_NAME}/lib.js"
20+
rm -rf "@core/${MODULE_NAME}/lib.js"

pkg/js/node_libraries/@core/assert/index.js

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/js/node_libraries/@core/buffer/index.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/js/utils/nucleijs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"sync"
88

99
"github.com/dop251/goja"
10+
"github.com/projectdiscovery/nuclei/v3/pkg/js"
1011
)
1112

1213
// temporary on demand runtime to throw errors when vm is not available
1314
var (
1415
tmpRuntime *goja.Runtime
1516
runtimeInit func() = sync.OnceFunc(func() {
1617
tmpRuntime = goja.New()
18+
js.RegisterNodeModules(tmpRuntime)
1719
})
1820
)
1921

pkg/protocols/common/protocolstate/js.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/dop251/goja"
55
"github.com/dop251/goja/parser"
66
"github.com/projectdiscovery/gologger"
7+
"github.com/projectdiscovery/nuclei/v3/pkg/js"
78
)
89

910
// NewJSRuntime returns a new javascript runtime
@@ -12,9 +13,13 @@ import (
1213
func NewJSRuntime() *goja.Runtime {
1314
vm := goja.New()
1415
vm.SetParserOptions(parser.WithDisableSourceMaps)
16+
1517
// disable eval by default
1618
if err := vm.Set("eval", "undefined"); err != nil {
1719
gologger.Error().Msgf("could not set eval to undefined: %s", err)
1820
}
21+
22+
js.RegisterNodeModules(vm)
23+
1924
return vm
2025
}

0 commit comments

Comments
 (0)