2016-09-11 11:44:14 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-07 17:36:42 +00:00
|
|
|
"strings"
|
2016-09-11 11:44:14 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-11-07 17:36:42 +00:00
|
|
|
"github.com/status-im/status-go/geth/rpc"
|
2017-04-06 19:36:55 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2016-10-07 14:48:36 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
const (
|
|
|
|
web3InstanceCode = `
|
|
|
|
var Web3 = require('web3');
|
|
|
|
var web3 = new Web3(jeth);
|
|
|
|
var Bignumber = require("bignumber.js");
|
|
|
|
function bn(val) {
|
|
|
|
return new Bignumber(val);
|
|
|
|
}
|
|
|
|
`
|
2018-01-09 16:53:33 +00:00
|
|
|
// EmptyResponse is returned when cell is successfully created and initialized
|
|
|
|
// but no additional JS was provided to the initialization method.
|
|
|
|
EmptyResponse = `{"result": ""}`
|
2017-11-07 17:36:42 +00:00
|
|
|
)
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
var (
|
|
|
|
web3Code = string(static.MustAsset("scripts/web3.js"))
|
|
|
|
// ErrNoRPCClient is returned when an RPC client is required but it's nil.
|
|
|
|
ErrNoRPCClient = errors.New("RPC client is not available")
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// RPCClientProvider is an interface that provides a way
|
|
|
|
// to obtain an rpc.Client.
|
|
|
|
type RPCClientProvider interface {
|
|
|
|
RPCClient() *rpc.Client
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Jail manages multiple JavaScript execution contexts (JavaScript VMs) called cells.
|
|
|
|
// Each cell is a separate VM with web3.js set up.
|
|
|
|
//
|
|
|
|
// As rpc.Client might not be available during Jail initialization,
|
|
|
|
// a provider function is used.
|
|
|
|
type Jail struct {
|
|
|
|
rpcClientProvider RPCClientProvider
|
|
|
|
baseJS string
|
|
|
|
cellsMx sync.RWMutex
|
|
|
|
cells map[string]*Cell
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// New returns a new Jail.
|
|
|
|
func New(provider RPCClientProvider) *Jail {
|
|
|
|
return NewWithBaseJS(provider, "")
|
2017-07-13 11:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// NewWithBaseJS returns a new Jail with base JS configured.
|
|
|
|
func NewWithBaseJS(provider RPCClientProvider, code string) *Jail {
|
2017-05-16 12:09:52 +00:00
|
|
|
return &Jail{
|
2017-11-07 17:36:42 +00:00
|
|
|
rpcClientProvider: provider,
|
|
|
|
baseJS: code,
|
|
|
|
cells: make(map[string]*Cell),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// SetBaseJS sets initial JavaScript code loaded to each new cell.
|
|
|
|
func (j *Jail) SetBaseJS(js string) {
|
|
|
|
j.baseJS = js
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Stop stops jail and all assosiacted cells.
|
|
|
|
func (j *Jail) Stop() {
|
|
|
|
j.cellsMx.Lock()
|
|
|
|
defer j.cellsMx.Unlock()
|
|
|
|
|
|
|
|
for _, cell := range j.cells {
|
|
|
|
cell.Stop() //nolint: errcheck
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// TODO(tiabc): Move this initialisation to a proper place.
|
|
|
|
j.cells = make(map[string]*Cell)
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:32:30 +00:00
|
|
|
// obtainCell returns an existing cell for given ID or
|
|
|
|
// creates a new one if it does not exist.
|
|
|
|
// Passing in true as a second argument will cause a non-nil error if the
|
|
|
|
// cell already exists.
|
|
|
|
func (j *Jail) obtainCell(chatID string, expectNew bool) (cell *Cell, err error) {
|
2017-11-07 17:36:42 +00:00
|
|
|
j.cellsMx.Lock()
|
|
|
|
defer j.cellsMx.Unlock()
|
2017-07-13 11:04:47 +00:00
|
|
|
|
2017-12-08 15:32:30 +00:00
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if cell, ok = j.cells[chatID]; ok {
|
|
|
|
// Return a non-nil error if a new cell was expected
|
|
|
|
if expectNew {
|
|
|
|
err = fmt.Errorf("cell with id '%s' already exists", chatID)
|
|
|
|
}
|
|
|
|
return
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:32:30 +00:00
|
|
|
cell, err = NewCell(chatID)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
2017-12-08 15:32:30 +00:00
|
|
|
return
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
j.cells[chatID] = cell
|
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-11-07 17:36:42 +00:00
|
|
|
// CreateCell creates a new cell. It returns an error
|
|
|
|
// if a cell with a given ID already exists.
|
|
|
|
func (j *Jail) CreateCell(chatID string) (common.JailCell, error) {
|
2017-12-08 15:32:30 +00:00
|
|
|
return j.obtainCell(chatID, true)
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// initCell initializes a cell with default JavaScript handlers and user code.
|
|
|
|
func (j *Jail) initCell(cell *Cell) error {
|
|
|
|
// Register objects being a bridge between Go and JavaScript.
|
|
|
|
if err := registerWeb3Provider(j, cell); err != nil {
|
|
|
|
return err
|
2017-10-06 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
if err := registerStatusSignals(cell); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Run some initial JS code to provide some global objects.
|
|
|
|
c := []string{
|
|
|
|
j.baseJS,
|
|
|
|
web3Code,
|
|
|
|
web3InstanceCode,
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
2017-07-13 11:04:47 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
_, err := cell.Run(strings.Join(c, ";"))
|
|
|
|
return err
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// CreateAndInitCell creates and initializes a new Cell.
|
2018-01-09 16:53:33 +00:00
|
|
|
func (j *Jail) createAndInitCell(chatID string, extraCode ...string) (*Cell, string, error) {
|
2017-12-08 15:32:30 +00:00
|
|
|
cell, err := j.obtainCell(chatID, false)
|
2017-09-02 17:04:23 +00:00
|
|
|
if err != nil {
|
2018-01-09 16:53:33 +00:00
|
|
|
return nil, "", err
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
2016-10-07 14:48:36 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
if err := j.initCell(cell); err != nil {
|
2018-01-09 16:53:33 +00:00
|
|
|
return nil, "", err
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
response := EmptyResponse
|
|
|
|
|
|
|
|
if len(extraCode) > 0 {
|
|
|
|
result, err := cell.Run(strings.Join(extraCode, ";"))
|
2017-11-07 17:36:42 +00:00
|
|
|
if err != nil {
|
2018-01-09 16:53:33 +00:00
|
|
|
return nil, "", err
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
2018-02-08 11:25:01 +00:00
|
|
|
|
|
|
|
response = newJailResultResponse(formatOttoValue(result))
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
return cell, response, nil
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// CreateAndInitCell creates and initializes new Cell.
|
2017-11-07 17:36:42 +00:00
|
|
|
func (j *Jail) CreateAndInitCell(chatID string, code ...string) string {
|
2018-01-09 16:53:33 +00:00
|
|
|
_, result, err := j.createAndInitCell(chatID, code...)
|
2017-11-07 17:36:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-11-28 18:30:25 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
return result
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 12:37:59 +00:00
|
|
|
// Parse creates a new jail cell context, with the given chatID as identifier.
|
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
2017-11-23 12:51:52 +00:00
|
|
|
// DEPRECATED in favour of CreateAndInitCell.
|
2017-11-23 12:37:59 +00:00
|
|
|
func (j *Jail) Parse(chatID, code string) string {
|
|
|
|
cell, err := j.cell(chatID)
|
2018-01-09 16:53:33 +00:00
|
|
|
result := EmptyResponse
|
2017-11-23 12:37:59 +00:00
|
|
|
if err != nil {
|
2017-11-23 12:47:20 +00:00
|
|
|
// cell does not exist, so create and init it
|
2018-01-09 16:53:33 +00:00
|
|
|
cell, result, err = j.createAndInitCell(chatID, code)
|
2017-11-23 12:37:59 +00:00
|
|
|
} else {
|
2017-11-23 12:47:20 +00:00
|
|
|
// cell already exists, so just reinit it
|
|
|
|
err = j.initCell(cell)
|
2017-11-23 12:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:47:20 +00:00
|
|
|
if _, err = cell.Run(code); err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
return result
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
func (j *Jail) cell(chatID string) (*Cell, error) {
|
|
|
|
j.cellsMx.RLock()
|
|
|
|
defer j.cellsMx.RUnlock()
|
|
|
|
|
|
|
|
cell, ok := j.cells[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cell '%s' not found", chatID)
|
2017-07-13 11:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return cell, nil
|
|
|
|
}
|
2016-10-13 18:02:48 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Cell returns a cell by chatID. If it does not exist, error is returned.
|
|
|
|
// Required by the Backend.
|
|
|
|
func (j *Jail) Cell(chatID string) (common.JailCell, error) {
|
|
|
|
return j.cell(chatID)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Execute allows to run arbitrary JS code within a cell.
|
|
|
|
func (j *Jail) Execute(chatID, code string) string {
|
|
|
|
cell, err := j.cell(chatID)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +00:00
|
|
|
return newJailErrorResponse(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
value, err := cell.Run(code)
|
|
|
|
if err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return value.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call executes the `call` function within a cell with chatID.
|
|
|
|
// Returns a string being a valid JS code. In case of a successful result,
|
|
|
|
// it's {"result": any}. In case of an error: {"error": "some error"}.
|
|
|
|
//
|
|
|
|
// Call calls commands from `_status_catalog`.
|
|
|
|
// commandPath is an array of properties to retrieve a function.
|
|
|
|
// For instance:
|
|
|
|
// `["prop1", "prop2"]` is translated to `_status_catalog["prop1"]["prop2"]`.
|
|
|
|
func (j *Jail) Call(chatID, commandPath, args string) string {
|
|
|
|
cell, err := j.cell(chatID)
|
2017-09-18 12:13:32 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +00:00
|
|
|
return newJailErrorResponse(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-11-07 17:36:42 +00:00
|
|
|
|
|
|
|
value, err := cell.Call("call", nil, commandPath, args)
|
2017-09-18 12:13:32 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +00:00
|
|
|
return newJailErrorResponse(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return newJailResultResponse(value)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// RPCClient returns an rpc.Client.
|
|
|
|
func (j *Jail) RPCClient() *rpc.Client {
|
|
|
|
if j.rpcClientProvider == nil {
|
|
|
|
return nil
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return j.rpcClientProvider.RPCClient()
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// sendRPCCall executes a raw JSON-RPC request.
|
|
|
|
func (j *Jail) sendRPCCall(request string) (interface{}, error) {
|
|
|
|
client := j.RPCClient()
|
|
|
|
if client == nil {
|
|
|
|
return nil, ErrNoRPCClient
|
|
|
|
}
|
2016-12-18 20:36:17 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
rawResponse := client.CallRaw(request)
|
2016-12-18 20:36:17 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
var response interface{}
|
|
|
|
if err := json.Unmarshal([]byte(rawResponse), &response); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response: %s", err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return response, nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// newJailErrorResponse returns an error.
|
|
|
|
func newJailErrorResponse(err error) string {
|
|
|
|
response := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: err.Error(),
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
rawResponse, err := json.Marshal(response)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +00:00
|
|
|
return `{"error": "` + err.Error() + `"}`
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return string(rawResponse)
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:25:01 +00:00
|
|
|
// formatOttoValue : formats an otto value string to be processed as valid
|
|
|
|
// javascript code
|
|
|
|
func formatOttoValue(result otto.Value) otto.Value {
|
|
|
|
val := result.String()
|
|
|
|
if result.IsString() {
|
|
|
|
if val != "undefined" {
|
|
|
|
val = fmt.Sprintf(`"%s"`, strings.Replace(val, `"`, `\"`, -1))
|
|
|
|
result, _ = otto.ToValue(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// newJailResultResponse returns a string that is a valid JavaScript code.
|
|
|
|
// Marshaling is not required as result.String() produces a string
|
|
|
|
// that is a valid JavaScript code.
|
2018-02-08 11:25:01 +00:00
|
|
|
func newJailResultResponse(value otto.Value) string {
|
|
|
|
res := value.String()
|
|
|
|
if res == "undefined" {
|
|
|
|
res = "null"
|
|
|
|
}
|
|
|
|
return `{"result":` + res + `}`
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|