usps-support 0.2.67 → 0.2.68

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: 6ad988404fb8acfa5eb813cb28b5bd713b35ebad66fdb53adba4b32b1128439f
4
- data.tar.gz: f0187bd578ecdbb68ca7778ae0a45f056816dfa4f1e1d8dcbe508a6731588917
3
+ metadata.gz: 9ab084dcb52c5a1119ae74ca610dfe8b2a085e4696cff6ce8b1ff5762f127e70
4
+ data.tar.gz: ef0b3eb3c2adaf9d14dce1a21edd28c9d4b75d4e44ddac21fc8784e64c8c5be5
5
5
  SHA512:
6
- metadata.gz: b074fafea1a8586b18c95cd3f8f7fe28503fad5e52b397ef4a524867073542fccd9e80ed12141c1ac93430709d6fbd2145db5e57bb8420f5c352c030d329906f
7
- data.tar.gz: 7b3478b1c301cb42fe364bc7291eef417785283d068815d38ffe5fdc9f46a0d03c39fbe77c86e3672406a8069dc38760a5aeee28a3c7a0424a0a95e0311342ca
6
+ metadata.gz: 8cc4c79d0e24bf6ce257071e3bd43eeb34f776ab93277e80a36051da47f34d239d804b76db7c90df1555b8c661c2f73cdd3aa98b0b592a8e005ff22fdac488db
7
+ data.tar.gz: cf6505c09e4feb247910c4fea0c4a11334e665b868c96d2d66c6879b05651256004316dd8a1714b933dd50a0d90e6f3594d1e94c76683796a92ba9273ef5acba
data/Readme.md CHANGED
@@ -66,3 +66,19 @@ Dir.glob('lib/proto/extensions/*.rb').sort.each { |f| require_relative("../../#{
66
66
 
67
67
  Usps::Example::SerializableMessage.extend(Usps::Support::Lib::ProtoSerialize)
68
68
  ```
69
+
70
+ ### Blocked Email Domains
71
+
72
+ `Usps::Support::BlockedDomainInterceptor` drops `to`/`cc`/`bcc` recipients at domains that never accept mail
73
+ (`abcboating.com`, `abcemail.com`, `example.com`, and their subdomains), and suppresses delivery entirely if no
74
+ recipients remain. Each interception is logged and posted to the `#email-problems` Slack channel.
75
+
76
+ Register it in production only, since test suites routinely address mail to `example.com`:
77
+
78
+ ```ruby
79
+ # config/environments/production.rb
80
+ config.action_mailer.interceptors = %w[Usps::Support::BlockedDomainInterceptor]
81
+ ```
82
+
83
+ The Slack notice needs `SLACK_URL_EMAIL_PROBLEMS` (or `credentials.slack.email_problems`) in the app's production
84
+ environment. Without it, the interception still happens and is logged; only the Slack post is skipped, with a warning.
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'slack_notification'
4
+
5
+ module Usps::Support
6
+ # ActionMailer interceptor that drops recipients at domains known never to accept mail (placeholder
7
+ # addresses entered for members with no real email), so a send never goes out to them and bounces.
8
+ #
9
+ # Only the to/cc/bcc fields that actually held a blocked address are rewritten; the rest of the
10
+ # message is left untouched. If no recipients remain, delivery is suppressed. Either way, a notice is
11
+ # logged and posted to the #email-problems Slack channel.
12
+ #
13
+ # Register it in production only, from config/environments/production.rb:
14
+ #
15
+ # config.action_mailer.interceptors = %w[Usps::Support::BlockedDomainInterceptor]
16
+ #
17
+ # Test suites routinely address mail to example.com, so registering it in every environment would
18
+ # silently empty ActionMailer::Base.deliveries.
19
+ #
20
+ class BlockedDomainInterceptor
21
+ # Subdomains of these are blocked too.
22
+ BLOCKED_DOMAINS = %w[abcboating.com abcemail.com example.com].freeze
23
+
24
+ RECIPIENT_FIELDS = %i[to cc bcc].freeze
25
+ SLACK_CHANNEL = 'email-problems'
26
+
27
+ def self.delivering_email(message) = new(message).intercept
28
+
29
+ # Whether a bare email address (no display name) is at a blocked domain or one of its subdomains.
30
+ def self.blocked?(address)
31
+ domain = address.to_s.split('@').last.to_s.downcase
32
+ BLOCKED_DOMAINS.any? { domain == it || domain.end_with?(".#{it}") }
33
+ end
34
+
35
+ def initialize(message)
36
+ @message = message
37
+ end
38
+
39
+ def intercept
40
+ blocked = RECIPIENT_FIELDS.flat_map { remove_blocked(it) }
41
+ return if blocked.empty?
42
+
43
+ @message.perform_deliveries = false if no_recipients?
44
+ report(blocked)
45
+ end
46
+
47
+ private
48
+
49
+ # Rewrites the field from the formatted addresses it keeps, so display names survive.
50
+ def remove_blocked(field)
51
+ blocked, kept = addresses(field).partition { self.class.blocked?(it.address) }
52
+ @message.public_send(:"#{field}=", kept.empty? ? nil : kept.map(&:format)) if blocked.any?
53
+ blocked.map(&:address)
54
+ end
55
+
56
+ # A header that failed to parse as addresses carries no address list; leave it for delivery to judge.
57
+ def addresses(field)
58
+ header = @message[field]
59
+ header.respond_to?(:addrs) ? header.addrs : []
60
+ end
61
+
62
+ def no_recipients? = RECIPIENT_FIELDS.all? { Array(@message.public_send(it)).empty? }
63
+
64
+ def report(blocked)
65
+ Rails.logger.info("[#{self.class.name}] #{summary(blocked)}")
66
+ notify_slack(blocked)
67
+ end
68
+
69
+ def notify_slack(blocked)
70
+ SlackNotification.new(
71
+ channel: SLACK_CHANNEL, type: :info, title: 'Blocked Email Domain', fallback: summary(blocked),
72
+ fields: [
73
+ { title: 'Application', value: application_name, short: true },
74
+ { title: 'Delivery', value: delivery_status, short: true },
75
+ { title: 'Subject', value: @message.subject.to_s, short: false },
76
+ { title: 'Blocked Recipients', value: blocked.join("\n"), short: false }
77
+ ]
78
+ ).notify!
79
+ rescue StandardError => e
80
+ # Best effort: a Slack outage or a missing channel URL must never block the delivery itself.
81
+ Rails.logger.warn("[#{self.class.name}] Slack notification failed: #{e.class}: #{e.message}")
82
+ end
83
+
84
+ def summary(blocked)
85
+ "#{application_name}: removed #{blocked.join(', ')} from \"#{@message.subject}\"; #{delivery_status.downcase}"
86
+ end
87
+
88
+ def delivery_status
89
+ @message.perform_deliveries ? 'Sent to remaining recipients' : 'Suppressed (no recipients left)'
90
+ end
91
+
92
+ def application_name = Rails.application.class.module_parent_name.titleize
93
+ end
94
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Support
5
- VERSION = '0.2.67'
5
+ VERSION = '0.2.68'
6
6
  end
7
7
  end
data/lib/usps/support.rb CHANGED
@@ -20,6 +20,7 @@ require_relative 'support/models'
20
20
  require_relative 'support/lib'
21
21
  require_relative 'support/health_diagnostics'
22
22
  require_relative 'support/health_monitor'
23
+ require_relative 'support/blocked_domain_interceptor'
23
24
 
24
25
  # The Engine require lives in `usps/all`, not here. This file can
25
26
  # legitimately be required before Rails is loaded (e.g. from .simplecov to pull
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.67
4
+ version: 0.2.68
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -37,6 +37,26 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1'
40
+ - !ruby/object:Gem::Dependency
41
+ name: slack-notification
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.1'
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.1.18
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.1'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.1.18
40
60
  - !ruby/object:Gem::Dependency
41
61
  name: usps-imis-api
42
62
  requirement: !ruby/object:Gem::Requirement
@@ -96,6 +116,7 @@ files:
96
116
  - db/hq/websites_schema.rb
97
117
  - lib/usps/all.rb
98
118
  - lib/usps/support.rb
119
+ - lib/usps/support/blocked_domain_interceptor.rb
99
120
  - lib/usps/support/engine.rb
100
121
  - lib/usps/support/health_diagnostics.rb
101
122
  - lib/usps/support/health_monitor.rb