Skip to content

Commit

Permalink
Consistently treat tinyint(1) fields in message database as booleans (#…
Browse files Browse the repository at this point in the history
…2380)

* Update mysql2 query call to cast booleans

* Treat messages:held field as boolean

* Treat messages:inspected field as boolean

* Treat messages:spam field as boolean

* Treat messages:threat field as boolean

* Treat messages:bounce field as boolean

* Treat messages:received_with_ssl field as boolean

* Treat deliveries:sent_with_ssl field as boolean
  • Loading branch information
catphish authored Mar 22, 2023
1 parent 87e9b3e commit 1e3622c
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion api/controllers/send_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
message.scope = "outgoing"
message.domain_id = authenticated_domain.id
message.credential_id = identity.id
message.bounce = params.bounce ? 1 : 0
message.bounce = params.bounce
message.save
result[:message_id] = message.message_id if result[:message_id].nil?
result[:messages][rcpt_to] = { id: message.id, token: message.token }
Expand Down
2 changes: 1 addition & 1 deletion api/structures/delivery_api_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
basic :status
basic :details
basic :output, value: proc { o.output&.strip }
basic :sent_with_ssl, value: proc { o.sent_with_ssl == 1 }
basic :sent_with_ssl, value: proc { o.sent_with_ssl }
basic :log_id
basic :time, value: proc { o.time&.to_f }
basic :timestamp, value: proc { o.timestamp.to_f }
Expand Down
8 changes: 4 additions & 4 deletions api/structures/message_api_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
status: o.status,
last_delivery_attempt: o.last_delivery_attempt ? o.last_delivery_attempt.to_f : nil,
held: o.held == 1,
held: o.held,
hold_expiry: o.hold_expiry ? o.hold_expiry.to_f : nil
}
end
Expand All @@ -29,10 +29,10 @@

expansion(:inspection) do
{
inspected: o.inspected == 1,
spam: o.spam == 1,
inspected: o.inspected,
spam: o.spam,
spam_score: o.spam_score.to_f,
threat: o.threat == 1,
threat: o.threat,
threat_details: o.threat_details
}
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def activity

def get_messages(scope)
if scope == "held"
options = { where: { held: 1 } }
options = { where: { held: true } }
else
options = { where: { scope: scope, spam: false }, order: :timestamp, direction: "desc" }

Expand Down
24 changes: 12 additions & 12 deletions app/jobs/unqueue_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def perform
#
# If this is a bounce, we need to handle it as such
#
if queued_message.message.bounce == 1
if queued_message.message.bounce
log "#{log_prefix} Message is a bounce"
original_messages = queued_message.message.original_messages
unless original_messages.empty?
Expand Down Expand Up @@ -120,17 +120,17 @@ def perform
#
# Inspect incoming messages
#
if queued_message.message.inspected == 0
unless queued_message.message.inspected
log "#{log_prefix} Inspecting message"
queued_message.message.inspect_message
if queued_message.message.inspected == 1
if queued_message.message.inspected
is_spam = queued_message.message.spam_score > queued_message.server.spam_threshold
queued_message.message.update(spam: 1) if is_spam
queued_message.message.update(spam: true) if is_spam
queued_message.message.append_headers(
"X-Postal-Spam: #{queued_message.message.spam == 1 ? 'yes' : 'no'}",
"X-Postal-Spam: #{queued_message.message.spam ? 'yes' : 'no'}",
"X-Postal-Spam-Threshold: #{queued_message.server.spam_threshold}",
"X-Postal-Spam-Score: #{queued_message.message.spam_score}",
"X-Postal-Threat: #{queued_message.message.threat == 1 ? 'yes' : 'no'}"
"X-Postal-Threat: #{queued_message.message.threat ? 'yes' : 'no'}"
)
log "#{log_prefix} Message inspected successfully. Headers added."
end
Expand Down Expand Up @@ -162,15 +162,15 @@ def perform
if route = queued_message.message.route

