Skip to content

Commit 8f6c3f7

Browse files
Implement safe parsing when body is not base64-encoded (#3)
* Implement safe parsing when `body` is not base64-encoded * Address comments and clean up logic
1 parent 7271167 commit 8f6c3f7

8 files changed

Lines changed: 302 additions & 5 deletions

File tree

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1.2

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
source 'https://rubygems.org'
22

33
gem 'moesif_api'
4+
5+
group :development, :test do
6+
gem 'rspec'
7+
end

Gemfile.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ GEM
33
specs:
44
addressable (2.8.1)
55
public_suffix (>= 2.0.2, < 6.0)
6+
diff-lcs (1.5.1)
67
domain_name (0.5.20190701)
78
unf (>= 0.0.5, < 1.0.0)
89
http-accept (1.7.0)
@@ -28,6 +29,19 @@ GEM
2829
http-cookie (>= 1.0.2, < 2.0)
2930
mime-types (>= 1.16, < 4.0)
3031
netrc (~> 0.8)
32+
rspec (3.13.0)
33+
rspec-core (~> 3.13.0)
34+
rspec-expectations (~> 3.13.0)
35+
rspec-mocks (~> 3.13.0)
36+
rspec-core (3.13.1)
37+
rspec-support (~> 3.13.0)
38+
rspec-expectations (3.13.3)
39+
diff-lcs (>= 1.2.0, < 2.0)
40+
rspec-support (~> 3.13.0)
41+
rspec-mocks (3.13.1)
42+
diff-lcs (>= 1.2.0, < 2.0)
43+
rspec-support (~> 3.13.0)
44+
rspec-support (3.13.1)
3145
unf (0.1.4)
3246
unf_ext
3347
unf_ext (0.0.8.2)
@@ -37,6 +51,7 @@ PLATFORMS
3751

3852
DEPENDENCIES
3953
moesif_api
54+
rspec
4055

4156
BUNDLED WITH
4257
2.1.4

lib/moesif_aws_lambda/moesif_aws_middleware.rb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,31 @@ def decompress_body(body)
5858
Zlib::GzipReader.new(StringIO.new(body)).read
5959
end
6060

61-
def base64_encode_body(body)
62-
return Base64.encode64(body), "base64"
61+
def base64_encode_body(data)
62+
return Base64.encode64(data), "base64"
63+
end
64+
65+
def is_base64_str(body)
66+
if not body.instance_of?(String)
67+
return false
68+
end
69+
if body.length % 4 != 0
70+
return false
71+
end
72+
73+
b64_regex = /^[A-Za-z0-9+\/]+={0,2}$/
74+
75+
if not body.match?(b64_regex)
76+
return false
77+
end
78+
79+
begin
80+
_ = Base64.decode64(body)
81+
return true
82+
rescue
83+
return false
84+
end
85+
6386
end
6487

6588
def @moesif_helpers.log_debug(message)
@@ -68,7 +91,27 @@ def @moesif_helpers.log_debug(message)
6891
end
6992
end
7093

71-
def parse_body(body, headers)
94+
def process_body(body_wrapper, headers)
95+
# the `event` object can either be a Hash or a String
96+
if body_wrapper.instance_of?(Hash) and body_wrapper.key?("body")
97+
body = body_wrapper.fetch("body")
98+
begin
99+
if body_wrapper.fetch("isBase64Encoded", false) and is_base64_str(body)
100+
return body, "base64"
101+
else
102+
return safe_json_parse(body, headers)
103+
end
104+
rescue
105+
return base64_encode_body(body)
106+
end
107+
elsif body_wrapper.instance_of?(String)
108+
return base64_encode_body(body_wrapper)
109+
else
110+
return nil, nil
111+
end
112+
end
113+
114+
def safe_json_parse(body, headers)
72115
begin
73116
if (body.instance_of?(Hash) || body.instance_of?(Array))
74117
parsed_body = body
@@ -187,8 +230,12 @@ def handle(event:, context:)
187230
event_req.headers = req_headers
188231

189232
if @log_body
190-
event_req.body = event["body"]
191-
event_req.transfer_encoding = event["isBase64Encoded"] ? "base64" : "json"
233+
if event.key?("body") and event.fetch("isBase64Encoded") and is_base64_str(event.fetch("body"))
234+
event_req.body= event.fetch("body")
235+
event_req.transfer_encoding = "base64"
236+
else
237+
event_req.body, event_req.transfer_encoding = process_body(event, req_headers)
238+
end
192239
end
193240

194241
# RESPONSEE

moesif_aws_lambda.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
88
s.homepage = 'https://moesif.com'
99
s.license = 'Apache-2.0'
1010
s.add_development_dependency('test-unit', '~> 3.5', '>= 3.5.0')
11+
s.add_development_dependency('rspec', '~> 3.5')
1112
s.add_dependency('moesif_api', '~> 1.2.14')
1213
s.required_ruby_version = '>= 2.5'
1314
s.files = Dir['{lib}/**/*', 'README*', 'LICENSE*']

spec/moesif_aws_middleware_spec.rb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
require 'rspec'
2+
require 'json'
3+
require_relative '../lib/moesif_aws_lambda'
4+
5+
class AwsContext
6+
attr_accessor :aws_request_id, :function_name
7+
8+
def initialize
9+
@function_name = 'test function'
10+
@function_version = '123421'
11+
@aws_request_id = Time.now.to_i
12+
end
13+
end
14+
15+
fake_event_str = {
16+
'rawPath' => '/test/route',
17+
'headers' => {
18+
'foo' => 'bar'
19+
},
20+
'requestContext' => {
21+
'http' => {
22+
'method' => 'post'
23+
}
24+
},
25+
'isBase64Encoded' => true,
26+
'body' => 'hello world'
27+
}
28+
fake_event_hash = {
29+
'rawPath' => '/test/route',
30+
'headers' => {
31+
'foo' => 'bar'
32+
},
33+
'requestContext' => {
34+
'http' => {
35+
'method' => 'post'
36+
}
37+
},
38+
'isBase64Encoded' => true,
39+
'body' => {
40+
'msg' => 'Hello world!',
41+
'city' => 'New York',
42+
'year' => 2024
43+
}
44+
}
45+
fake_event_json = {
46+
'rawPath' => '/test/route',
47+
'headers' => {
48+
'foo' => 'bar'
49+
},
50+
'requestContext' => {
51+
'http' => {
52+
'method' => 'post'
53+
}
54+
},
55+
'isBase64Encoded' => true,
56+
'body' => '{
57+
"foo": "bar",
58+
"year": 2024
59+
}'
60+
}
61+
fake_event_b64 = {
62+
'rawPath' => '/test/route',
63+
'headers' => {
64+
'foo' => 'bar'
65+
},
66+
'requestContext' => {
67+
'http' => {
68+
'method' => 'post'
69+
}
70+
},
71+
'isBase64Encoded' => true,
72+
'body' => 'eyJmb28iOiJiYXIifQ=='
73+
}
74+
75+
describe Moesif::MoesifAwsMiddleware do
76+
handler = Proc.new{ |event:, context:|
77+
{ event: JSON.generate(event), context: JSON.generate(context.inspect), my_test: 12_342 }
78+
}
79+
options = {
80+
'application_id' => '',
81+
'debug' => true
82+
}
83+
84+
let(:moesif) { Moesif::MoesifAwsMiddleware.new(handler, options) }
85+
86+
describe '#process_body' do
87+
it 'parses a string body correctly' do
88+
body, transfer_encoding = moesif.process_body(fake_event_str, fake_event_str.fetch('headers'))
89+
expect(transfer_encoding).to eq('base64')
90+
expect(body == 'aGVsbG8gd29ybGQ=')
91+
end
92+
it 'parses a hash body correctly' do
93+
body, transfer_encoding = moesif.process_body(fake_event_hash, fake_event_hash.fetch('headers'))
94+
expect(transfer_encoding).to eq('json')
95+
expect(body.instance_of?(Hash)).to be true
96+
end
97+
it 'parses a JSON body correctly' do
98+
body, transfer_encoding = moesif.process_body(fake_event_json, fake_event_json.fetch('headers'))
99+
expect(transfer_encoding).to eq('json')
100+
expect(body.instance_of?(Hash)).to be true
101+
end
102+
it 'parses a valid base64-encoded body correctly' do
103+
body, transfer_encoding = moesif.process_body(fake_event_b64, fake_event_b64.fetch('headers'))
104+
expect(transfer_encoding).to eq('base64')
105+
expect(body).to eq('eyJmb28iOiJiYXIifQ==')
106+
end
107+
end
108+
109+
describe '#is_base64_str' do
110+
it 'returns true for a valid base64-encoded string' do
111+
valid_base64 = 'eyJmb28iOiJiYXIifQ=='
112+
expect(moesif.is_base64_str(valid_base64)).to be true
113+
end
114+
it 'returns false for an invalid base64-encoded string' do
115+
invalid_base64 = {
116+
'name' => 'Alex',
117+
'age' => 27
118+
}
119+
expect(moesif.is_base64_str(invalid_base64)).to be false
120+
end
121+
end
122+
123+
describe '.handle' do
124+
it 'returns a Lambda result as a Hash object' do
125+
result = moesif.handle(event: fake_event_str, context: AwsContext.new)
126+
puts result
127+
expect(result.instance_of?(Hash)).to be true
128+
end
129+
end
130+
end

