Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def show
HTML

response.headers.delete("X-Frame-Options")
response.headers.delete("Content-Security-Policy")
response.headers["Content-Security-Policy"] = "script-src 'unsafe-inline';"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using the csp_nonce_placeholder helper work as well? (adding it to the script tag in the html heredoc a few lines above - I think nonce=#{view_context.csp_nonce_placeholder} should work)


# Render the content
render html: html.html_safe, layout: false, content_type: "text/html"
Expand Down
72 changes: 72 additions & 0 deletions spec/requests/ai_bot/artifacts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

RSpec.describe DiscourseAi::AiBot::ArtifactsController do
fab!(:user)
fab!(:topic) { Fabricate(:private_message_topic, user: user) }
fab!(:post) { Fabricate(:post, user: user, topic: topic) }
fab!(:artifact) do
AiArtifact.create!(
user: user,
post: post,
name: "Test Artifact",
html: "<div>Hello World</div>",
css: "div { color: blue; }",
js: "console.log('test');",
metadata: {
public: false,
},
)
end

before do
SiteSetting.discourse_ai_enabled = true
SiteSetting.ai_artifact_security = "strict"
end

describe "#show" do
it "returns 404 when discourse_ai is disabled" do
SiteSetting.discourse_ai_enabled = false
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.status).to eq(404)
end

it "returns 404 when ai_artifact_security disables it" do
SiteSetting.ai_artifact_security = "disabled"
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.status).to eq(404)
end

context "with private artifact" do
it "returns 404 when user cannot see the post" do
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.status).to eq(404)
end

it "shows artifact when user can see the post" do
sign_in(user)
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.status).to eq(200)
expect(response.body).to include(artifact.html)
expect(response.body).to include(artifact.css)
expect(response.body).to include(artifact.js)
end
end

context "with public artifact" do
before { artifact.update!(metadata: { public: true }) }

it "shows artifact without authentication" do
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.status).to eq(200)
expect(response.body).to include(artifact.html)
end
end

it "removes security headers" do
sign_in(user)
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.headers["X-Frame-Options"]).to eq(nil)
expect(response.headers["Content-Security-Policy"]).to eq("script-src 'unsafe-inline';")
end
end
end