decoding 0.6.0 → 0.7.0

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: 3dcd003beb1f65e74c31189b632c9c290d636a20467f5fed1a8c382d1aff634b
4
- data.tar.gz: 338652cc9d8ea559a31fc9b14adc14a83cdc10c5730a175a1fa3670b87435b3d
3
+ metadata.gz: a7a5b525faf20e681dd01310ad1284bea882297110b581635f7e593dcff1fdd5
4
+ data.tar.gz: ee7e5a7de54788c50c17d2a98973a7c03bc5e8c892ce8027820c4bd4ff918bad
5
5
  SHA512:
6
- metadata.gz: 1a20f3f88d3970063dd6b3f8890ee8f0646df418e0c157fffe3081f6de6c2c3b7718cd685335c1b7f108e21f110d5823be6bdab3e37a222c2d6b4a728bcb820f
7
- data.tar.gz: 31edcf5c7f5721d93308269883d271b1cd1336738a1013c18c5d21e071dddfd3de855fb3091b5d2426ddc1e57f19baca471051cbffdddf2cc47e7b157120bd1e
6
+ metadata.gz: 51e84d624feb1ded893a89d025c0372707d8ef23e8d6a626e976f5a2a5ee08449cbbb559ce5e5a5ffaf8a4b0e916500bd961b3d1d1e21d0fb55290297cb91571
7
+ data.tar.gz: cfc8871eeb93f82c1bf4449ad415e4349b1d114070e04d787ecad75f6f45abd0a7767256634b73d56015f0578de79ac115c22ce1d7bbe573b3f90393c658e1df
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.7.0]
4
+
5
+ * Leave the value out of the error `Decoding.env` raises, as an environment variable can hold a secret. A message in the form `expected ..., got <inspected value>` gets `[REDACTED]` in place of the value; any other message is left out altogether
6
+ * Add `http_url` decoder for an absolute http or https URL with a host, returned as a string, available after `require "decoding/decoders/uri"`
7
+
3
8
  ## [0.6.0]
4
9
 
5
10
  * Decode arrays, hashes and multi-field decoders (such as `decode_hash` and `Decoding::Data`) in linear rather than quadratic time. Decoding 1,600 records is about 29 times faster.
data/README.md CHANGED
@@ -247,6 +247,13 @@ Decoding.decode(D.uri, "https://example.com") # => Decoding::Ok(URI("https://exa
247
247
  Decoding.decode(D.uri, 123) # => Decoding::Err("expected a URI, got 123")
248
248
  ```
249
249
 
250
+ The same file provides `http_url`, which is stricter: it accepts only an absolute `http` or `https` URL with a host, and returns it as a string. `uri` also accepts an empty string and a host without a scheme, so reach for `http_url` when a value has to be a working web address:
251
+
252
+ ```ruby
253
+ Decoding.decode(D.http_url, "https://example.com") # => Decoding::Ok("https://example.com")
254
+ Decoding.decode(D.http_url, "example.com") # => Decoding::Err("expected an http(s) URL, got \"example.com\"")
255
+ ```
256
+
250
257
  * `time` and `date` -- decode a `Time` or `Date` object, or a string in a given format. The format is either the name of a standard format or a `strptime` pattern:
251
258
 
252
259
  ```ruby
@@ -317,15 +324,18 @@ The named types are `:string` (the default), `:symbol`, `:integer`, `:float` and
317
324
  require "decoding/decoders/uri"
318
325
 
319
326
  Decoding.env("DATABASE_URL", D.uri) # => #<URI::Generic postgres://localhost/app>
327
+ Decoding.env("API_URL", D.http_url) # => "https://api.example.com"
320
328
  ```
321
329
 
322
- Failures name the variable, so the reason an application would not start is clear:
330
+ Failures name the variable, so the reason an application would not start is clear. They leave out the value, as a variable can hold a secret, such as a password in a URL or a token in its query string, that would otherwise end up in the logs:
323
331
 
324
332
  ```
325
333
  Decoding::UnwrapError: ENV["PORT"] is not set
326
- Decoding::UnwrapError: ENV["PORT"]: expected an integer, got "abc"
334
+ Decoding::UnwrapError: ENV["PORT"]: expected an integer, got [REDACTED]
327
335
  ```
328
336
 
337
+ Only a message in the form the built-in decoders use, `expected ..., got` followed by the inspected value, keeps its description. Any other message is left out altogether, since there is no telling which part of the value it shows: `ENV["PORT"]: cannot be decoded; the value is not shown`. An empty value hides nothing and stays visible.
338
+
329
339
  A variable set to an empty string counts as set, so it still has to decode rather than quietly falling back to the default. To allow a variable to be absent without a default, give it a decoder that accepts `nil`:
330
340
 
331
341
  ```ruby
