Skip to content

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

FEATURE: supports header auth cred for http request (#40971)

Joffrey JAFFEUX2890df7f2342
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 1e5240e582d..f618f5456f8 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -611,6 +611,7 @@ en:
authentication_none: "None"
authentication_basic_auth: "Basic auth"
authentication_bearer_token: "Bearer token"
+ authentication_header_auth: "Header auth"
credential: "Credential"
method: "Method"
url: "URL"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 89aa45232f6..404012b59a2 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -74,6 +74,7 @@ en:
host_required: "URL must include a host."
credential_required: "Credential is required for authentication mode '%{mode}'."
exec_ctx_required: "Execution context is required for authentication mode '%{mode}'."
+ header_name_invalid: "Header name contains invalid characters."
data_table:
unknown_column: "Unknown column name '%{column}'."
unknown_operation: "Unknown operation: %{operation}."
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/authenticator.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/authenticator.rb
index f0f5eb19ff1..33e11964663 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/authenticator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/authenticator.rb
@@ -6,6 +6,9 @@ module DiscourseWorkflows
class Authenticator
extend NodeErrorHandling
+ # RFC 7230 header field-name
+ HEADER_NAME_PATTERN = /\A[A-Za-z0-9!#$%&'*+.^_`|~-]+\z/
+
def self.apply(config, headers, exec_ctx, item_index: 0)
auth_mode = config.fetch("authentication") { "none" }
return if auth_mode == "none"
@@ -17,6 +20,8 @@ module DiscourseWorkflows
apply_basic_auth(headers, cred_data)
when "bearer_token"
apply_bearer_token(headers, cred_data)
+ when "header_auth"
+ apply_header_auth(headers, cred_data)
end
end
@@ -40,7 +45,18 @@ module DiscourseWorkflows
headers["Authorization"] = "Bearer #{token}"
end
- private_class_method :fetch_credential_data, :apply_basic_auth, :apply_bearer_token
+ def self.apply_header_auth(headers, cred_data)
+ name = cred_data["name"].to_s
+ unless name.match?(HEADER_NAME_PATTERN)
+ raise_node_error!(I18n.t("discourse_workflows.errors.http_request.header_name_invalid"))
+ end
+ headers[name] = cred_data["value"]
+ end
+
+ private_class_method :fetch_credential_data,
+ :apply_basic_auth,
+ :apply_bearer_token,
+ :apply_header_auth
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/v1.rb
index cc50353afd1..cd29bc70a6b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/http_request/v1.rb
@@ -32,7 +32,7 @@ module DiscourseWorkflows
},
authentication: {
type: :options,
- options: %w[none basic_auth bearer_token],
+ options: %w[none basic_auth bearer_token header_auth],
default: "none",
no_data_expression: true,
},
@@ -173,11 +173,11 @@ module DiscourseWorkflows
credentials: [
{
name: "auth",
- credential_types: %w[basic_auth bearer_token],
+ credential_types: %w[basic_auth bearer_token header_auth],
required: false,
display_options: {
show: {
- authentication: %w[basic_auth bearer_token],
+ authentication: %w[basic_auth bearer_token header_auth],
},
},
label_key: "discourse_workflows.http_request.credential",
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request/authenticator_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request/authenticator_spec.rb
index 11d4edc083e..6bfc0ae529e 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request/authenticator_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request/authenticator_spec.rb
@@ -63,5 +63,70 @@ RSpec.describe DiscourseWorkflows::Nodes::HttpRequest::Authenticator do
expect(headers["Authorization"]).to eq("Bearer my-secret-token")
end
end
+
+ context "with header_auth" do
+ it "sets the custom header from the credential" do
+ headers = {}
+ exec_ctx =
+ instance_double(
+ DiscourseWorkflows::Executor::NodeExecutionContext,
+ get_credentials: {
+ "name" => "X-API-Key",
+ "value" => "my-secret-key",
+ },
+ )
+
+ described_class.apply({ "authentication" => "header_auth" }, headers, exec_ctx)
+
+ expect(headers["X-API-Key"]).to eq("my-secret-key")
+ end
+
+ it "raises when the resolved header name is blank" do
+ exec_ctx =
+ instance_double(
+ DiscourseWorkflows::Executor::NodeExecutionContext,
+ get_credentials: {
+ "name" => "",
+ "value" => "my-secret-key",
+ },
+ )
+
+ expect {
+ described_class.apply({ "authentication" => "header_auth" }, {}, exec_ctx)
+ }.to raise_error(DiscourseWorkflows::NodeError, /Header name contains invalid characters/)
+ end
+
+ it "raises when the resolved header name injects CRLF" do
+ headers = {}
+ exec_ctx =
+ instance_double(
+ DiscourseWorkflows::Executor::NodeExecutionContext,
+ get_credentials: {
+ "name" => "X-Foo\r\nX-Injected: evil",
+ "value" => "my-secret-key",
+ },
+ )
+
+ expect {
+ described_class.apply({ "authentication" => "header_auth" }, headers, exec_ctx)
+ }.to raise_error(DiscourseWorkflows::NodeError, /Header name contains invalid characters/)
+ expect(headers).to be_empty
+ end
+
+ it "raises when the resolved header name contains other illegal characters" do
+ exec_ctx =
+ instance_double(
+ DiscourseWorkflows::Executor::NodeExecutionContext,
+ get_credentials: {
+ "name" => "X Api Key",
+ "value" => "my-secret-key",
+ },
+ )
+
+ expect {
+ described_class.apply({ "authentication" => "header_auth" }, {}, exec_ctx)
+ }.to raise_error(DiscourseWorkflows::NodeError, /Header name contains invalid characters/)
+ end
+ end
end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb
index fda739bfdd4..cd587905afc 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb
@@ -454,6 +454,95 @@ RSpec.describe DiscourseWorkflows::Nodes::HttpRequest::V1 do
end
end
+ context "with header_auth authentication" do
+ fab!(:credential) do
+ Fabricate(
+ :discourse_workflows_credential,
+ credential_type: "header_auth",
+ data: {
+ "name" => "X-API-Key",
+ "value" => "my-secret-key",
+ },
+ )
+ end
+
+ it "injects the custom header" do
+ stub_request(:get, "https://api.example.com/data").with(
+ headers: {
+ "X-API-Key" => "my-secret-key",
+ },
+ ).to_return(
+ status: 200,
+ body: { ok: true }.to_json,
+ headers: {
+ "content-type" => "application/json",
+ },
+ )
+
+ config = {
+ "method" => "GET",
+ "url" => "https://api.example.com/data",
+ "authentication" => "header_auth",
+ "credentials" => {
+ "auth" => {
+ "id" => credential.id,
+ "credential_type" => "header_auth",
+ },
+ },
+ }
+
+ result = execute_node(configuration: config, item: item)
+ expect(result).to eq("ok" => true)
+ end
+
+ it "resolves credential expressions against each input item" do
+ credential.update!(data: { "name" => "X-API-Key", "value" => "={{ $json.api_key }}" })
+
+ stub_request(:get, "https://api.example.com/data").with(
+ headers: {
+ "X-API-Key" => "key-alice",
+ },
+ ).to_return(
+ status: 200,
+ body: { user: "alice" }.to_json,
+ headers: {
+ "content-type" => "application/json",
+ },
+ )
+ stub_request(:get, "https://api.example.com/data").with(
+ headers: {
+ "X-API-Key" => "key-bob",
+ },
+ ).to_return(
+ status: 200,
+ body: { user: "bob" }.to_json,
+ headers: {
+ "content-type" => "application/json",
+ },
+ )
+
+ config = {
+ "method" => "GET",
+ "url" => "https://api.example.com/data",
+ "authentication" => "header_auth",
+ "credentials" => {
+ "auth" => {
+ "id" => credential.id,
+ "credential_type" => "header_auth",
+ },
+ },
+ }
+ input_items = [
+ { "json" => { "api_key" => "key-alice" } },
+ { "json" => { "api_key" => "key-bob" } },
+ ]
+
+ result = execute_node_output(configuration: config, input_items: input_items).first
+
+ expect(result.map { |output_item| output_item["json"]["user"] }).to eq(%w[alice bob])
+ end
+ end
+
context "with authentication set to none" do
it "does not add Authorization header" do
stub_request(:get, "https://api.example.com/data").to_return(
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb
index 8a260916091..9aafc954730 100644
--- a/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb
@@ -209,11 +209,11 @@ RSpec.describe DiscourseWorkflows::NodeType::List do
expect(http_request[:credentials]).to contain_exactly(
a_hash_including(
name: "auth",
- credential_types: %w[basic_auth bearer_token],
+ credential_types: %w[basic_auth bearer_token header_auth],
required: false,
display_options: {
show: {
- authentication: %w[basic_auth bearer_token],
+ authentication: %w[basic_auth bearer_token header_auth],
},
},
),