Skip to content

Commit

Permalink
v8: disable eval() function, add test_eval.js ut.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Zhong committed Nov 3, 2017
1 parent ec8674c commit 2972162
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
17 changes: 9 additions & 8 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import (

func TestRunScriptSource(t *testing.T) {
tests := []struct {
filepath string
filepath string
expectedErr error
}{
{"test/test_console.js"},
{"test/test_storage_handlers.js"},
{"test/test_storage_class.js"},
{"test/test_storage.js"},
{"test/test_ERC20.js"},
{"test/test_console.js", nil},
{"test/test_storage_handlers.js", nil},
{"test/test_storage_class.js", nil},
{"test/test_storage.js", nil},
{"test/test_ERC20.js", nil},
{"test/test_eval.js", ErrExecutionFailed},
}

for _, tt := range tests {
Expand All @@ -50,9 +52,8 @@ func TestRunScriptSource(t *testing.T) {

engine := NewV8Engine(balanceTrie, lcsTrie, gcsTrie)
err = engine.RunScriptSource(string(data))
assert.Equal(t, tt.expectedErr, err)
engine.Dispose()

assert.Nil(t, err)
})
}
}
Expand Down
Binary file modified nf/nvm/native-lib/libv8engine.dylib
Binary file not shown.
6 changes: 3 additions & 3 deletions nf/nvm/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func StorageGetFunc(handler unsafe.Pointer, key *C.char) *C.char {
"func": "nvm.StorageGetFunc",
"handler": uint64(uintptr(handler)),
"key": C.GoString(key),
}).Error("StorageGetFunc get key failed.")
}).Warn("StorageGetFunc get key failed.")
return nil
}
return C.CString(string(val))
Expand All @@ -87,7 +87,7 @@ func StoragePutFunc(handler unsafe.Pointer, key *C.char, value *C.char) int {
"handler": uint64(uintptr(handler)),
"key": C.GoString(key),
"err": err,
}).Error("StoragePutFunc get key failed.")
}).Error("StoragePutFunc put key failed.")
return 1
}
return 0
Expand All @@ -108,7 +108,7 @@ func StorageDelFunc(handler unsafe.Pointer, key *C.char) int {
"handler": uint64(uintptr(handler)),
"key": C.GoString(key),
"err": err,
}).Error("StorageDelFunc get key failed.")
}).Warn("StorageDelFunc del key failed.")
return 1
}

Expand Down
18 changes: 18 additions & 0 deletions nf/nvm/test/ERC20.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//

'use strict';

var StandardToken = function () {
Expand Down
20 changes: 20 additions & 0 deletions nf/nvm/test/test_eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
'use strict';

eval("console.error(\"this log is executed by eval, please disable eval function ASAP.\");");
4 changes: 4 additions & 0 deletions nf/nvm/v8/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ int RunScriptSource(V8Engine *e, const char *data, void *lcsHandler,

// Create a new context.
Local<Context> context = Context::New(isolate, NULL, globalTpl);

// Disable eval().
context->AllowCodeGenerationFromStrings(false);

// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);

Expand Down

0 comments on commit 2972162

Please sign in to comment.