string_parse_erb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +53 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/str_parse_erb/str_parse_erb.rb +16 -0
- data/lib/string_parse_erb.rb +7 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/string_parse_erb_spec.rb +55 -0
- data/string_parse_erb.gemspec +63 -0
- metadata +142 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm_gemset_create_on_use_flag=1; rvm gemset use string-parse-erb-dev
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.9.2)
|
11
|
+
rcov (0.9.10)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.6.4)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Nadav Blum
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= string_parse_erb
|
2
|
+
|
3
|
+
Say you design a CMS, and you want the ability to replace some place-holders inside an item-content with some external values.
|
4
|
+
|
5
|
+
Of course, you can use regular expressions and friends to do the task.
|
6
|
+
|
7
|
+
But why not use the power of ERB engine instead ?
|
8
|
+
|
9
|
+
Implementing ERB fragments inside your content can open up wide array of possibilities,
|
10
|
+
all of the sudden you get the power of ruby inside you content: not just variables-replacement but conditionals, loops and so on..
|
11
|
+
|
12
|
+
The downside of this is security...
|
13
|
+
|
14
|
+
* You wouldn't want to process an item-content with something like:
|
15
|
+
|
16
|
+
<tt>"The time now is <%= time %>. Say bye bye... <% system('shutdown -r now') %>"</tt>
|
17
|
+
|
18
|
+
While 'time' may be a legal variable to be used inside the item-content template, calling #system
|
19
|
+
should definitely be forbidden.
|
20
|
+
|
21
|
+
* You would like to limit the 'binding' object passed to ERB, so it includes only the variables you wish to expose, and not any variable happen to be in the program's context.
|
22
|
+
|
23
|
+
The solution taken by this gem is very simple, (and so may not be perfect), and discussed in a couple of posts like: http://stackoverflow.com/questions/3619516/how-do-you-mark-a-ruby-binding-as-trusted
|
24
|
+
|
25
|
+
== Installation
|
26
|
+
* gem install string_parse_erb
|
27
|
+
|
28
|
+
* include of extend StrParseErb module
|
29
|
+
|
30
|
+
* string_parse_erb(template, var_hash, safe_level=4)
|
31
|
+
Where:
|
32
|
+
* template - a string containing erb fragments
|
33
|
+
* var_hash - A hash of variables and their values to be exposed for template usage
|
34
|
+
* safe_level(defaults to 4) - ruby SAFE level, under which ERB will operate
|
35
|
+
|
36
|
+
== Examples
|
37
|
+
string_parse_erb(
|
38
|
+
"Good <%= part_of_day %>, the time is <%= time %>.",
|
39
|
+
{:part_of_day => "morning", :time => "six o'clock"}
|
40
|
+
)
|
41
|
+
# => "Good morning, the time is six o'clock."
|
42
|
+
|
43
|
+
string_parse_erb(
|
44
|
+
"<%= cmd %>",
|
45
|
+
{:cmd=> "abort"}
|
46
|
+
)
|
47
|
+
# => Raises SecurityError Exception
|
48
|
+
|
49
|
+
== Copyright
|
50
|
+
|
51
|
+
Copyright (c) 2011 Nadav Blum. See LICENSE.txt for
|
52
|
+
further details.
|
53
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "string_parse_erb"
|
18
|
+
gem.homepage = "http://github.com/bnadav/string_parse_erb"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Process ERB fragments in a String, according to variables hash}
|
21
|
+
gem.description = %Q{Process ERB fragments in a String, according to variables hash, no extenal vars are visible to ERB, safe level and untainting of string can be set}
|
22
|
+
gem.email = "nadav.blum@gmail.com"
|
23
|
+
gem.authors = ["Nadav Blum"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "string_parse_erb #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module StrParseErb
|
2
|
+
|
3
|
+
VERSION = "0.1.0"
|
4
|
+
|
5
|
+
def string_parse_erb(template, var_hash, safe_level=4)
|
6
|
+
data = VarHash.new(var_hash)
|
7
|
+
ERB.new(template, safe_level).result(data.get_binding.taint)
|
8
|
+
end
|
9
|
+
|
10
|
+
class VarHash < OpenStruct
|
11
|
+
def get_binding
|
12
|
+
binding
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'string_parse_erb'
|
5
|
+
include StrParseErb
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "StringParseErb" do
|
4
|
+
it "does not change a string with no erb fragments" do
|
5
|
+
str = "Hello World!"
|
6
|
+
string_parse_erb(str, {}).should == "Hello World!"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "deletes from string, erb fragments which have no match in the variables hash" do
|
10
|
+
str = "Who are <%= person %>"
|
11
|
+
string_parse_erb(str, {:man => "you"}).should == "Who are "
|
12
|
+
end
|
13
|
+
|
14
|
+
it "replaces erb fragments with values from vars_hash" do
|
15
|
+
str = "Good <%= part_of_day %>, the time is <%= time %>."
|
16
|
+
vars_hash = {:part_of_day => "morning", :time => "six o'clock"}
|
17
|
+
string_parse_erb(str, vars_hash).should == "Good morning, the time is six o'clock."
|
18
|
+
end
|
19
|
+
|
20
|
+
it "replaces erb fragments with values from vars_hash, when string is tainted" do
|
21
|
+
str = "Good <%= part_of_day %>, the time is <%= time %>."
|
22
|
+
vars_hash = {:part_of_day => "morning", :time => "six o'clock"}
|
23
|
+
string_parse_erb(str.taint, vars_hash).should == "Good morning, the time is six o'clock."
|
24
|
+
end
|
25
|
+
|
26
|
+
it "blocks insecure operations by default" do
|
27
|
+
str = "<%= eval(\"system('ls')\") %>"
|
28
|
+
lambda { string_parse_erb(str, {}) }.should raise_error(SecurityError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "blocks another insecure operations by default" do
|
32
|
+
lambda { string_parse_erb("<%= abort %>", {}) }.should raise_error(SecurityError)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "blocks insecure operations by default, when string is tainted" do
|
36
|
+
str = "<%= eval(\"system('ls')\") %>"
|
37
|
+
lambda { string_parse_erb(str.taint, {}) }.should raise_error(SecurityError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "allows changing SAFE level" do
|
41
|
+
str = " 1 + 1 = <%= eval(\"1 + 1 \") %>"
|
42
|
+
string_parse_erb(str, {}, 0).should == " 1 + 1 = 2"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "does not bind external variables" do
|
46
|
+
str = "Good <%= part_of_day %>"
|
47
|
+
@part_of_day = part_of_day = @@part_of_day = "morning"
|
48
|
+
string_parse_erb(str, {}).should == "Good "
|
49
|
+
end
|
50
|
+
|
51
|
+
it "processes complex fragments" do
|
52
|
+
str = "Hello<% if planet %> planet <% else %> earth <% end %>"
|
53
|
+
string_parse_erb(str, {:planet => false}).should == "Hello earth "
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{string_parse_erb}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nadav Blum"]
|
12
|
+
s.date = %q{2011-09-02}
|
13
|
+
s.description = %q{Process ERB fragments in a String, according to variables hash, no extenal vars are visible to ERB, safe level and untainting of string can be set}
|
14
|
+
s.email = %q{nadav.blum@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
".rvmrc",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/str_parse_erb/str_parse_erb.rb",
|
30
|
+
"lib/string_parse_erb.rb",
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/string_parse_erb_spec.rb",
|
33
|
+
"string_parse_erb.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/bnadav/string_parse_erb}
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Process ERB fragments in a String, according to variables hash}
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_parse_erb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Nadav Blum
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-02 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
name: rspec
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 2.3.0
|
35
|
+
requirement: *id001
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
name: bundler
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 1.0.0
|
51
|
+
requirement: *id002
|
52
|
+
type: :development
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
name: jeweler
|
56
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 7
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 6
|
65
|
+
- 4
|
66
|
+
version: 1.6.4
|
67
|
+
requirement: *id003
|
68
|
+
type: :development
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
prerelease: false
|
71
|
+
name: rcov
|
72
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirement: *id004
|
82
|
+
type: :development
|
83
|
+
description: Process ERB fragments in a String, according to variables hash, no extenal vars are visible to ERB, safe level and untainting of string can be set
|
84
|
+
email: nadav.blum@gmail.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.rdoc
|
92
|
+
files:
|
93
|
+
- .document
|
94
|
+
- .rspec
|
95
|
+
- .rvmrc
|
96
|
+
- Gemfile
|
97
|
+
- Gemfile.lock
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.rdoc
|
100
|
+
- Rakefile
|
101
|
+
- VERSION
|
102
|
+
- lib/str_parse_erb/str_parse_erb.rb
|
103
|
+
- lib/string_parse_erb.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- spec/string_parse_erb_spec.rb
|
106
|
+
- string_parse_erb.gemspec
|
107
|
+
has_rdoc: true
|
108
|
+
homepage: http://github.com/bnadav/string_parse_erb
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
122
|
+
segments:
|
123
|
+
- 0
|
124
|
+
version: "0"
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
hash: 3
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
requirements: []
|
135
|
+
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.3.7
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Process ERB fragments in a String, according to variables hash
|
141
|
+
test_files: []
|
142
|
+
|