referer_tracking 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4a943652b97e3ec7de397bb63b9790b3d0a891a5681a419912622930f663cd7
4
- data.tar.gz: dcb1f135ffe680a0eadbda0b9f8c8190c545671afd3faff670d7699433f41f9b
3
+ metadata.gz: b1d31bdfc71fe8f3693ee62bb07b24175dbd1d3ff73df06241d7081ba689ed23
4
+ data.tar.gz: 6c49d4106a856d379501d0e7257357effbd4580c7b79a592e30e4b2fe0d25660
5
5
  SHA512:
6
- metadata.gz: caa1aedca0dd0bc04c2ea890d2761f69b77f0b84f522e88ebd65737e034ff8cfa4367253aa0008e877413173ea71415ac4541e6c20d7fb30eaf8e8e0d9fe39b4
7
- data.tar.gz: e706b830603bc58520a4a8e60301ff5419ad970485adecbeee51496372c39dcdb5020b2de19b0dbd940c3b2770a098e96f195548bbfd727122b794744603711a
6
+ metadata.gz: 29d8ba7c6144d187b7f4c2c1601c89a95d41e1dd577d308391100774870dc519df4e839f3352348f44daa1bd8d1657ef81ede748a31486d1c92ad756270e7fae
7
+ data.tar.gz: fc5b9e4b06b3c54eac91c0579e354c6d7b5011e84c595ff785e695fda7b1cbc02bdf8d0ad39fc85c5edf35fe513f57468a470e2c158eae20071807ab4a2f0dbc
data/README.md CHANGED
@@ -6,8 +6,6 @@ It does it by saving referrer url to session and saving information about the re
6
6
 
7
7
  Also includes tools to add log lines to models to get better information about the flow of the users or which A/B testing variation was used for which model. Aim is to make data collection for model creation, A/B-testing and growth hacking easier.
8
8
 
