Skip to content

Commit

Permalink
Remove trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Aug 31, 2022
1 parent d560010 commit 178a4a6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 82 deletions.
2 changes: 1 addition & 1 deletion lib/action_view/template_handlers/safe_haml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module TemplateHandlers
class SafeHaml < TemplateHandler
include Compilable rescue nil # does not exist prior Rails 2.1
extend SafemodeHandler

def self.line_offset
3
end
Expand Down
10 changes: 5 additions & 5 deletions lib/action_view/template_handlers/safemode_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ module SafemodeHandler
def valid_assigns(assigns)
assigns = assigns.reject{|key, value| skip_assigns.include?(key) }
end

def delegate_methods(view)
[ :render, :params, :flash ] +
helper_methods(view) +
[ :render, :params, :flash ] +
helper_methods(view) +
ActionController::Routing::Routes.named_routes.helpers
end

def helper_methods(view)
view.class.included_modules.collect {|m| m.instance_methods(false) }.flatten.map(&:to_sym)
end

def skip_assigns
[ "_cookies", "_flash", "_headers", "_params", "_request",
"_response", "_session", "before_filter_chain_aborted",
"ignore_missing_templates", "logger", "request_origin",
"template", "template_class", "url", "variables_added",
"view_paths" ]
"view_paths" ]
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions lib/haml/safemode.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
require 'haml'

module Haml
module Haml
class Buffer
class Jail < Safemode::Jail
allow :push_script, :push_text, :_hamlout, :open_tag
end
end
end

module Haml
module Haml
class Engine
def precompile_for_safemode(filename, ignore_assigns = [], delegate_methods = [])
def precompile_for_safemode(filename, ignore_assigns = [], delegate_methods = [])
@precompiled.gsub!('\\','\\\\\\') # backslashes would disappear in compile_template/modul_eval, so we escape them
<<-CODE