# If the route says we're holding quananteed mail and this is spam, we'll hold this
if route.spam_mode == "Quarantine" && queued_message.message.spam == 1 && !queued_message.manual?
if route.spam_mode == "Quarantine" && queued_message.message.spam && !queued_message.manual?
queued_message.message.create_delivery("Held", details: "Message placed into quarantine.")
queued_message.destroy
log "#{log_prefix} Route says to quarantine spam message. Holding."
next
end

# If the route says we're holding quananteed mail and this is spam, we'll hold this
if route.spam_mode == "Fail" && queued_message.message.spam == 1 && !queued_message.manual?
if route.spam_mode == "Fail" && queued_message.message.spam && !queued_message.manual?
queued_message.message.create_delivery("HardFail", details: "Message is spam and the route specifies it should be failed.")
queued_message.destroy
log "#{log_prefix} Route says to fail spam message. Hard failing."
Expand Down Expand Up @@ -325,18 +325,18 @@ def perform
end

# Inspect outgoing messages when there's a threshold set for the server
if queued_message.message.inspected == 0 && queued_message.server.outbound_spam_threshold
if !queued_message.message.inspected && queued_message.server.outbound_spam_threshold
log "#{log_prefix} Inspecting message"
queued_message.message.inspect_message
if queued_message.message.inspected == 1
if queued_message.message.inspected
if queued_message.message.spam_score >= queued_message.server.outbound_spam_threshold
queued_message.message.update(spam: 1)
queued_message.message.update(spam: true)
end
log "#{log_prefix} Message inspected successfully"
end
end

if queued_message.message.spam == 1
if queued_message.message.spam
queued_message.message.create_delivery("HardFail", details: "Message is likely spam. Threshold is #{queued_message.server.outbound_spam_threshold} and the message scored #{queued_message.message.spam_score}.")
queued_message.destroy
log "#{log_prefix} Message is spam (#{queued_message.message.spam_score}). Hard failing."
Expand Down
2 changes: 1 addition & 1 deletion app/models/outgoing_message_prototype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def create_message(address)
message.tag = tag
message.credential_id = credential&.id
message.received_with_ssl = true
message.bounce = @bounce ? 1 : 0
message.bounce = @bounce
message.save
{ id: message.id, token: message.token }
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def message_rate
end

def held_messages
@held_messages ||= message_db.messages(where: { held: 1 }, count: true)
@held_messages ||= message_db.messages(where: { held: true }, count: true)
end

def throughput_stats
Expand Down
2 changes: 1 addition & 1 deletion app/views/messages/_deliveries.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
.deliveryList__time
= delivery.timestamp.to_s(:long)
.deliveryList__status
- if delivery.sent_with_ssl == 1
- if delivery.sent_with_ssl
= image_tag 'icons/lock.svg', :class => 'deliveryList__secure'
%span.label.label--large{:class => "label--messageStatus-#{delivery.status.underscore}"}= delivery.status.underscore.humanize
- if delivery.details
Expand Down
2 changes: 1 addition & 1 deletion app/views/messages/activity.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
%p.messageActivity__extra
- if @message.credential
Received using the #{@message.credential.name} #{@message.credential.type} credential.
- if @message.received_with_ssl == 1
- if @message.received_with_ssl
Connection secured with SSL.
6 changes: 3 additions & 3 deletions app/views/messages/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
= link_to @message.domain.name, [organization, @server, :domains], :class => "u-link"
- else
Unknown Domain
- if @message.threat == 1
- if @message.threat
%dl.messagePropertiesPage__property
%dt Threat
%dd= @message.threat_details
Expand All @@ -66,8 +66,8 @@
- unless @message.received_with_ssl.nil?
%dl.messagePropertiesPage__property
%dt Transport Security
- if @message.received_with_ssl == 1
%dd.messagePropertiesPage__property--locked Received over a SSL connection
- if @message.received_with_ssl
%dd.messagePropertiesPage__property--locked Received over an SSL connection
- else
%dd Not received with SSL

