Render link previews only from web URLs

A link preview's link and image come from attributes on the message body,
which the composer fills in from the unfurl the server performed. A body
written by hand can put anything in those attributes, and the preview
partial rendered them as they were.

Keep the link and the image only when they parse as absolute http or https
URLs, so nothing in a message body can aim either one at another scheme or
at a path on this Campfire, and render the title and the description as
text.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186eyzivcTn6wqjEE4Wnxdt
This commit is contained in:
Rosa Gutierrez
2026-09-11 18:58:04 +02:00
parent ef147d17db
commit 3a501cd32c
4 changed files with 115 additions and 4 deletions
+12 -2
View File
@@ -16,12 +16,22 @@ class ActionText::Attachment::OpengraphEmbed
private
def attributes_from_node(node)
{
href: node["href"],
url: node["url"],
href: web_url(node["href"]),
url: web_url(node["url"]),
filename: node["filename"],
description: node["caption"]
}
end
# A link preview points at what we unfurled, which is always an absolute
# http or https URL. Drop anything else the message body asks for, so a
# body written by hand can't aim the preview's link or its image at another
# scheme or at a path on this Campfire.
def web_url(value)
value if value.present? && URI.parse(value).is_a?(URI::HTTP)
rescue URI::InvalidURIError
nil
end
end
attr_accessor :href, :url, :filename, :description