@@ -23,5 +23,42 @@ module Decoding
23
23
  )
24
24
  ) { |_msg, value| "expected a URI, got #{value.inspect}" }
25
25
  end
26
+
27
+ # Decode an absolute http or https URL into a string: a URI object, or a
28
+ # string that can be parsed as one, with an http or https scheme and a host.
29
+ #
30
+ # Stricter than {uri}, which also accepts an empty string and a host without
31
+ # a scheme. That makes it the one to read a URL from the environment with,
32
+ # where a mistyped value should stop the application from starting.
33
+ #
34
+ # A string also has to come out of parsing unchanged. The parser escapes
35
+ # rather than refuses some mistakes, such as a space at the end of a query
36
+ # string, and returning the escaped URL would hide the mistake.
37
+ #
38
+ # @example
39
+ # decode(http_url, "https://example.com") # => Decoding::Ok("https://example.com")
40
+ # decode(http_url, "example.com") # => Decoding::Err("expected an http(s) URL, got \"example.com\"")
41
+ # @return [Decoding::Decoder<String>]
42
+ # @see Decoding::Decoders.uri
43
+ def http_url
44
+ Decoders.map_err(
45
+ Decoders.and_then(Decoders.original) do |value|
46
+ Decoders.and_then(uri) do |parsed|
47
+ if web_address?(parsed, value)
48
+ Decoders.succeed(parsed.to_s)
49
+ else
50
+ Decoders.fail("expected an http or https scheme, a host and no characters the parser would escape")
51
+ end
52
+ end
53
+ end
54
+ ) { |_msg, value| "expected an http(s) URL, got #{value.inspect}" }
55
+ end
56
+
57
+ # @private
58
+ def web_address?(parsed, value)
59
+ parsed.is_a?(::URI::HTTP) && !parsed.host.to_s.empty? && (!value.is_a?(::String) || parsed.to_s == value)
60
+ end
61
+
62
+ private_class_method :web_address?
26
63
  end
27
64
  end
data/lib/decoding/env.rb CHANGED
@@ -51,12 +51,36 @@ module Decoding
51
51
 
52
52
  decode!(
53
53
  Decoders.map_err(decoder) do |message, value|
54
- value.nil? ? "ENV[#{name.inspect}] is not set" : "ENV[#{name.inspect}]: #{message}"
54
+ value.nil? ? "ENV[#{name.inspect}] is not set" : "ENV[#{name.inspect}]: #{redact_env_value(message, value)}"
55
55
  end,
56
56
  from.fetch(name, nil)
57
57
  )
58
58
  end
59
59
 
60
+ # Takes the value out of a decoder's error message, as an environment
61
+ # variable can hold a secret (a password in a URL, a token in its query
62
+ # string) that would otherwise end up in the logs of an application that
63
+ # refused to start.
64
+ #
65
+ # Only a message in the form the built-in decoders use, `expected ..., got`
66
+ # followed by the inspected value, keeps its description, with `[REDACTED]`
67
+ # in place of the value, and only when that description does not contain the
68
+ # value as well. Any other message is left out altogether, as there is no
69
+ # telling which part of the value it shows. An empty value has nothing to
70
+ # hide and stays visible.
71
+ #
72
+ # @private
73
+ def redact_env_value(message, value)
74
+ return message if value == ""
75
+
76
+ shown = ", got #{value.inspect}"
77
+ description = message.delete_suffix(shown)
78
+ return "cannot be decoded; the value is not shown" unless message.start_with?("expected ") && message.end_with?(shown)
79
+ return "cannot be decoded; the value is not shown" if [value.to_s, value.to_s.inspect[1..-2]].any? { description.include?(_1) }
80
+
81
+ "#{description}, got [REDACTED]"
82
+ end
83
+
60
84
  # @private
61
85
  def env_decoder(type)
62
86
  return type.to_decoder if type.respond_to?(:to_decoder)
@@ -66,5 +90,5 @@ module Decoding
66
90
  Decoders.public_send(ENV_TYPES.fetch(type))
67
91
  end
68
92
 
69
- private_class_method :env_decoder
93
+ private_class_method :env_decoder, :redact_env_value
70
94
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arjan van der Gaag