<<-CODE
buffer = Haml::Buffer.new(#{options_for_buffer.inspect})
local_assigns = local_assigns.merge :_hamlout => buffer
handler = ActionView::TemplateHandlers::SafeHaml
assigns = handler.valid_assigns(@template.assigns)
methods = handler.delegate_methods(self)
code = %Q(#{code});
box = Safemode::Box.new(self, methods, #{filename.inspect}, 0)
box.eval(code, assigns, local_assigns, &lambda{ yield })
buffer.buffer
box.eval(code, assigns, local_assigns, &lambda{ yield })
buffer.buffer
CODE

# preamble = "buffer = Haml::Buffer.new(#{options_for_buffer.inspect})
# local_assigns = local_assigns.merge :_hamlout => buffer
# assigns = @template.assigns.reject{|key, value| #{ignore_assigns.inspect}.include?(key) };".gsub("\n", ';')
#
#
# postamble = "box = Safemode::Box.new(self, #{delegate_methods.inspect})
# box.eval(code, assigns, local_assigns, &lambda{ yield })
# buffer.buffer".gsub("\n", ';')
#
#
# preamble + "code = %Q(#{@precompiled});" + postamble
end
end
Expand Down
40 changes: 20 additions & 20 deletions lib/ruby_parser_string_io_patch.diff
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
+++ lib/ruby_lexer.rb 2008-04-27 01:07:03.000000000 +0200
@@ -45,7 +45,7 @@
raise "bad val: #{str.inspect}" unless String === str

self.file = file
- self.lexer.src = StringIO.new(str)
+ self.lexer.src = RubyParser::StringIO.new(str)

@yydebug = ENV.has_key? 'DEBUG'

@@ -2604,104 +2604,106 @@
end
end

-class StringIO # HACK: everything in here is a hack
- attr_accessor :begin_of_line, :was_begin_of_line
- alias :begin_of_line? :begin_of_line
Expand All @@ -22,10 +22,10 @@
+ attr_accessor :begin_of_line, :was_begin_of_line
+ alias :begin_of_line? :begin_of_line
+ alias :read_all :read

- alias :old_initialize :initialize
+ alias :old_initialize :initialize

- def initialize(*args)
- self.begin_of_line = true
- self.was_begin_of_line = false
Expand All @@ -38,26 +38,26 @@
+ old_initialize(*args)
+ @original_string = self.string.dup
+ end

- def rest
- self.string[self.pos..-1]
- end
+ def rest
+ self.string[self.pos..-1]
+ end

- def current_line # HAHA fuck you
- @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size
- end
+ def current_line # HAHA fuck you
+ @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size
+ end

- def read
- c = self.getc
+ def read
+ c = self.getc

- if c == ?\r then
- d = self.getc
- self.ungetc d if d and d != ?\n
Expand All @@ -68,7 +68,7 @@
+ self.ungetc d if d and d != ?\n
+ c = ?\n
+ end

- self.was_begin_of_line = self.begin_of_line
- self.begin_of_line = c == ?\n
- if c and c != 0 then
Expand All @@ -84,12 +84,12 @@
+ end
end
- end

- def match_string term, indent=false # TODO: add case insensitivity, or just remove
- buffer = []
+ def match_string term, indent=false # TODO: add case insensitivity, or just remove
+ buffer = []

- if indent
- while c = self.read do
- if c !~ /\s/ or c == "\n" or c == "\r" then
Expand All @@ -106,7 +106,7 @@
- buffer << c
end
- end

- term.each_byte do |c2|
- c = self.read
- c = self.read if c and c == "\r"
Expand All @@ -126,15 +126,15 @@
+
+ return true
end

- return true
- end
+ def read_line
+ self.begin_of_line = true
+ self.was_begin_of_line = false
+ gets.sub(/\r\n?$/, "\n") # HACK
+ end

- def read_line
- self.begin_of_line = true
- self.was_begin_of_line = false
Expand Down Expand Up @@ -162,26 +162,26 @@
+ end
end
- end

- def unread(c)
- return if c.nil? # UGH
+ def unread(c)
+ return if c.nil? # UGH

- # HACK: only depth is 2... who cares? really I want to remove all of this
- self.begin_of_line = self.was_begin_of_line || true
- self.was_begin_of_line = nil
+ # HACK: only depth is 2... who cares? really I want to remove all of this
+ self.begin_of_line = self.was_begin_of_line || true
+ self.was_begin_of_line = nil

- c = c[0] if String === c
- self.ungetc c
- end
+ c = c[0] if String === c
+ self.ungetc c
+ end

- def unread_many str
- str.split(//).reverse.each do |c|
- unread c
Expand Down
12 changes: 6 additions & 6 deletions lib/safemode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class << self
def jail(obj)
find_jail_class(obj.is_a?(Class) ? obj : obj.class).new obj
end

def find_jail_class(klass)
while klass != Object
return klass.const_get('Jail') if klass.const_defined?('Jail')
Expand All @@ -35,24 +35,24 @@ def find_jail_class(klass)
Jail
end
end

define_core_jail_classes

class Box
def initialize(delegate = nil, delegate_methods = [], filename = nil, line = nil)
@scope = Scope.new(delegate, delegate_methods)
@filename = filename
@line = line
end
end

def eval(code, assigns = {}, locals = {}, &block)
code = Parser.jail(code)
binding = @scope.bind(assigns, locals, &block)
result = Kernel.eval(code, binding, @filename || __FILE__, @line || __LINE__)
end

def output
@scope.output
end
end
end
end
8 changes: 4 additions & 4 deletions lib/safemode/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Kernel
module Kernel
def silently(&blk)
old_verbose, $VERBOSE = $VERBOSE, nil
yield
$VERBOSE = old_verbose
end
end
end

class Module
class Module
def undef_methods(*methods)
methods.each { |name| undef_method(name) }
end
Expand All @@ -29,7 +29,7 @@ def to_jail
# Safemode.jail collect{ |obj| obj.to_jail }
# end
# end
#
#
# class Hash
# def to_jail
# hash = {}
Expand Down
6 changes: 3 additions & 3 deletions lib/safemode/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module Safemode
class Error < RuntimeError; end

class SecurityError < Error
@@types = { :const => 'constant',
:xstr => 'shell command',
:fcall => 'method',
:vcall => 'method',
:gvar => 'global variable' }

def initialize(type, value = nil)
type = @@types[type] if @@types.include?(type)
super "Safemode doesn't allow to access '#{type}'" + (value ? " on #{value}" : '')
end
end

class NoMethodError < Error
def initialize(method, jail, source = nil)
super "undefined method '#{method}' for #{jail}" + (source ? " (#{source})" : '')
Expand Down
22 changes: 11 additions & 11 deletions lib/safemode/scope.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
module Safemode
class Scope < Blankslate
def initialize(delegate = nil, delegate_methods = [])
@delegate = delegate
@delegate = delegate
@delegate_methods = delegate_methods
@locals = {}
end

def bind(instance_vars = {}, locals = {}, &block)
@locals = symbolize_keys(locals) # why can't I just pull them to local scope in the same way like instance_vars?
instance_vars = symbolize_keys(instance_vars)
instance_vars.each {|key, obj| eval "@#{key} = instance_vars[:#{key}]" }
@_safemode_output = ''
binding
end

def to_jail
self
end

def puts(*args)
print args.to_s + "\n"
end

def print(*args)
def print(*args)
@_safemode_output += args.to_s
end

def output
@_safemode_output
end
Expand All @@ -39,18 +39,18 @@ def method_missing(method, *args, &block)
raise Safemode::SecurityError.new(method, "#<Safemode::ScopeObject>")
end
end

private

def symbolize_keys(hash)
hash.inject({}) do |hash, (key, value)|
hash.inject({}) do |hash, (key, value)|
hash[key.to_s.intern] = value
hash
end
end

def unjail_args(args)
args.collect do |arg|
args.collect do |arg|
arg.class.name =~ /::Jail$/ ? arg.instance_variable_get(:@source) : arg
end
end
Expand Down
Loading

0 comments on commit 178a4a6

Please sign in to comment.