9
- [<img src="https://secure.travis-ci.org/holli/referer_tracking.png" />](http://travis-ci.org/holli/referer_tracking)
10
-
11
9
  ## Example use cases
12
10
 
13
11
  ```
@@ -6,19 +6,17 @@ module RefererTracking::ControllerAddons
6
6
  unless request_is_from_a_known_bot?
7
7
  if session[:referer_tracking].nil?
8
8
  @referer_tracking_first_request = true
9
- session[:referer_tracking] = hash = Hash.new
10
9
 
11
- request_ref = "unknown"
12
- request_ref = request.headers["HTTP_REFERER"] if !request.headers["HTTP_REFERER"].blank?
10
+ request_ref = referer_tracking_utf8(request.headers["HTTP_REFERER"])
11
+ request_ref = "unknown" if request_ref.blank?
13
12
 
14
- request_ref = request_ref.to_s.gsub(/pass(word)?=[^&]+/, 'pass=xxxx')
15
- first_url = request.url.to_s.gsub(/pass(word)?=[^&]+/, 'pass=xxxx')
13
+ request_ref = request_ref.gsub(/pass(word)?=[^&]+/, 'pass=xxxx')
14
+ first_url = referer_tracking_utf8(request.url).gsub(/pass(word)?=[^&]+/, 'pass=xxxx')
16
15
 
17
16
  # String keys throughout the session hash. Rails 7.0 made :json the default
18
17
  # cookies_serializer, and JSON has no symbols — a symbol key written here comes
19
18
  # back as a string on the next request, so any symbol lookup silently returns nil.
20
- hash["session_referer_url"] = request_ref
21
- hash["session_first_url"] = first_url
19
+ hash = {"session_referer_url" => request_ref, "session_first_url" => first_url}
22
20
 
23
21
  if RefererTracking.set_referer_cookies && cookies[RefererTracking.set_referer_cookies_name].nil?
24
22
  cookie_info = "v01|||#{Time.now.utc.to_i}|||#{first_url.first(RefererTracking.set_referer_cookies_first_url_max_length)}|||#{request_ref.first(RefererTracking.set_referer_cookies_ref_url_max_length)}"
@@ -26,9 +24,17 @@ module RefererTracking::ControllerAddons
26
24
  end
27
25
 
28
26
  logger.info( "REFERER_TRACKING_FIRST: ver04 (ref|first) ||| #{hash["session_referer_url"]} ||| #{hash["session_first_url"]}" )
27
+
28
+ # Assigned once the hash is complete. A half-built one reaching the session cookie
29
+ # would leave session[:referer_tracking] non-nil, and this block would never run
30
+ # again for that visitor.
31
+ session[:referer_tracking] = hash
29
32
  end
30
33
 
31
34
  end
35
+
36
+ rescue => e
37
+ Rails.logger.info "RefererTracking::ControllerAddons.save_to_session problem with the first request: #{e}"
32
38
  end
33
39
 
34
40
  def referer_tracking_after_create(record)
@@ -58,13 +64,17 @@ module RefererTracking::ControllerAddons
58
64
  end
59
65
 
60
66
  ref_mod[:ip] = request.ip
61
- ref_mod[:user_agent] = request.env['HTTP_USER_AGENT'].to_s.first(200)
62
- ref_mod[:current_request_url] = request.url
63
- ref_mod[:current_request_referer_url] = request.env["HTTP_REFERER"] # or request.headers["HTTP_REFERER"]
67
+ ref_mod[:user_agent] = referer_tracking_utf8(request.env['HTTP_USER_AGENT']).to_s.first(200)
68
+ ref_mod[:current_request_url] = referer_tracking_utf8(request.url)
69
+ ref_mod[:current_request_referer_url] = referer_tracking_utf8(request.env["HTTP_REFERER"]) # or request.headers["HTTP_REFERER"]
64
70
  ref_mod[:session_id] = request.session["session_id"]
65
71
 
66
- unless cookies[RefererTracking.set_referer_cookies_name].blank?
67
- _cookie_ver, cookie_time_org, cookie_first_url, cookie_referer_url = cookies[RefererTracking.set_referer_cookies_name].to_s.split("|||")
72
+ # Sanitized before blank? and split, which match a regexp against the value and raise on
73
+ # invalid bytes instead of answering. Rack unescapes an incoming cookie into UTF-8 without
74
+ # checking the result, so whatever the client sent arrives here as it is.
75
+ ref_cookie = referer_tracking_utf8(cookies[RefererTracking.set_referer_cookies_name])
76
+ unless ref_cookie.blank?
77
+ _cookie_ver, cookie_time_org, cookie_first_url, cookie_referer_url = ref_cookie.split("|||")
68
78
  ref_mod[:cookie_first_url] = cookie_first_url
69
79
  ref_mod[:cookie_referer_url] = cookie_referer_url
70
80
  ref_mod[:cookie_time] = Time.at(cookie_time_org.to_i)
@@ -73,7 +83,7 @@ module RefererTracking::ControllerAddons
73
83
  if RefererTracking.save_cookies
74
84
  begin
75
85
  ref_mod[:cookies_yaml] = cookies.instance_variable_get('@cookies').to_yaml
76
- rescue
86
+ rescue => e
77
87
  str = "referer_tracking after create problem encoding cookie yml, probably non utf8 chars #{e}"
78
88
  logger.error(str)
79
89
  ref_mod[:cookies_yaml] = "error: #{str}"
@@ -87,6 +97,18 @@ module RefererTracking::ControllerAddons
87
97
  Rails.logger.info "RefererTracking::ControllerAddons.after_create problem with creating record: #{e}"
88
98
  end
89
99
 
100
+ # Everything the request hands us — headers tagged ASCII-8BIT by Rack, cookies unescaped into
101
+ # UTF-8 without a validity check — is scrubbed here before anything downstream looks at it.
102
+ #
103
+ # scrub rather than encode(invalid: :replace, undef: :replace): converting *from* ASCII-8BIT
104
+ # treats every byte above 0x7F as undefined, so encode turns a valid "hä" into "h??", and
105
+ # since Rack tags valid multibyte referers binary too that is the common case, not the edge
106
+ # case. scrub drops only the genuinely invalid bytes.
107
+ def referer_tracking_utf8(str)
108
+ str.nil? ? nil : str.to_s.dup.force_encoding(Encoding::UTF_8).scrub("")
109
+ end
110
+ private :referer_tracking_utf8
111
+
90
112
  ###############################################
91
113
  # Session add methods
92
114
 
@@ -1,3 +1,3 @@
1
1
  module RefererTracking
2
- VERSION = "5.0.0"
2
+ VERSION = "5.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: referer_tracking
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olli Huotari
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubygems_version: 4.0.20
118
+ rubygems_version: 3.6.9
119
119
  specification_version: 4
120
120
  summary: Referer tracking automates better tracking in your Rails app. It tells you
121
121
  who creates activerecord objects / models, where did they originally come from,