akita-har_logger 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51fcd2da35cb17ecb365988d7975f746a717f12b002d3a20f1d0c3eab245b10c
4
- data.tar.gz: 4972d841dfc7c8dcd41085137d45faedeb450836d46e6d3f14d584785d997a24
3
+ metadata.gz: d1a952e08b6244134dc4498e24b7185a700fe6d60abdc257bae8e5c3f67a57c6
4
+ data.tar.gz: 205780425d013375017a8677630c68c078b3be55772ebc0bebf0968d8e16601b
5
5
  SHA512:
6
- metadata.gz: 8fdffa25d122a573e8c72526934e5e713fbdc573c0d4ebfa13383d749ea856f9b01d2e5bc43e89b1c152df64f97cd965ad54bd5a4ebab924d222a1829bbbec60
7
- data.tar.gz: 8ca06a98ce89ec4e9c3e6ac7f3b10ec98f73c44aacab1af0bac8c81649f342ec721017ee57ff6590c93a24234cce9d40c9b341aa8277a39a59bc86a3a9c3484e
6
+ metadata.gz: 0beadf21aa88256c40d0b6bc4c2e0c54dae7656aea1cd472b8ae7a610484969f70ed9239c3a783362eaf68be41c373085cb181eb456bce413510e8f50d5a50ed
7
+ data.tar.gz: 622997ac27c899f809b1d7c632ca0da6f8916f00a25b9cdb22d8d6861b1107c8226cfd7802c9b03745d774268780d066db5bcb739081a06c66cd89cee235016a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.4)
4
+ akita-har_logger (0.2.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -5,12 +5,20 @@ module Akita
5
5
  class HarUtils
6
6
  # Rack apparently uses 8-bit ASCII for everything, even when the string
7
7
  # is not 8-bit ASCII. This reinterprets 8-bit ASCII strings as UTF-8.
8
+ #
9
+ # If we are unable to do this reinterpretation, return the string
10
+ # unchanged, but log a warning that points to the caller.
8
11
  def self.fixEncoding(v)
9
- if v == nil || v.encoding != Encoding::ASCII_8BIT then
10
- v
11
- else
12
- String.new(v).force_encoding(Encoding::UTF_8)
12
+ if v != nil && v.encoding == Encoding::ASCII_8BIT then
13
+ forced = String.new(v).force_encoding(Encoding::UTF_8)
14
+ if forced.valid_encoding? then
15
+ v = forced
16
+ else
17
+ Rails.logger.warn "[#{caller_locations(1, 1)}] Unable to fix encoding: not a valid UTF-8 string. This will likely cause JSON serialization to fail."
18
+ end
13
19
  end
20
+
21
+ v
14
22
  end
15
23
 
16
24
  # Converts a Hash into a list of Hash objects. Each entry in the given
@@ -85,7 +85,7 @@ module Akita
85
85
  return Encoding::ISO_8859_1
86
86
  end
87
87
 
88
- Encoding.default_external
88
+ Encoding::ASCII_8BIT
89
89
  end
90
90
 
91
91
  # Obtains the posted data from an HTTP environment.
@@ -117,8 +117,27 @@ module Akita
117
117
  # body when the request specifies UTF-8. Reinterpret the content
118
118
  # body according to what the request says it is, and re-encode into
119
119
  # UTF-8.
120
- result[:text] = req.body.string.encode(Encoding::UTF_8,
121
- getPostDataCharSet(env))
120
+ #
121
+ # Gracefully handle any characters that are invalid in the source
122
+ # encoding and characters that have no UTF-8 representation by
123
+ # replacing with '?'. Log a warning when this happens.
124
+ source = req.body.string.force_encoding(getPostDataCharSet(env))
125
+ utf8EncodingSuccessful = false
126
+ if source.valid_encoding? then
127
+ begin
128
+ result[:text] = source.encode(Encoding::UTF_8)
129
+ utf8EncodingSuccessful = true
130
+ rescue Encoding::UndefinedConversionError
131
+ Rails.logger.warn "[#{caller_locations(0, 1)}] Unable to losslessly convert request body from #{source.encoding} to UTF-8. Characters undefined in UTF-8 will be replaced with '?'."
132
+ end
133
+ else
134
+ Rails.logger.warn "[#{caller_locations(0, 1)}] Request body is not valid #{source.encoding}. Invalid characters and characters undefined in UTF-8 will be replaced with '?'."
135
+ end
136
+
137
+ if !utf8EncodingSuccessful then
138
+ result[:text] = source.encode(Encoding::UTF_8,
139
+ invalid: :replace, undef: :replace, replace: '?')
140
+ end
122
141
  end
123
142
 
124
143
  result
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.4"
5
+ VERSION = "0.2.5"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akita-har_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jed Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-11 00:00:00.000000000 Z
11
+ date: 2021-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec