jsonld_helper.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # frozen_string_literal: true
  2. module JsonLdHelper
  3. include ContextHelper
  4. def equals_or_includes?(haystack, needle)
  5. haystack.is_a?(Array) ? haystack.include?(needle) : haystack == needle
  6. end
  7. def equals_or_includes_any?(haystack, needles)
  8. needles.any? { |needle| equals_or_includes?(haystack, needle) }
  9. end
  10. def first_of_value(value)
  11. value.is_a?(Array) ? value.first : value
  12. end
  13. # The url attribute can be a string, an array of strings, or an array of objects.
  14. # The objects could include a mimeType. Not-included mimeType means it's text/html.
  15. def url_to_href(value, preferred_type = nil)
  16. single_value = begin
  17. if value.is_a?(Array) && !value.first.is_a?(String)
  18. value.find { |link| preferred_type.nil? || ((link['mimeType'].presence || 'text/html') == preferred_type) }
  19. elsif value.is_a?(Array)
  20. value.first
  21. else
  22. value
  23. end
  24. end
  25. if single_value.nil? || single_value.is_a?(String)
  26. single_value
  27. else
  28. single_value['href']
  29. end
  30. end
  31. def as_array(value)
  32. value.is_a?(Array) ? value : [value]
  33. end
  34. def value_or_id(value)
  35. value.is_a?(String) || value.nil? ? value : value['id']
  36. end
  37. def supported_context?(json)
  38. !json.nil? && equals_or_includes?(json['@context'], ActivityPub::TagManager::CONTEXT)
  39. end
  40. def unsupported_uri_scheme?(uri)
  41. !uri.start_with?('http://', 'https://')
  42. end
  43. def invalid_origin?(url)
  44. return true if unsupported_uri_scheme?(url)
  45. needle = Addressable::URI.parse(url).host
  46. haystack = Addressable::URI.parse(@account.uri).host
  47. !haystack.casecmp(needle).zero?
  48. end
  49. def canonicalize(json)
  50. graph = RDF::Graph.new << JSON::LD::API.toRdf(json, documentLoader: method(:load_jsonld_context))
  51. graph.dump(:normalize)
  52. end
  53. def compact(json)
  54. compacted = JSON::LD::API.compact(json.without('signature'), full_context, documentLoader: method(:load_jsonld_context))
  55. compacted['signature'] = json['signature']
  56. compacted
  57. end
  58. # Patches a JSON-LD document to avoid compatibility issues on redistribution
  59. #
  60. # Since compacting a JSON-LD document against Mastodon's built-in vocabulary
  61. # means other extension namespaces will be expanded, malformed JSON-LD
  62. # attributes lost, and some values “unexpectedly” compacted this method
  63. # patches the following likely sources of incompatibility:
  64. # - 'https://www.w3.org/ns/activitystreams#Public' being compacted to
  65. # 'as:Public' (for instance, pre-3.4.0 Mastodon does not understand
  66. # 'as:Public')
  67. # - single-item arrays being compacted to the item itself (`[foo]` being
  68. # compacted to `foo`)
  69. #
  70. # It is not always possible for `patch_for_forwarding!` to produce a document
  71. # deemed safe for forwarding. Use `safe_for_forwarding?` to check the status
  72. # of the output document.
  73. #
  74. # @param original [Hash] The original JSON-LD document used as reference
  75. # @param compacted [Hash] The compacted JSON-LD document to be patched
  76. # @return [void]
  77. def patch_for_forwarding!(original, compacted)
  78. original.without('@context', 'signature').each do |key, value|
  79. next if value.nil? || !compacted.key?(key)
  80. compacted_value = compacted[key]
  81. if value.is_a?(Hash) && compacted_value.is_a?(Hash)
  82. patch_for_forwarding!(value, compacted_value)
  83. elsif value.is_a?(Array)
  84. compacted_value = [compacted_value] unless compacted_value.is_a?(Array)
  85. return if value.size != compacted_value.size
  86. compacted[key] = value.zip(compacted_value).map do |v, vc|
  87. if v.is_a?(Hash) && vc.is_a?(Hash)
  88. patch_for_forwarding!(v, vc)
  89. vc
  90. elsif v == 'https://www.w3.org/ns/activitystreams#Public' && vc == 'as:Public'
  91. v
  92. else
  93. vc
  94. end
  95. end
  96. elsif value == 'https://www.w3.org/ns/activitystreams#Public' && compacted_value == 'as:Public'
  97. compacted[key] = value
  98. end
  99. end
  100. end
  101. # Tests whether a JSON-LD compaction is deemed safe for redistribution,
  102. # that is, if it doesn't change its meaning to consumers that do not actually
  103. # handle JSON-LD, but rely on values being serialized in a certain way.
  104. #
  105. # See `patch_for_forwarding!` for details.
  106. #
  107. # @param original [Hash] The original JSON-LD document used as reference
  108. # @param compacted [Hash] The compacted JSON-LD document to be patched
  109. # @return [Boolean] Whether the patched document is deemed safe
  110. def safe_for_forwarding?(original, compacted)
  111. original.without('@context', 'signature').all? do |key, value|
  112. compacted_value = compacted[key]
  113. return false unless value.class == compacted_value.class
  114. if value.is_a?(Hash)
  115. safe_for_forwarding?(value, compacted_value)
  116. elsif value.is_a?(Array)
  117. value.zip(compacted_value).all? do |v, vc|
  118. v.is_a?(Hash) ? (vc.is_a?(Hash) && safe_for_forwarding?(v, vc)) : v == vc
  119. end
  120. else
  121. value == compacted_value
  122. end
  123. end
  124. end
  125. def fetch_resource(uri, id, on_behalf_of = nil)
  126. unless id
  127. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  128. return if !json.is_a?(Hash) || unsupported_uri_scheme?(json['id'])
  129. uri = json['id']
  130. end
  131. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  132. json.present? && json['id'] == uri ? json : nil
  133. end
  134. def fetch_resource_without_id_validation(uri, on_behalf_of = nil, raise_on_temporary_error = false)
  135. on_behalf_of ||= Account.representative
  136. build_request(uri, on_behalf_of).perform do |response|
  137. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  138. body_to_json(response.body_with_limit) if response.code == 200
  139. end
  140. end
  141. def body_to_json(body, compare_id: nil)
  142. json = body.is_a?(String) ? Oj.load(body, mode: :strict) : body
  143. return if compare_id.present? && json['id'] != compare_id
  144. json
  145. rescue Oj::ParseError
  146. nil
  147. end
  148. def merge_context(context, new_context)
  149. if context.is_a?(Array)
  150. context << new_context
  151. else
  152. [context, new_context]
  153. end
  154. end
  155. def response_successful?(response)
  156. (200...300).cover?(response.code)
  157. end
  158. def response_error_unsalvageable?(response)
  159. response.code == 501 || ((400...500).cover?(response.code) && ![401, 408, 429].include?(response.code))
  160. end
  161. def build_request(uri, on_behalf_of = nil)
  162. Request.new(:get, uri).tap do |request|
  163. request.on_behalf_of(on_behalf_of) if on_behalf_of
  164. request.add_headers('Accept' => 'application/activity+json, application/ld+json')
  165. end
  166. end
  167. def load_jsonld_context(url, _options = {}, &_block)
  168. json = Rails.cache.fetch("jsonld:context:#{url}", expires_in: 30.days, raw: true) do
  169. request = Request.new(:get, url)
  170. request.add_headers('Accept' => 'application/ld+json')
  171. request.perform do |res|
  172. raise JSON::LD::JsonLdError::LoadingDocumentFailed unless res.code == 200 && res.mime_type == 'application/ld+json'
  173. res.body_with_limit
  174. end
  175. end
  176. doc = JSON::LD::API::RemoteDocument.new(json, documentUrl: url)
  177. block_given? ? yield(doc) : doc
  178. end
  179. end