Expand Down
2 changes: 1 addition & 1 deletion lib/postal/bounce_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def queue
message.mail_from = @message.route.description
message.domain_id = @message.domain&.id
message.raw_message = raw_message
message.bounce = 1
message.bounce = true
message.bounce_for_id = @message.id
message.save
message.id
Expand Down
4 changes: 2 additions & 2 deletions lib/postal/http_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def parameters(message, options = {})
timestamp: message.timestamp.to_f,
size: message.size,
spam_status: message.spam_status,
bounce: message.bounce == 1,
received_with_ssl: message.received_with_ssl == 1,
bounce: message.bounce,
received_with_ssl: message.received_with_ssl,
to: message.headers["to"]&.last,
cc: message.headers["cc"]&.last,
from: message.headers["from"]&.last,
Expand Down
2 changes: 1 addition & 1 deletion lib/postal/message_db/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def query(query)

def query_on_connection(connection, query)
start_time = Time.now.to_f
result = connection.query(query)
result = connection.query(query, cast_booleans: true)
time = Time.now.to_f - start_time
logger.debug " \e[4;34mMessageDB Query (#{time.round(2)}s) \e[0m \e[33m#{query}\e[0m"
if time > 0.5 && query =~ /\A(SELECT|UPDATE|DELETE) /
Expand Down
12 changes: 6 additions & 6 deletions lib/postal/message_db/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def read?
def create_delivery(status, options = {})
delivery = Delivery.create(self, options.merge(status: status))
hold_expiry = status == "Held" ? Postal.config.general.maximum_hold_expiry_days.days.from_now.to_f : nil
update(status: status, last_delivery_attempt: delivery.timestamp.to_f, held: status == "Held" ? 1 : 0, hold_expiry: hold_expiry)
update(status: status, last_delivery_attempt: delivery.timestamp.to_f, held: status == "Held", hold_expiry: hold_expiry)
delivery
end

Expand Down Expand Up @@ -371,9 +371,9 @@ def queued_message
# Return the spam status
#
def spam_status
return "NotChecked" unless inspected == 1
return "NotChecked" unless inspected

spam == 1 ? "Spam" : "NotSpam"
spam ? "Spam" : "NotSpam"
end

#
Expand Down Expand Up @@ -445,7 +445,7 @@ def bounce!(bounce_message)
# Should bounces be sent for this message?
#
def send_bounces?
bounce != 1 && mail_from.present?
!bounce && mail_from.present?
end

#
Expand All @@ -471,7 +471,7 @@ def create_link(url)
#  Return a message object that this message is a reply to
#
def original_messages
return nil unless bounce == 1
return nil unless bounce

other_message_ids = raw_message.scan(/\X-Postal-MsgID:\s*([a-z0-9]+)/i).flatten
if other_message_ids.empty?
Expand All @@ -495,7 +495,7 @@ def inspect_message
result = MessageInspection.scan(self, scope&.to_sym)

# Update the messages table with the results of our inspection
update(inspected: 1, spam_score: result.spam_score, threat: result.threat?, threat_details: result.threat_message)
update(inspected: true, spam_score: result.spam_score, threat: result.threat, threat_details: result.threat_message)

# Add any spam details into the spam checks database
database.insert_multi(:spam_checks, [:message_id, :code, :score, :description], result.spam_checks.map { |d| [id, d.code, d.score, d.description] })
Expand Down
4 changes: 0 additions & 4 deletions lib/postal/message_inspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def spam_score
@spam_checks.sum(&:score)
end

def threat?
@threat == true
end

def scan
MessageInspector.inspectors.each do |inspector|
inspector.inspect_message(self)
Expand Down
2 changes: 1 addition & 1 deletion lib/postal/smtp_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def send_message(message, force_rcpt_to = nil)
end

begin
if message.bounce == 1
if message.bounce
mail_from = ""
elsif message.domain.return_path_status == "OK"
mail_from = "#{message.server.token}@#{message.domain.return_path_domain}"
Expand Down
2 changes: 1 addition & 1 deletion script/insert-bounce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
message.rcpt_to = "#{server.token}@#{Postal.config.dns.return_path}"
message.mail_from = "[email protected]"
message.raw_message = template
message.bounce = 1
message.bounce = true
message.save
puts "Added message with id #{message.id}"

0 comments on commit 1e3622c

Please sign in to comment.