Skip to content

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

SECURITY: Hidden first-post excerpt is emitted in Q&A schema JSON-LD

Chris Albertiab16304ccc20
diff --git a/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb b/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
index bf08f8b83c1..446a20b7a5b 100644
--- a/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
+++ b/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
@@ -29,6 +29,11 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
first_post.excerpt(nil, keep_onebox_body: true, keep_quotes: true)
end
+ def include_text?
+ first_post = object.first_post
+ first_post.present? && guardian.can_see_post?(first_post)
+ end
+
def upvoteCount
object.first_post.like_count
end
@@ -58,4 +63,8 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
def suggested_answers
options[:suggested_answers]
end
+
+ def guardian
+ scope || Guardian.new
+ end
end
diff --git a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
index f91c0e46023..a17cf3f3642 100644
--- a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
+++ b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
@@ -54,11 +54,12 @@ class DiscourseSolved::BuildSchemaMarkup
.select { |post| DiscourseSolved::SchemaUtils.eligible_answer?(post) }
end
- def fetch_html(topic:, accepted_answers:, suggested_answers:)
+ def fetch_html(topic:, guardian:, accepted_answers:, suggested_answers:)
question_json =
DiscourseSolved::QuestionSchemaSerializer.new(
topic,
root: false,
+ scope: guardian,
accepted_answers: accepted_answers,
suggested_answers: suggested_answers,
).serializable_hash
diff --git a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
index 42435270ae6..f8a3129c970 100644
--- a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
+++ b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
@@ -55,6 +55,25 @@ RSpec.describe TopicsController do
expect(response.body).to include(expected_schema_json(p2_single_answer_json))
end
+ it "does not include first post text in schema information when the guardian cannot see it" do
+ hidden_text = "super-secret first post body"
+ p1.update!(raw: hidden_text, hidden: true)
+ p1.rebake!
+ Fabricate(:solved_topic, topic:, answer_post: p2)
+
+ get "/t/#{topic.slug}/#{topic.id}"
+
+ schema_json =
+ Nokogiri::HTML5
+ .fragment(response.body)
+ .at_css('script[type="application/ld+json"]')
+ &.content
+
+ expect(response.status).to eq(200)
+ expect(schema_json).to include('"@type":"QAPage"')
+ expect(schema_json).not_to include(hidden_text)
+ end
+
it "should include quoted content in schema information" do
Fabricate(:solved_topic, topic:, answer_post: p2)