Skip to content

A platform for community discussion. Free, open, simple.

FIX: Mask webhook header auth credentials in trigger data (#44560)

Régis Hanolab659f9cdebb
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/action/authenticate_node.rb b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/action/authenticate_node.rb
index 61900ab485c..852d3cea955 100644
--- a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/action/authenticate_node.rb
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/action/authenticate_node.rb
@@ -17,6 +17,7 @@ module DiscourseWorkflows
option :node
option :params
option :credentials, default: -> { {} }
+ option :request, optional: true
def call
auth_mode = NodeData.resolved_parameters(node)["authentication"] || NO_AUTH
@@ -94,14 +95,16 @@ module DiscourseWorkflows
def authenticate_header_auth(credential)
cred_data = credential.data || {}
- expected_name = ExpressionResolver.resolve(cred_data["name"])
+ header_key = ExpressionResolver.resolve(cred_data["name"]).to_s.downcase
expected_value = ExpressionResolver.resolve(cred_data["value"])
- return MISCONFIGURED if expected_name.blank? || expected_value.blank?
+ return MISCONFIGURED if header_key.blank? || expected_value.blank?
- header_key = expected_name.to_s.downcase
- provided_value = (params.headers[header_key] || params.headers[header_key.to_sym]).to_s
- secure_compare(provided_value, expected_value) ? AUTHENTICATED : DENIED
+ provided_value = params.headers[header_key] || params.headers[header_key.to_sym]
+ return DENIED if !secure_compare(provided_value, expected_value)
+
+ request&.filter_header(header_key)
+ AUTHENTICATED
end
def secure_compare(a, b)
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
index 631792b3f3c..4c115a92dc5 100644
--- a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
@@ -243,7 +243,7 @@ module DiscourseWorkflows
PublishedTrigger.new(workflow:, workflow_version:, trigger_node: node)
end
- def filter_authenticated_nodes(webhook_nodes:, params:)
+ def filter_authenticated_nodes(webhook_nodes:, params:, webhook_context:)
credentials = preload_auth_credentials(webhook_nodes)
authenticated = []
failure_reasons = []
@@ -254,6 +254,7 @@ module DiscourseWorkflows
node: published_trigger.trigger_node,
params:,
credentials:,
+ request: webhook_context.request,
)
if result == Webhook::Action::AuthenticateNode::AUTHENTICATED
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/webhook_request.rb b/plugins/discourse-workflows/lib/discourse_workflows/webhook_request.rb
index d0a67c9f13b..1bab5d5c94d 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/webhook_request.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/webhook_request.rb
@@ -2,6 +2,9 @@
module DiscourseWorkflows
class WebhookRequest
+ FILTERED_HEADERS = %w[authorization cookie proxy-authorization].to_set.freeze
+ FILTERED_VALUE = Nodes::HttpHelpers::FILTERED_QUERY_VALUE
+
attr_reader :method, :path, :headers, :params, :query, :body, :raw_body, :ip, :ips, :webhook_url
def initialize(
@@ -18,7 +21,10 @@ module DiscourseWorkflows
)
@method = method.to_s
@path = path.to_s
- @headers = headers.to_h.deep_stringify_keys
+ @headers =
+ headers.to_h.deep_stringify_keys.to_h do |name, value|
+ [name, FILTERED_HEADERS.include?(name) ? FILTERED_VALUE : value]
+ end
@params = params.to_h.deep_stringify_keys
@query = query.to_h.deep_stringify_keys
@body = body
@@ -28,6 +34,10 @@ module DiscourseWorkflows
@webhook_url = webhook_url.to_s
end
+ def filter_header(name)
+ headers[name] = FILTERED_VALUE if headers.key?(name)
+ end
+
def item_json
data = {
"body" => body,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/webhook_request_parser.rb b/plugins/discourse-workflows/lib/discourse_workflows/webhook_request_parser.rb
index 522e077d7a9..1cb4b8ed0e1 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/webhook_request_parser.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/webhook_request_parser.rb
@@ -5,8 +5,6 @@ module DiscourseWorkflows
MAX_BODY_SIZE = 1.megabyte
BODY_TOO_LARGE_ERROR = "Request body too large"
- FILTERED_HEADERS = %w[authorization cookie proxy-authorization].to_set.freeze
-
RACK_UNPREFIXED_HEADERS = {
"CONTENT_TYPE" => "content-type",
"CONTENT_LENGTH" => "content-length",
@@ -38,7 +36,7 @@ module DiscourseWorkflows
header_name = normalize_header_name(key)
next unless header_name
- headers[header_name] = FILTERED_HEADERS.include?(header_name) ? "[FILTERED]" : value.to_s
+ headers[header_name] = value.to_s
end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_parser_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_parser_spec.rb
index afae7ff46ec..3390f0254a2 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_parser_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_parser_spec.rb
@@ -114,13 +114,6 @@ RSpec.describe DiscourseWorkflows::WebhookRequestParser do
expect(headers["content-type"]).to eq("application/json")
end
- it "filters sensitive headers" do
- headers = parser.extract_headers
-
- expect(headers["authorization"]).to eq("[FILTERED]")
- expect(headers["cookie"]).to eq("[FILTERED]")
- end
-
it "skips non-HTTP rack variables" do
headers = parser.extract_headers
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_spec.rb
new file mode 100644
index 00000000000..5431bcab8c4
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/webhook_request_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::WebhookRequest do
+ subject(:request) do
+ described_class.new(
+ method: "POST",
+ path: "hook",
+ headers: {
+ "authorization" => "Bearer secret",
+ "cookie" => "session=1",
+ "x-api-key" => "key",
+ },
+ webhook_url: "http://test.localhost/workflows/webhooks/hook",
+ )
+ end
+
+ it "masks sensitive headers" do
+ expect(request.headers).to eq(
+ "authorization" => "[FILTERED]",
+ "cookie" => "[FILTERED]",
+ "x-api-key" => "key",
+ )
+ end
+
+ describe "#filter_header" do
+ it "masks the given header" do
+ request.filter_header("x-api-key")
+
+ expect(request.headers["x-api-key"]).to eq("[FILTERED]")
+ end
+
+ it "ignores absent headers" do
+ request.filter_header("x-missing")
+
+ expect(request.headers).not_to have_key("x-missing")
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/webhooks_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/webhooks_controller_spec.rb
index 0de1e122e69..fa3ea4ff10c 100644
--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/webhooks_controller_spec.rb
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/webhooks_controller_spec.rb
@@ -910,6 +910,35 @@ RSpec.describe DiscourseWorkflows::WebhooksController do
expect(response).to have_http_status(:ok)
end
+
+ it "masks the authentication header in the trigger data" do
+ post "/workflows/webhooks/my-hook.json",
+ params: {}.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ "HTTP_X_API_KEY" => "secret-value",
+ }
+
+ trigger_data =
+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.last["args"][0]["trigger_data"]
+ expect(trigger_data["headers"]).to include("x-api-key" => "[FILTERED]")
+ end
+
+ it "authenticates against a header the parser filters" do
+ credential.update!(data: { "name" => "Proxy-Authorization", "value" => "Token abc" })
+
+ post "/workflows/webhooks/my-hook.json",
+ params: {}.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ "HTTP_PROXY_AUTHORIZATION" => "Token abc",
+ }
+
+ expect(response).to have_http_status(:ok)
+ trigger_data =
+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.last["args"][0]["trigger_data"]
+ expect(trigger_data["headers"]).to include("proxy-authorization" => "[FILTERED]")
+ end
end
end
end
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/action/authenticate_node_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/action/authenticate_node_spec.rb
index ff5d8ac26cf..129304d8cea 100644
--- a/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/action/authenticate_node_spec.rb
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/action/authenticate_node_spec.rb
@@ -204,6 +204,21 @@ RSpec.describe DiscourseWorkflows::Webhook::Action::AuthenticateNode do
end
end
+ context "when the header is the authorization header" do
+ let(:headers) { { "authorization" => "Token abc" } }
+
+ before do
+ header_credential.update_column(
+ :data,
+ { "name" => "Authorization", "value" => "Token abc" },
+ )
+ end
+
+ it "returns :authenticated" do
+ expect(result).to eq(:authenticated)
+ end
+ end
+
context "when credential is missing name or value" do
before { header_credential.update_column(:data, { "name" => "X-Api-Key" }) }