unpoly-rails 3.3.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,7 +48,7 @@ module Unpoly
48
48
  end
49
49
 
50
50
  ##
51
- # An array of strings, serialized as JSON.
51
+ # An array of strings, separated by a space character.
52
52
  class SeparatedValues < Field
53
53
 
54
54
  def initialize(name, separator: ' ', default: nil)
@@ -60,8 +60,8 @@ module Unpoly
60
60
  def parse(raw)
61
61
  if raw
62
62
  raw.split(@separator)
63
- elsif @default
64
- instance_exec(&@default)
63
+ else
64
+ @default&.call
65
65
  end
66
66
  end
67
67
 
@@ -101,11 +101,7 @@ module Unpoly
101
101
  end
102
102
 
103
103
  def parse(raw)
104
- if raw.present?
105
- result = Util.json_decode(raw)
106
- elsif @default
107
- result = instance_exec(&@default)
108
- end
104
+ result = Util.guard_json_decode(raw, &@default)
109
105
 
110
106
  if result.is_a?(::Hash)
111
107
  result = ActiveSupport::HashWithIndifferentAccess.new(result)
@@ -132,13 +128,7 @@ module Unpoly
132
128
  end
133
129
 
134
130
  def parse(raw)
135
- if raw.present?
136
- result = Util.json_decode(raw)
137
- elsif @default
138
- result = instance_exec(&@default)
139
- end
140
-
141
- result
131
+ Util.guard_json_decode(raw, &@default)
142
132
  end
143
133
 
144
134
  def stringify(value)
@@ -3,8 +3,20 @@ module Unpoly
3
3
  class Util
4
4
  class << self
5
5
 
6
- def json_decode(string)
7
- ActiveSupport::JSON.decode(string)
6
+ def guard_json_decode(raw, &default)
7
+ if raw.present?
8
+ begin
9
+ ActiveSupport::JSON.decode(raw)
10
+ rescue ActiveSupport::JSON.parse_error
11
+ # We would love to crash here, as it might indicate a bug in the frontend code.
12
+ # Unfortunately security scanners may be spamming malformed JSON in X-Up headers,
13
+ # DOSing us with error notifications.
14
+ ::Rails.logger.error('unpoly-rails: Ignoring malformed JSON in X-Up header')
15
+ default&.call
16
+ end
17
+ else
18
+ default&.call
19
+ end
8
20
  end
9
21
 
10
22
  # We build a lot of JSON that goes into HTTP header.
@@ -2,8 +2,7 @@ module Unpoly
2
2
  module Rails
3
3
  ##
4
4
  # The current version of the unpoly-rails gem.
5
- # This version number is also used for releases of the Unpoly
6
- # frontend code.
7
- VERSION = '3.3.0'
5
+ # The first 3 digits should match the version of the Unpoly frontend code.
6
+ VERSION = '3.5.0'
8
7
  end
9
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unpoly-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-16 00:00:00.000000000 Z
11
+ date: 2023-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties