Giter Site home page Giter Site logo

goscript's Introduction

Goscript logo by Rob Baines

goscript

Goscript: Runtime execution of Go code.

Usage

A Goscript is a string that contains valid Go code that can be executed by Goscript.

The script string defines a goscript function that takes in zero or more arguments, and returns two; a value and an error.

You then start the script like this:

script := goscript.New(`
	
	import (
		"strings"
	)
	
	func goscript(name string) (string, error) {
		return "Hello " + strings.ToUpper(name), nil
	}
	
`)
defer script.Close()

And make calls to the goscript function like this:

greeting, err := script.Execute("Mat")
if err != nil {
	log.Fatalln(err)
}
log.Println(greeting)

// prints: Hello MAT

Rules

  • Every script must provide a goscript entry function
  • Imports must be included above the goscript function if required
  • Any special types being used as input or output require gob.Register in the script and the calling code
  • The goscript function must return two values and the second type must be error
  • Only execute trusted code; there are no limits to what scripts can do

Security

Running any Go code unsupervised represents a pretty giant security concern for obvious reasons, but that doesn't nncessarily spell the end for using Goscript in your projects.

One option is to wrap Goscript and provide the goscript func signature youself, and allow users to only provide the body. This would also prevent them from controlling the imports too. And with some simple string checking, you'd be able to protect from injection attacks.

For an example of how this might work, see the example/rename tool.

How it works

  • Goscript generates a mini Go program and executes it with go run
  • The script program communicates with the host program via stdin/stdout
  • Values are encoded/decoded via the encoding/gob package
  • The script program stays running until Close is called

Logo by Rob Baines, inspired by Renee French, licensed under a Creative Commons Attribution 4.0 International license.

goscript's People

Contributors

matryer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

goscript's Issues

I have some problems at running exe with goscript

Hey!
Maybe my questions will be stupid but why Im getting errors like
gob: bad data: undefined type CommonType = struct { Name string; Id int; }
when there is shoud be no errors after running goscript program? When Im trying to run bad code there is correct error answers like wrong package import or variavle unused.
Also Im getting response from goscript.Execute before goscript.exe is finished to work. Like Im running TCP connection via net.Conn and there is active TCP port and goscript.exe running, when my main.exe is already finished and giving me this error

Sorry for my English

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.