2016-09-11 11:44:14 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-09-18 12:13:32 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/internal/vm"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-04-06 19:36:55 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2016-10-07 14:48:36 +00:00
|
|
|
)
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
var (
|
2017-10-06 16:52:26 +00:00
|
|
|
// FIXME(tiabc): Get rid of this global variable. Move it to a constructor or initialization.
|
|
|
|
web3JSCode = static.MustAsset("scripts/web3.js")
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//ErrInvalidJail - error jail init env
|
2016-09-11 11:44:14 +00:00
|
|
|
ErrInvalidJail = errors.New("jail environment is not properly initialized")
|
|
|
|
)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// Jail represents jailed environment inside of which we hold multiple cells.
|
|
|
|
// Each cell is a separate JavaScript VM.
|
|
|
|
type Jail struct {
|
2017-10-06 16:52:26 +00:00
|
|
|
nodeManager common.NodeManager
|
|
|
|
baseJSCode string // JavaScript used to initialize all new cells with
|
|
|
|
|
|
|
|
cellsMx sync.RWMutex
|
|
|
|
cells map[string]*Cell // jail supports running many isolated instances of jailed runtime
|
|
|
|
|
|
|
|
vm *vm.VM // vm for internal otto related tasks (see Send method)
|
2017-07-13 11:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
// New returns new Jail environment with the associated NodeManager.
|
|
|
|
// It's caller responsibility to call jail.Stop() when jail is not needed.
|
|
|
|
func New(nodeManager common.NodeManager) *Jail {
|
2017-05-16 12:09:52 +00:00
|
|
|
return &Jail{
|
2017-10-06 16:52:26 +00:00
|
|
|
nodeManager: nodeManager,
|
|
|
|
cells: make(map[string]*Cell),
|
|
|
|
vm: vm.New(otto.New()),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// BaseJS allows to setup initial JavaScript to be loaded on each jail.Parse().
|
2017-05-16 12:09:52 +00:00
|
|
|
func (jail *Jail) BaseJS(js string) {
|
|
|
|
jail.baseJSCode = js
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// NewCell initializes and returns a new jail cell.
|
|
|
|
func (jail *Jail) NewCell(chatID string) (common.JailCell, error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
if jail == nil {
|
|
|
|
return nil, ErrInvalidJail
|
|
|
|
}
|
|
|
|
|
2017-07-13 11:04:47 +00:00
|
|
|
vm := otto.New()
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := newCell(chatID, vm)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
jail.cellsMx.Lock()
|
2017-09-02 17:04:23 +00:00
|
|
|
jail.cells[chatID] = cell
|
2017-10-06 16:52:26 +00:00
|
|
|
jail.cellsMx.Unlock()
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
return cell, nil
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
// Stop stops jail and all assosiacted cells.
|
|
|
|
func (jail *Jail) Stop() {
|
|
|
|
jail.cellsMx.Lock()
|
|
|
|
defer jail.cellsMx.Unlock()
|
|
|
|
|
|
|
|
for _, cell := range jail.cells {
|
|
|
|
cell.Stop()
|
|
|
|
}
|
2017-10-13 12:13:20 +00:00
|
|
|
// TODO(tiabc): Move this initialisation to a proper place.
|
|
|
|
jail.cells = make(map[string]*Cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// Cell returns the existing instance of Cell.
|
|
|
|
func (jail *Jail) Cell(chatID string) (common.JailCell, error) {
|
2017-10-06 16:52:26 +00:00
|
|
|
jail.cellsMx.RLock()
|
|
|
|
defer jail.cellsMx.RUnlock()
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
cell, ok := jail.cells[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cell[%s] doesn't exist", chatID)
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
2017-07-13 11:04:47 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return cell, nil
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// Parse creates a new jail cell context, with the given chatID as identifier.
|
2017-05-03 14:24:48 +00:00
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
2017-09-02 17:04:23 +00:00
|
|
|
func (jail *Jail) Parse(chatID, js string) string {
|
2016-09-11 11:44:14 +00:00
|
|
|
if jail == nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(ErrInvalidJail.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := jail.Cell(chatID)
|
|
|
|
if err != nil {
|
|
|
|
if _, mkerr := jail.NewCell(chatID); mkerr != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return makeError(mkerr.Error())
|
|
|
|
}
|
2016-10-13 18:02:48 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, _ = jail.Cell(chatID)
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
2016-10-07 14:48:36 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// init jeth and its handlers
|
2017-09-02 17:04:23 +00:00
|
|
|
if err = cell.Set("jeth", struct{}{}); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
if err = registerHandlers(jail, cell, chatID); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
initJs := jail.baseJSCode + ";"
|
2017-09-02 17:04:23 +00:00
|
|
|
if _, err = cell.Run(initJs); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-11-28 18:30:25 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
jjs := string(web3JSCode) + `
|
2016-09-11 11:44:14 +00:00
|
|
|
var Web3 = require('web3');
|
|
|
|
var web3 = new Web3(jeth);
|
|
|
|
var Bignumber = require("bignumber.js");
|
|
|
|
function bn(val){
|
|
|
|
return new Bignumber(val);
|
|
|
|
}
|
|
|
|
` + js + "; var catalog = JSON.stringify(_status_catalog);"
|
2017-09-02 17:04:23 +00:00
|
|
|
if _, err = cell.Run(jjs); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
res, err := cell.Get("catalog")
|
2017-05-03 14:24:48 +00:00
|
|
|
if err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeResult(res.String(), err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// Call executes the `call` function w/i a jail cell context identified by the chatID.
|
2017-09-02 17:04:23 +00:00
|
|
|
func (jail *Jail) Call(chatID, this, args string) string {
|
|
|
|
cell, err := jail.Cell(chatID)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeError(err.Error())
|
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
res, err := cell.Call("call", nil, this, args)
|
2016-10-13 18:02:48 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return makeResult(res.String(), err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
// Send is a wrapper for executing RPC calls from within Otto VM.
|
|
|
|
// It uses own jail's VM instance instead of cell's one to
|
|
|
|
// increase safety of cell's vm usage.
|
|
|
|
// TODO(divan): investigate if it's possible to do conversions
|
|
|
|
// withouth involving otto code at all.
|
2017-05-03 14:24:48 +00:00
|
|
|
// nolint: errcheck, unparam
|
2017-10-06 16:52:26 +00:00
|
|
|
func (jail *Jail) Send(call otto.FunctionCall) otto.Value {
|
|
|
|
request, err := jail.vm.Call("JSON.stringify", nil, call.Argument(0))
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-09-18 12:13:32 +00:00
|
|
|
throwJSException(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
rpc := jail.nodeManager.RPCClient()
|
|
|
|
// TODO(divan): remove this check as soon as jail cells have
|
|
|
|
// proper cancellation mechanism implemented.
|
|
|
|
if rpc == nil {
|
|
|
|
throwJSException(fmt.Errorf("Error getting RPC client. Node stopped?"))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
response := rpc.CallRaw(request.String())
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
// unmarshal response to pass to otto
|
|
|
|
var resp interface{}
|
|
|
|
err = json.Unmarshal([]byte(response), &resp)
|
2017-09-18 12:13:32 +00:00
|
|
|
if err != nil {
|
2017-10-06 16:52:26 +00:00
|
|
|
throwJSException(fmt.Errorf("Error unmarshalling result: %s", err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
respValue, err := jail.vm.ToValue(resp)
|
2017-09-18 12:13:32 +00:00
|
|
|
if err != nil {
|
2017-10-06 16:52:26 +00:00
|
|
|
throwJSException(fmt.Errorf("Error converting result to Otto's value: %s", err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
|
|
|
return respValue
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 12:13:32 +00:00
|
|
|
func newErrorResponse(msg string, id interface{}) map[string]interface{} {
|
2016-09-11 11:44:14 +00:00
|
|
|
// Bundle the error into a JSON RPC call response
|
2017-09-18 12:13:32 +00:00
|
|
|
return map[string]interface{}{
|
2017-09-14 20:14:31 +00:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": id,
|
|
|
|
"error": map[string]interface{}{
|
2017-09-18 12:13:32 +00:00
|
|
|
"code": -32603, // Internal JSON-RPC Error, see http://www.jsonrpc.org/specification#error_object
|
2017-09-14 20:14:31 +00:00
|
|
|
"message": msg,
|
|
|
|
},
|
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newErrorResponseOtto(vm *vm.VM, msg string, id interface{}) otto.Value {
|
|
|
|
// TODO(tiabc): Handle errors.
|
|
|
|
errResp, _ := json.Marshal(newErrorResponse(msg, id))
|
|
|
|
errRespVal, _ := vm.Run("(" + string(errResp) + ")")
|
|
|
|
return errRespVal
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
func newResultResponse(vm *otto.Otto, result interface{}) otto.Value {
|
|
|
|
resp, _ := vm.Object(`({"jsonrpc":"2.0"})`)
|
2017-05-03 14:24:48 +00:00
|
|
|
resp.Set("result", result) // nolint: errcheck
|
2016-12-18 20:36:17 +00:00
|
|
|
|
|
|
|
return resp.Value()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
// throwJSException panics on an otto.Value. The Otto VM will recover from the
|
|
|
|
// Go panic and throw msg as a JavaScript error.
|
2017-10-20 09:06:22 +00:00
|
|
|
// nolint: unparam
|
2017-09-18 12:13:32 +00:00
|
|
|
func throwJSException(msg error) otto.Value {
|
|
|
|
val, err := otto.ToValue(msg.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-09-18 12:13:32 +00:00
|
|
|
log.Error(fmt.Sprintf("Failed to serialize JavaScript exception %v: %v", msg.Error(), err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
panic(val)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// JSONError is wrapper around errors, that are sent upwards
|
|
|
|
type JSONError struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeError(error string) string {
|
|
|
|
str := JSONError{
|
2016-09-11 11:44:14 +00:00
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&str)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
func makeResult(res string, err error) string {
|
2016-09-11 11:44:14 +00:00
|
|
|
var out string
|
|
|
|
if err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
out = makeError(err.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
} else {
|
|
|
|
if "undefined" == res {
|
|
|
|
res = "null"
|
|
|
|
}
|
|
|
|
out = fmt.Sprintf(`{"result": %s}`, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|