gettext_i18n_rails 0.2.14 → 0.2.15

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -134,11 +134,16 @@ namespace-aware translation
134
134
 
135
135
  XSS / html_safe
136
136
  ===============
137
- If you trust your translators and all your usages of % on translations:
138
- (% on string is atm buggy with always staying html_safe, no matter what was replaced)
137
+ If you trust your translators and all your usages of % on translations:<br/>
139
138
  # config/environment.rb
140
139
  GettextI18nRails.translations_are_html_safe = true
141
140
 
141
+ String % vs html_safe is buggy (can be used for XSS on 1.8 and is always non-safe in 1.9)<br/>
142
+ My recommended fix is: `require 'gettext_i18n_rails/string_interpolate_fix'`
143
+
144
+ - safe stays safe (escape added strings)
145
+ - unsafe stays unsafe (do not escape added strings)
146
+
142
147
  ActiveRecord - error messages
143
148
  =============================
144
149
  ActiveRecord error messages are translated through Rails::I18n, but
@@ -194,10 +199,6 @@ lib/tasks/gettext.rake:
194
199
  end
195
200
  end
196
201
 
197
- TODO
198
- =====
199
- - fix % on string to respect html_safe: `("<a>%{x}</a>".html_safe % {:x=>'<script>y</script>'})` should escape the `<script>y</script>` part)
200
-
201
202
  Contributors
202
203
  ======
203
204
  - [ruby gettext extractor](http://github.com/retoo/ruby_gettext_extractor/tree/master) from [retoo](http://github.com/retoo)
@@ -208,6 +209,6 @@ Contributors
208
209
  - [Anh Hai Trinh](http://blog.onideas.ws)
209
210
  - [ed0h](http://github.com/ed0h)
210
211
 
211
- [Michael Grosser](http://grosser.it)
212
- grosser.michael@gmail.com
212
+ [Michael Grosser](http://grosser.it)<br/>
213
+ grosser.michael@gmail.com<br/>
213
214
  Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.14
1
+ 0.2.15
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gettext_i18n_rails}
8
- s.version = "0.2.14"
8
+ s.version = "0.2.15"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-01-28}
12
+ s.date = %q{2011-02-10}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.files = [
15
15
  "Rakefile",
@@ -27,11 +27,13 @@ Gem::Specification.new do |s|
27
27
  "lib/gettext_i18n_rails/model_attributes_finder.rb",
28
28
  "lib/gettext_i18n_rails/railtie.rb",
29
29
  "lib/gettext_i18n_rails/ruby_gettext_extractor.rb",
30
+ "lib/gettext_i18n_rails/string_interpolate_fix.rb",
30
31
  "lib/gettext_i18n_rails/tasks.rb",
31
32
  "lib/tasks/gettext_rails_i18n.rake",
32
33
  "spec/gettext_i18n_rails/action_controller_spec.rb",
33
34
  "spec/gettext_i18n_rails/active_record_spec.rb",
34
35
  "spec/gettext_i18n_rails/backend_spec.rb",
36
+ "spec/gettext_i18n_rails/string_interpolate_fix_spec.rb",
35
37
  "spec/gettext_i18n_rails_spec.rb",
36
38
  "spec/spec_helper.rb"
37
39
  ]
@@ -43,6 +45,7 @@ Gem::Specification.new do |s|
43
45
  "spec/gettext_i18n_rails/action_controller_spec.rb",
44
46
  "spec/gettext_i18n_rails/active_record_spec.rb",
45
47
  "spec/gettext_i18n_rails/backend_spec.rb",
48
+ "spec/gettext_i18n_rails/string_interpolate_fix_spec.rb",
46
49
  "spec/gettext_i18n_rails_spec.rb",
47
50
  "spec/spec_helper.rb"
48
51
  ]
@@ -0,0 +1,20 @@
1
+ needed = "".respond_to?(:html_safe) and
2
+ (
3
+ "".html_safe % {:x => '<br/>'} == '<br/>' or
4
+ not ("".html_safe % {:x=>'a'}).html_safe?
5
+ )
6
+
7
+ if needed
8
+ class String
9
+ alias :interpolate_without_html_safe :%
10
+
11
+ def %(*args)
12
+ if args.first.is_a?(Hash) and html_safe?
13
+ safe_replacement = Hash[args.first.map{|k,v| [k,ERB::Util.h(v)] }]
14
+ interpolate_without_html_safe(safe_replacement).html_safe
15
+ else
16
+ interpolate_without_html_safe(*args).dup # make sure its not html_safe
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path("../spec_helper", File.dirname(__FILE__))
2
+ require 'gettext_i18n_rails/string_interpolate_fix'
3
+
4
+ describe "String#%" do
5
+ it "is not safe if it was not safe" do
6
+ result = ("<br/>%{x}" % {:x => 'a'})
7
+ result.should == '<br/>a'
8
+ result.html_safe?.should == false
9
+ end
10
+
11
+ it "stays safe if it was safe" do
12
+ result = ("<br/>%{x}".html_safe % {:x => 'a'})
13
+ result.should == '<br/>a'
14
+ result.html_safe?.should == true
15
+ end
16
+
17
+ it "escapes unsafe added to safe" do
18
+ result = ("<br/>%{x}".html_safe % {:x => '<br/>'})
19
+ result.should == '<br/>&lt;br/&gt;'
20
+ result.html_safe?.should == true
21
+ end
22
+
23
+ it "does not escape unsafe if it was unsafe" do
24
+ result = ("<br/>%{x}" % {:x => '<br/>'})
25
+ result.should == '<br/><br/>'
26
+ result.html_safe?.should == false
27
+ end
28
+
29
+ it "does not break array replacement" do
30
+ "%ssd" % ['a'].should == "asd"
31
+ end
32
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext_i18n_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 14
10
- version: 0.2.14
9
+ - 15
10
+ version: 0.2.15
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-28 00:00:00 +01:00
18
+ date: 2011-02-10 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,11 +56,13 @@ files:
56
56
  - lib/gettext_i18n_rails/model_attributes_finder.rb
57
57
  - lib/gettext_i18n_rails/railtie.rb
58
58
  - lib/gettext_i18n_rails/ruby_gettext_extractor.rb
59
+ - lib/gettext_i18n_rails/string_interpolate_fix.rb
59
60
  - lib/gettext_i18n_rails/tasks.rb
60
61
  - lib/tasks/gettext_rails_i18n.rake
61
62
  - spec/gettext_i18n_rails/action_controller_spec.rb
62
63
  - spec/gettext_i18n_rails/active_record_spec.rb
63
64
  - spec/gettext_i18n_rails/backend_spec.rb
65
+ - spec/gettext_i18n_rails/string_interpolate_fix_spec.rb
64
66
  - spec/gettext_i18n_rails_spec.rb
65
67
  - spec/spec_helper.rb
66
68
  has_rdoc: true
@@ -101,5 +103,6 @@ test_files:
101
103
  - spec/gettext_i18n_rails/action_controller_spec.rb
102
104
  - spec/gettext_i18n_rails/active_record_spec.rb
103
105
  - spec/gettext_i18n_rails/backend_spec.rb
106
+ - spec/gettext_i18n_rails/string_interpolate_fix_spec.rb
104
107
  - spec/gettext_i18n_rails_spec.rb
105
108
  - spec/spec_helper.rb