FIX: Prevent invalid tos and privacy URLs in cache (#24291)

Followup to 5fc1586abf

There are certain cases where the tos_url and privacy_policy_url
can end up with a "nil" value in the Discourse.urls_cache.

The cause of this is unclear, but it seems to behave differently
between doing this caching in the rails console and the running
server.

To avoid this we can just not store anything that looks like nil
in the cache; we can delete the cache keys entirely if we don't
need them anymore.
This commit is contained in:
Martin Brennan 2023-11-09 10:33:38 +10:00 committed by GitHub
parent be2eb3df44
commit fe5383dbff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -606,15 +606,18 @@ module Discourse
if SiteSetting.tos_url.present? if SiteSetting.tos_url.present?
SiteSetting.tos_url SiteSetting.tos_url
else else
urls_cache["tos"] ||= ( return urls_cache["tos"] if urls_cache["tos"].present?
tos_url =
if SiteSetting.tos_topic_id > 0 && Topic.exists?(id: SiteSetting.tos_topic_id) if SiteSetting.tos_topic_id > 0 && Topic.exists?(id: SiteSetting.tos_topic_id)
"#{Discourse.base_path}/tos" "#{Discourse.base_path}/tos"
else
:nil
end end
)
urls_cache["tos"] != :nil ? urls_cache["tos"] : nil if tos_url
urls_cache["tos"] = tos_url
else
urls_cache.delete("tos")
end
end end
end end
@ -622,15 +625,18 @@ module Discourse
if SiteSetting.privacy_policy_url.present? if SiteSetting.privacy_policy_url.present?
SiteSetting.privacy_policy_url SiteSetting.privacy_policy_url
else else
urls_cache["privacy_policy"] ||= ( return urls_cache["privacy_policy"] if urls_cache["privacy_policy"].present?
privacy_policy_url =
if SiteSetting.privacy_topic_id > 0 && Topic.exists?(id: SiteSetting.privacy_topic_id) if SiteSetting.privacy_topic_id > 0 && Topic.exists?(id: SiteSetting.privacy_topic_id)
"#{Discourse.base_path}/privacy" "#{Discourse.base_path}/privacy"
else
:nil
end end
)
urls_cache["privacy_policy"] != :nil ? urls_cache["privacy_policy"] : nil if privacy_policy_url
urls_cache["privacy_policy"] = privacy_policy_url
else
urls_cache.delete("privacy_policy")
end
end end
end end