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);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createCell creates a new cell if it does not exists.
|
|
|
|
func (j *Jail) createCell(chatID string) (*Cell, error) {
|
|
|
|
j.cellsMx.Lock()
|
|
|
|
defer j.cellsMx.Unlock()
|
2017-07-13 11:04:47 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
if cell, ok := j.cells[chatID]; ok {
|
|
|
|
return cell, fmt.Errorf("cell with id '%s' already exists", chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
cell, err := NewCell(chatID)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return j.createCell(chatID)
|
|
|
|
}
|
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.
|
|
|
|
func (j *Jail) createAndInitCell(chatID string, code ...string) (*Cell, error) {
|
|
|
|
cell, err := j.createCell(chatID)
|
2017-09-02 17:04:23 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +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 {
|
|
|
|
return nil, err
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// Run custom user code
|
|
|
|
for _, js := range code {
|
|
|
|
_, err := cell.Run(js)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return cell, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAndInitCell creates and initializes new Cell. Additionally,
|
|
|
|
// it creates a `catalog` variable in the VM.
|
|
|
|
// It returns the response as a JSON string.
|
|
|
|
func (j *Jail) CreateAndInitCell(chatID string, code ...string) string {
|
|
|
|
cell, err := j.createAndInitCell(chatID, code...)
|
|
|
|
if err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-11-28 18:30:25 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return j.makeCatalogVariable(cell)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// DEPRECATED
|
|
|
|
func (j *Jail) Parse(chatID, code string) string {
|
|
|
|
cell, err := j.cell(chatID)
|
|
|
|
if err != nil {
|
2017-11-23 12:47:20 +00:00
|
|
|
// cell does not exist, so create and init it
|
2017-11-23 12:37:59 +00:00
|
|
|
cell, err = j.createAndInitCell(chatID, code)
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:37:59 +00:00
|
|
|
return j.makeCatalogVariable(cell)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// makeCatalogVariable provides `catalog` as a global variable.
|
|
|
|
// TODO(divan): this can and should be implemented outside of jail,
|
|
|
|
// on a clojure side. Moving this into separate method to nuke it later
|
|
|
|
// easier.
|
|
|
|
func (j *Jail) makeCatalogVariable(cell *Cell) string {
|
|
|
|
_, err := cell.Run(`var catalog = JSON.stringify(_status_catalog)`)
|
|
|
|
if err != nil {
|
|
|
|
return newJailErrorResponse(err)
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
value, err := cell.Get("catalog")
|
2017-05-03 14:24:48 +00:00
|
|
|
if err != nil {
|
2017-11-07 17:36:42 +00:00
|
|
|
return newJailErrorResponse(err)
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +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
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func newJailResultResponse(result otto.Value) string {
|
|
|
|
return `{"result": ` + result.String() + `}`
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|