Skip to content

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

DEV: Stop mutating `allowed_iframes` for the discobot certificate (#39936)

David Taylor80b7a438d72c
diff --git a/frontend/discourse-markdown-it/src/options.js b/frontend/discourse-markdown-it/src/options.js
index 68ef452bd8b..329c0aa1057 100644
--- a/frontend/discourse-markdown-it/src/options.js
+++ b/frontend/discourse-markdown-it/src/options.js
@@ -33,6 +33,7 @@ export default function buildOptions(state) {
hashtagTypesInPriorityOrder,
hashtagIcons,
hashtagLookup,
+ allowedIframes,
} = state;
let features = {};
@@ -65,11 +66,14 @@ export default function buildOptions(state) {
allowedHrefSchemes: siteSettings.allowed_href_schemes
? siteSettings.allowed_href_schemes.split("|")
: null,
- allowedIframes: siteSettings.allowed_iframes
- ? siteSettings.allowed_iframes
- .split("|")
- .filter((str) => (str.match(/\//g) || []).length >= 3)
- : [], // Only allow urls with at least 3 slashes. Ex: 'https://example.com/'.
+ // Server-side `pretty_text.rb` passes the modifier-applied list as `allowedIframes`;
+ // client-side composer preview falls back to deriving it from `siteSettings`.
+ // Only allow urls with at least 3 slashes. Ex: 'https://example.com/'.
+ allowedIframes: (
+ allowedIframes ??
+ siteSettings.allowed_iframes?.split("|") ??
+ []
+ ).filter((str) => (str.match(/\//g) || []).length >= 3),
markdownIt: true,
previewing,
disableEmojis,
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 35355c7649b..d9ce78619b1 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -200,12 +200,19 @@ module PrettyText
custom_emoji = {}
Emoji.custom.map { |e| custom_emoji[e.name] = e.url }
+ allowed_iframes =
+ DiscoursePluginRegistry.apply_modifier(
+ :pretty_text_allowed_iframes,
+ SiteSetting.allowed_iframes.split("|"),
+ )
+
# note, any additional options added to __optInput here must be
# also be added to the buildOptions function in pretty-text.js,
# otherwise they will be discarded
buffer = +<<~JS
__optInput = {};
__optInput.siteSettings = #{SiteSetting.client_settings_json};
+ __optInput.allowedIframes = #{allowed_iframes.to_json};
#{"__optInput.disableEmojis = true" if opts[:disable_emojis]}
__paths = #{paths_json};
__optInput.getURL = __getURL;
diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index e689893eaec..1fbabd3bb55 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -39,15 +39,14 @@ after_initialize do
if SiteSetting.discourse_narrative_bot_enabled
# Disable welcome message because that is what the bot is supposed to replace.
SiteSetting.send_welcome_message = false if SiteSetting.send_welcome_message
-
- certificate_path = "#{Discourse.base_url}/discobot/certificate.svg"
- if !SiteSetting.allowed_iframes.include?(certificate_path)
- SiteSetting.allowed_iframes =
- SiteSetting.allowed_iframes.split("|").append(certificate_path).join("|")
- end
end
end
+ register_modifier(:pretty_text_allowed_iframes) do |list|
+ certificate_path = "#{Discourse.base_url}/discobot/certificate.svg"
+ list.include?(certificate_path) ? list : list + [certificate_path]
+ end
+
self.add_model_callback(User, :after_destroy) { DiscourseNarrativeBot::Store.remove(self.id) }
self.on(:user_created) do |user|