Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Zerobounce::Warmup#download_seeds` and `#download_statistics` raise `Zerobounce::ApiResponseError` (with the HTTP status) for an HTML page on a 200, such as a proxy's "Access denied" page, as the Python SDK does. Up to 3.0.1 the page was returned as the CSV text.
- An HTML page on a 200 (such as a proxy's "Access denied" page), or a JSON content type with a body that does not parse, raises `Zerobounce::ApiResponseError` with `status_code` 200 from every request method, as the Python and PHP SDKs do; so does any other non-JSON body from the calls that answer in JSON. Up to 3.0.1 `validate_file_send`, `scoring_file_send` (and their `_stream` forms), `send_email_finder_file`, `send_domain_search_file`, `email_finder_file_status`, `email_finder_file_delete`, `domain_search_file_status`, `domain_search_file_delete`, `send_list_evaluator_file`, `send_evaluated_file` and `list_s3_files` returned an HTML or other non-JSON body as a successful `String`; `validate_file_check`, `validate_file_delete`, `scoring_file_check` and `scoring_file_delete` raised a plain `RuntimeError` with no status for an HTML page, and `JSON::ParserError` for a JSON content type with a body that does not parse.
- `Zerobounce::Warmup#add_sending_account` parses `ignore_conflict` strictly, as the client parses its boolean options: `true`/`false`, `'true'`/`'false'` (any case) and `1`/`0` are accepted, and anything else raises `ArgumentError` before a request is sent. Up to 3.0.1 any truthy value was sent as `true`, so `ignore_conflict: 'false'` sent `true`. `ignore_conflict: nil` now leaves the field out (3.0.1 sent `false`).
- `Zerobounce::Warmup#add_sending_account` sends the documented ZeroBounce Managed fields: `email_content` takes `company_name`, `company_website`, `company_description`, `types_of_emails` and `example_email`, and `managed_sending` takes `weekdays_only`, `timezone`, `timezone_offset` and `warmup_start_date`, as the Python SDK and the API docs have them. Up to 3.0.1 it dropped `company_website`, `company_description`, `example_email`, `weekdays_only` and `timezone` without a word and sent `industry`, `website` and `daily_limit`, which the API does not read. The old `website` key is still accepted and sent as `company_website`; `industry`, `daily_limit` and any other unknown key are left out, as unknown keys always were, but now with a warning that names the documented fields. `weekdays_only` is parsed strictly (`'false'` is false; anything that is not a boolean raises `ArgumentError`), `timezone` must be a String, and `timezone_offset` takes fractional hours (-11 to 14, e.g. `3.0`), which 3.0.1 rejected.

### Security

- No error raised by the SDK carries the API key when a response body quotes it. Messages built from a body (a 2xx `{"error": "Invalid API key <key>"}`, a `{"success": false, "message": ...}`, a batch `"all"` error, an HTML page's title, a result-file download's JSON error) were passed on unredacted on every client method up to 3.0.1; only HTTP error statuses were redacted. `Zerobounce::ApiResponseError` now redacts its message itself (raw and URL-encoded forms), so every path that raises it is covered. A body that is not valid JSON no longer keeps the parser's error, which quotes the body, as `#cause` (client and `Zerobounce::Warmup`).

## [3.0.1] - 2026-11-11

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,16 @@ Or have ZeroBounce provision the mailbox. A 200 means the request was accepted;
```ruby
warmup.add_sending_account(
'sender@example.com', 'zb_managed',
email_content: { types_of_emails: %w[newsletter marketing] },
managed_sending: { timezone_offset: -5, warmup_start_date: Date.new(2026, 3, 4) }
email_content: { company_name: 'Acme', company_website: 'https://acme.example',
company_description: 'B2B email tools', types_of_emails: %w[newsletter marketing],
example_email: 'Hi, a short intro to Acme.' },
managed_sending: { weekdays_only: true, timezone: 'America/New_York', timezone_offset: -5,
warmup_start_date: Date.new(2026, 3, 4) }
)
```

Keys other than these are not API fields: they are left out of the request with a warning.

A custom engagement rule can be changed only once every seven days; inside that window the API answers 429.

```ruby
Expand Down
4 changes: 3 additions & 1 deletion lib/zerobounce/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def self.__parse_json_body__(response)
begin
JSON.parse(body)
rescue JSON::ParserError
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', code)
# cause: nil, or the parser's error, which quotes the body, would ride
# along as #cause.
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', code), cause: nil
end
end

Expand Down
14 changes: 13 additions & 1 deletion lib/zerobounce/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ def parse200(env)
# successful HTTP response; both carry the same short message. It is a
# RuntimeError, which those errors were before, so `rescue RuntimeError`
# still catches them.
#
# Its message is often built from a response body, which can quote the
# API key (an "Invalid API key <key>" message, a proxy echoing the request
# URL, an HTML title). Every message is therefore redacted here, once, so
# each path that raises this error inherits it.
class ApiResponseError < RuntimeError
# @return [Integer] the HTTP status of the failed response, or 0
attr_reader :status_code

def initialize(message, status_code = 0)
super(message)
super(ApiResponseError.redact(message))
@status_code = status_code.to_i
end

# The message with the request's API key replaced by ***.
def self.redact(message)
return message unless defined?(Zerobounce::BaseRequest)

Zerobounce::BaseRequest.__redact_api_key__(message)
end
end

# Server returned a 500 error.
Expand Down
5 changes: 4 additions & 1 deletion lib/zerobounce/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def self.interpret_json_response(response, path = nil)
begin
parsed = JSON.parse(body)
rescue JSON::ParserError
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', response.code)
# cause: nil, or the parser's error, which quotes the body, would ride
# along as #cause.
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', response.code),
cause: nil
end
return parsed unless parsed.is_a?(Hash)

Expand Down
81 changes: 66 additions & 15 deletions lib/zerobounce/warmup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class Warmup
ONBOARDING_MODELS = %w[self_managed zb_managed].freeze
EMAIL_TYPES = %w[transactional newsletter marketing].freeze

# The documented email_content fields
# (https://www.zerobounce.net/docs/warmup-api/adding-a-sending-account).
EMAIL_CONTENT_FIELDS = %w[company_name company_website company_description types_of_emails example_email].freeze
# The documented managed_sending fields.
MANAGED_SENDING_FIELDS = %w[weekdays_only timezone timezone_offset warmup_start_date].freeze

# Required by the schema but always replaced with the key's own team.
ANY_TEAM_ID = 1

Expand Down Expand Up @@ -74,9 +80,12 @@ def get_sending_account(from_address)
# Adds a from-address to warmup. POST /sending-accounts
#
# +self_managed+ runs your own mailbox and needs +engagement_rule_id+ 0-4.
# +zb_managed+ has ZeroBounce provision it and takes +email_content+ and
# +managed_sending+; a 200 means accepted, and the mailbox is provisioned
# afterwards.
# +zb_managed+ has ZeroBounce provision it and takes +email_content+
# (+company_name+, +company_website+, +company_description+,
# +types_of_emails+, +example_email+) and +managed_sending+
# (+weekdays_only+, +timezone+, +timezone_offset+ -11 to 14,
# +warmup_start_date+); a 200 means accepted, and the mailbox is
# provisioned afterwards. Other keys are not sent and draw a warning.
def add_sending_account(from_address, onboarding_model, **options)
require_value(from_address, 'from_address')
unless ONBOARDING_MODELS.include?(onboarding_model.to_s)
Expand Down Expand Up @@ -351,39 +360,81 @@ def engagement_body(c_from, reply_pct, important_pct, click_pct)
'important_pct' => important_pct, 'click_pct' => click_pct }
end

# The value of +key+ (a Symbol or String key) and whether it was given.
# Looked up with key? so a false value is kept.
def field(hash, key)
return [true, hash[key.to_sym]] if hash.key?(key.to_sym)
return [true, hash[key.to_s]] if hash.key?(key.to_s)

[false, nil]
end

# Keys that are not API fields are left out of the request, with a
# warning naming the documented fields, as unknown keys were always left
# out (silently, up to 3.0.1).
def warn_unknown(hash, known, name)
unknown = hash.keys.map(&:to_s).uniq - known
return if unknown.empty?

warn "Zerobounce::Warmup#add_sending_account: #{name} #{unknown.join(', ')} " \
"#{unknown.length == 1 ? 'is not an API field' : 'are not API fields'} and not sent; " \
"the fields are #{known.join(', ')}"
end

def email_content_body(content)
raise ArgumentError, 'email_content must be a Hash' unless content.is_a?(Hash)

body = {}
types = content[:types_of_emails] || content['types_of_emails']
unless types.nil?
given, types = field(content, :types_of_emails)
if given && !types.nil?
Array(types).each do |kind|
unless EMAIL_TYPES.include?(kind.to_s)
raise ArgumentError, 'types_of_emails must be transactional, newsletter and/or marketing'
end
end
body['types_of_emails'] = Array(types).map(&:to_s)
end
%i[industry company_name website].each do |key|
value = content[key] || content[key.to_s]
body[key.to_s] = value unless value.nil?
%w[company_name company_website company_description example_email].each do |key|
given, value = field(content, key)
body[key] = value if given && !value.nil?
end
# Up to 3.0.1 the SDK took the website as "website", which the API does
# not read; it is still accepted and sent as company_website.
given, website = field(content, :website)
body['company_website'] = website if given && !website.nil? && !body.key?('company_website')
warn_unknown(content, EMAIL_CONTENT_FIELDS + %w[website], 'email_content')
body
end

def managed_sending_body(sending)
raise ArgumentError, 'managed_sending must be a Hash' unless sending.is_a?(Hash)

body = {}
offset = sending[:timezone_offset] || sending['timezone_offset']
body['timezone_offset'] = in_range(offset, -11, 14, 'timezone_offset') unless offset.nil?
start = sending[:warmup_start_date] || sending['warmup_start_date']
body['warmup_start_date'] = api_date(start, 'warmup_start_date') unless start.nil?
limit = sending[:daily_limit] || sending['daily_limit']
body['daily_limit'] = limit unless limit.nil?
given, weekdays = field(sending, :weekdays_only)
body['weekdays_only'] = StrictBool.parse(weekdays, 'weekdays_only') if given && !weekdays.nil?
given, timezone = field(sending, :timezone)
if given && !timezone.nil?
raise ArgumentError, 'timezone must be a String, e.g. Europe/Bucharest' unless timezone.is_a?(String)

body['timezone'] = timezone
end
given, offset = field(sending, :timezone_offset)
body['timezone_offset'] = timezone_offset(offset) if given && !offset.nil?
given, start = field(sending, :warmup_start_date)
body['warmup_start_date'] = api_date(start, 'warmup_start_date') if given && !start.nil?
warn_unknown(sending, MANAGED_SENDING_FIELDS, 'managed_sending')
body
end

# -11 to 14, whole or fractional hours (the docs' example sends 3.0).
def timezone_offset(value)
unless (value.is_a?(Integer) || value.is_a?(Float)) && value.between?(-11, 14)
raise ArgumentError, 'timezone_offset must be -11 to 14'
end

value
end

# -- transport ----------------------------------------------------------

# Replaces every form of a secret in a message with ***.
Expand Down Expand Up @@ -457,7 +508,7 @@ def json_request(method, path, params = {}, body = nil, secrets: [])
response.code, (response.headers[:content_type] || '').to_s, text
), secrets
), response.code
)
), cause: nil # the parser's error quotes the body
end
end

Expand Down
Loading
Loading