Skip to content

Instantly share code, notes, and snippets.

@jlturner
Created February 22, 2015 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlturner/2011db6f30bfd1b7e323 to your computer and use it in GitHub Desktop.
Save jlturner/2011db6f30bfd1b7e323 to your computer and use it in GitHub Desktop.
Quine Eval Server - An experiment
# Quine Eval Server - An experiment
# =================================
#
# This server, when run, will accept POST requests on the root ('/') where the post body is ruby code to eval.
# This server also makes a copy of its source code, and includes the eval'd statements in the new source.
# Because this is a sinatra server, you can use this to create new routes and build up a web service incrementally while running.
#
# Example post to server using curl:
# curl -vv -XPOST -d 'get "/hi" do ; "hi!" ; end' 'http://localhost:4567'
#
# * This is an experiment. This is probably irresponsible to use in production, but is interesting nonetheless :)
require 'sinatra/base'
# This will be the output server source code which is created from this server and code passed to the server by posting to '/'
$_output = File.new "new_server.rb", "w"
$_server_file = File.readlines(File.expand_path(__FILE__, Dir.pwd))
$_output_line_start = __LINE__ + 1 ; $_output.print $_server_file[0...$_output_line_start].join
class DynamicEvalServer < Sinatra::Base
def self._binding
binding
end
# Posting to the root will let users pass executable code to this server. This code will be saved along with the server in the output.
post '/' do
a = request.body.read
begin
eval a, self.class._binding
$_output.puts a
halt 200
rescue
halt 400
end
end
run!
end
$_output.print $_server_file[$_output_line_start...$_server_file.length].join
$_output.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment