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

Ensure that JavaScript code accessing process.env doesn't immediatly crash the VM #902

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/blugelabs/bluge_segment_api v0.2.0
github.com/blugelabs/query_string v0.3.0
github.com/dop251/goja v0.0.0-20220806120448-1444e6b94559
github.com/dop251/goja_nodejs v0.0.0-20220808115320-bac29516aae9
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/gorilla/handlers v1.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ github.com/dop251/goja v0.0.0-20220806120448-1444e6b94559 h1:S3U65m9SN2p5CJpT3CD
github.com/dop251/goja v0.0.0-20220806120448-1444e6b94559/go.mod h1:1jWwHOtOkEqsfX6tYsufUc7BBTuGHH2ekiJabpkN4CA=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/dop251/goja_nodejs v0.0.0-20220808115320-bac29516aae9 h1:7nszERfxMR5Gyw+M21EbrZZyTtVbRhNdRmtW/Vr3hzc=
github.com/dop251/goja_nodejs v0.0.0-20220808115320-bac29516aae9/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
8 changes: 8 additions & 0 deletions server/runtime_javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

"github.com/dop251/goja"
"github.com/dop251/goja/ast"
"github.com/dop251/goja_nodejs/process"
"github.com/dop251/goja_nodejs/require"
"github.com/gofrs/uuid"
"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/rtapi"
Expand Down Expand Up @@ -1986,6 +1988,12 @@ func evalRuntimeModules(rp *RuntimeProviderJS, modCache *RuntimeJSModuleCache, m
logger := rp.logger

r := goja.New()
// Quite a few JavaScript that were originally written with node.js
// in mind test for something like `process.env.NODE_ENV === "production"`.
// The goja_nodejs libraries provide a subset of the node.js API to
// mitigate these issues.
new(require.Registry).Enable(r)
process.Enable(r)

callbacks := &RuntimeJavascriptCallbacks{
Rpc: make(map[string]string),
Expand Down
10 changes: 10 additions & 0 deletions server/runtime_javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ m.get('a');
}
})
}

func TestMinimalProcessEnv(t *testing.T) {
vm := goja.New()

_, err := vm.RunString("if (process.env.NODE_ENV === 'production') { }")

if err != nil {
t.Fatal("Legit code involving process.env failed")
}
}
13 changes: 13 additions & 0 deletions vendor/github.com/dop251/goja_nodejs/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/github.com/dop251/goja_nodejs/process/module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

214 changes: 214 additions & 0 deletions vendor/github.com/dop251/goja_nodejs/require/module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading