From 4f55751500d118f8296b99c6c655a2d4088ba261 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Thu, 26 Feb 2026 20:30:20 -0500 Subject: [PATCH] Refactor QuickBooks webhook handling to use ActiveJob for processing; improve security with signature verification and streamline entity processing --- app/controllers/qbo_controller.rb | 91 ++++++++++--------------------- app/jobs/webhook_process_job.rb | 59 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 62 deletions(-) create mode 100644 app/jobs/webhook_process_job.rb diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index a65ce09..865bac5 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -86,68 +86,6 @@ class QboController < ApplicationController notice: I18n.t(:label_billing_enqueued) + " #{issue.customer.name}" } end - - # Quickbooks Webhook Callback - def webhook - - logger.info "Quickbooks is calling webhook" - - # check the payload - signature = request.headers['intuit-signature'] - key = Setting.plugin_redmine_qbo['settingsWebhookToken'] - data = request.body.read - hash = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), key, data)).strip() - - # proceed if the request is good - if hash.eql? signature - Thread.new do - if request.headers['content-type'] == 'application/json' - data = JSON.parse(data) - else - # application/x-www-form-urlencoded - data = params.as_json - end - # Process the information - entities = data['eventNotifications'][0]['dataChangeEvent']['entities'] - entities.each do |entity| - id = entity['id'].to_i - name = entity['name'] - - logger.info "Casting #{name.constantize} to obj" - - # Magicly initialize the correct class - obj = name.constantize - - # for merge events - obj.destroy(entity['deletedId']) if entity['deletedId'] - - #Check to see if we are deleting a record - if entity['operation'].eql? "Delete" - obj.destroy(id) - #if not then update! - else - begin - obj.sync_by_id(id) - rescue => e - logger.error "Failed to call sync_by_id on obj" - logger.error e.message - logger.error e.backtrace.join("\n") - end - end - end - - # Record that last time we updated - Qbo.update_time_stamp - ActiveRecord::Base.connection.close - end - # The webhook doesn't require a response but let's make sure we don't send anything - render nothing: true, status: 200 - else - render nothing: true, status: 400 - end - - logger.info "Quickbooks webhook complete" - end # # Synchronizes the QboCustomer table with QBO @@ -170,4 +108,33 @@ class QboController < ApplicationController redirect_to :home, flash: { notice: I18n.t(:label_syncing) } end + + # QuickBooks Webhook Callback + def webhook + logger.info "QBO: Webhook received" + + signature = request.headers['intuit-signature'] + key = Setting.plugin_redmine_qbo['settingsWebhookToken'] + body = request.raw_post + + digest = OpenSSL::Digest.new('sha256') + computed = Base64.strict_encode64(OpenSSL::HMAC.digest(digest, key, body)) + + unless secure_compare(computed, signature) + logger.warn "QBO: Invalid webhook signature" + head :unauthorized + return + end + + WebhookProcessJob.perform_later(body) + + head :ok + end + + private + + def secure_compare(a, b) + return false if a.blank? || b.blank? + ActiveSupport::SecurityUtils.secure_compare(a, b) + end end diff --git a/app/jobs/webhook_process_job.rb b/app/jobs/webhook_process_job.rb new file mode 100644 index 0000000..eb398d1 --- /dev/null +++ b/app/jobs/webhook_process_job.rb @@ -0,0 +1,59 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 - 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class WebhookProcessJob < ActiveJob::Base + queue_as :default + + ALLOWED_ENTITIES = %w[ + Customer + Invoice + Estimate + ].freeze + + def perform(raw_body) + data = JSON.parse(raw_body) + + data.fetch('eventNotifications', []).each do |notification| + entities = notification.dig('dataChangeEvent', 'entities') || [] + + entities.each do |entity| + process_entity(entity) + end + end + + Qbo.update_time_stamp + end + + private + + def process_entity(entity) + name = entity['name'] + id = entity['id']&.to_i + + return unless ALLOWED_ENTITIES.include?(name) + + model = name.safe_constantize + return unless model + + if entity['deletedId'] + model.destroy(entity['deletedId']) + return + end + + if entity['operation'] == "Delete" + model.destroy(id) + else + model.sync_by_id(id) + end + rescue => e + Rails.logger.error "QBO Webhook entity processing failed" + Rails.logger.error e.message + end +end \ No newline at end of file