Permalink
Browse files

Implemented and tested safe mode.

* Closes #130.
  • Loading branch information...
1 parent cfd61b8 commit 64186a85baf5db22e111b0594585b78802672047 @h3rald committed Jun 8, 2010
Showing with 28 additions and 3 deletions.
  1. +8 −3 lib/glyph.rb
  2. +4 −0 lib/glyph/macro_validators.rb
  3. +5 −0 macros/core.rb
  4. +11 −0 spec/lib/glyph_spec.rb
View
@@ -50,8 +50,13 @@ class MutualInclusionError < MacroError; end
begin
unless const_defined? :MODE then
- # Glyph's modes: debug/lite/test
- MODE = {:debug => false, :lite => false, :test => false, :library => false}
+ MODE = {
+ :debug => false,
+ :lite => false,
+ :test => false,
+ :library => false,
+ :safe => false
+ }
end
rescue
end
@@ -60,7 +65,7 @@ class MutualInclusionError < MacroError; end
@@document = nil
(class << self; self; end).instance_eval do
- ["test", "lite", "debug", "library"].each do |mode|
+ ["test", "lite", "debug", "library", "safe"].each do |mode|
define_method((mode+"?").to_sym) do
MODE[mode.to_sym]
end
@@ -99,6 +99,10 @@ def no_parameters(options={:level=>:error})
end
end
+ def safety_check
+ macro_error "Macro '#@name' cannot be used in safe mode" if Glyph.safe?
+ end
+
# Ensure that no mutual inclusion occurs within the specified parameter or attribute
# @param [Fixnum, Symbol] the parameter index or attribute name to check
# @raise [Glyph::MacroError] mutual inclusion was detected
View
@@ -33,6 +33,7 @@
end
macro "macro:" do
+ safety_check
exact_parameters 2
ident = param(0)
code = param(1)
@@ -43,6 +44,7 @@
end
macro :include do
+ safety_check
exact_parameters 1
no_mutual_inclusion_in 0
v = value
@@ -82,6 +84,7 @@
end
macro :ruby do
+ safety_check
max_parameters 1
res = Glyph.instance_eval(value.gsub(/\\*([\[\]\|])/){$1})
res.is_a?(Proc) ? "" : res
@@ -92,6 +95,7 @@
end
macro "config:" do
+ safety_check
max_parameters 2
setting = param(0)
v = param(1) rescue nil
@@ -160,6 +164,7 @@
end
macro "rewrite:" do
+ safety_check
exact_parameters 2
macro_name = param(0).to_sym
raw_param(1).descend do |n, level|
@@ -62,4 +62,15 @@
Glyph['test_setting'].should == nil
end
+ it "should not allow certain macros to be expanded in safe mode" do
+ create_project
+ Glyph.run! "load:all"
+ Glyph.safe_mode = true
+ lambda { output_for("include[test.glyph]")}.should raise_error Glyph::MacroError
+ lambda {output_for("config:[test|true]")}.should raise_error Glyph::MacroError
+ lambda { output_for("ruby[Time.now]")}.should raise_error Glyph::MacroError
+ lambda { output_for("rw:[a|section[{{0}}]]")}.should raise_error Glyph::MacroError
+ Glyph.safe_mode = false
+ end
+
end

0 comments on commit 64186a8

Please sign in to comment.