spec/spec_helper.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
RSpec.configure do |config|
17+
# rspec-expectations config goes here. You can use an alternate
18+
# assertion/expectation library such as wrong or the stdlib/minitest
19+
# assertions if you prefer.
20+
config.expect_with :rspec do |expectations|
21+
# This option will default to `true` in RSpec 4. It makes the `description`
22+
# and `failure_message` of custom matchers include text for helper methods
23+
# defined using `chain`, e.g.:
24+
# be_bigger_than(2).and_smaller_than(4).description
25+
# # => "be bigger than 2 and smaller than 4"
26+
# ...rather than:
27+
# # => "be bigger than 2"
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
# rspec-mocks config goes here. You can use an alternate test double
32+
# library (such as bogus or mocha) by changing the `mock_with` option here.
33+
config.mock_with :rspec do |mocks|
34+
# Prevents you from mocking or stubbing a method that does not exist on
35+
# a real object. This is generally recommended, and will default to
36+
# `true` in RSpec 4.
37+
mocks.verify_partial_doubles = true
38+
end
39+
40+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41+
# have no way to turn it off -- the option exists only for backwards
42+
# compatibility in RSpec 3). It causes shared context metadata to be
43+
# inherited by the metadata hash of host groups and examples, rather than
44+
# triggering implicit auto-inclusion in groups with matching metadata.
45+
config.shared_context_metadata_behavior = :apply_to_host_groups
46+
47+
# The settings below are suggested to provide a good initial experience
48+
# with RSpec, but feel free to customize to your heart's content.
49+
=begin
50+
# This allows you to limit a spec run to individual examples or groups
51+
# you care about by tagging them with `:focus` metadata. When nothing
52+
# is tagged with `:focus`, all examples get run. RSpec also provides
53+
# aliases for `it`, `describe`, and `context` that include `:focus`
54+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55+
config.filter_run_when_matching :focus
56+
57+
# Allows RSpec to persist some state between runs in order to support
58+
# the `--only-failures` and `--next-failure` CLI options. We recommend
59+
# you configure your source control system to ignore this file.
60+
config.example_status_persistence_file_path = "spec/examples.txt"
61+
62+
# Limits the available syntax to the non-monkey patched syntax that is
63+
# recommended. For more details, see:
64+
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
65+
config.disable_monkey_patching!
66+
67+
# This setting enables warnings. It's recommended, but in some cases may
68+
# be too noisy due to issues in dependencies.
69+
config.warnings = true
70+
71+
# Many RSpec users commonly either run the entire suite or an individual
72+
# file, and it's useful to allow more verbose output when running an
73+
# individual spec file.
74+
if config.files_to_run.one?
75+
# Use the documentation formatter for detailed output,
76+
# unless a formatter has already been configured
77+
# (e.g. via a command-line flag).
78+
config.default_formatter = "doc"
79+
end
80+
81+
# Print the 10 slowest examples and example groups at the
82+
# end of the spec run, to help surface which specs are running
83+
# particularly slow.
84+
config.profile_examples = 10
85+
86+
# Run specs in random order to surface order dependencies. If you find an
87+
# order dependency and want to debug it, you can fix the order by providing
88+
# the seed, which is printed after each run.
89+
# --seed 1234
90+
config.order = :random
91+
92+
# Seed global randomization in this process using the `--seed` CLI option.
93+
# Setting this allows you to use `--seed` to deterministically reproduce
94+
# test failures related to randomization by passing the same `--seed` value
95+
# as the one that triggered the failure.
96+
Kernel.srand config.seed
97+
=end
98+
end

0 commit comments

Comments
 (0)