grover 1.2.10 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6975e57956621f04557b7b9634611de621cfbca81f067dec8f0f302adb861cdc
4
- data.tar.gz: 922fb8f8df08bbbe4870fa013e543ca332703bcf888308d894047689824a9ee5
3
+ metadata.gz: cb5b937d91e68448cb7d33b190207dca1312046dbf29370814923bbc2a996ec9
4
+ data.tar.gz: 7816fc5566f60b7ebe17747d0a1e0b4ee562134192b1c435ab705da44bb86e6c
5
5
  SHA512:
6
- metadata.gz: fc2c71aa2cd450e562425f9f14df68c3e1fa4dfd3281a71a6d830a1acecf574c629d52c3d0b033414a6d1d9e675533f38d0300e2a97413845db2b354e559efb5
7
- data.tar.gz: cc6ff7f47a9e34b7dc01d8a2a15c09482e90e7af414720eb05b64b1616829fddb10f738de696cac215cf8db5c079960cf55817ed5ff6ce8350b4762e141cb1de
6
+ metadata.gz: 03ae9f131c703dad8c92e4a00eb6543412f20f260263ec110f7b2635cc245ed4c84ee35f22449731e8805bbe881fe2fef133c50b37d5bb12edec52d79b2c19ac
7
+ data.tar.gz: 9c0591aadb74a69f51292d0a7e9e3201f72a56358dc6ca561a10624635b8a61077c773f781dc3ad2a3c62eae2f5527ce67eaba8ad6c7d7d0aa06e2f157cf6a58
@@ -5,7 +5,7 @@ class Grover
5
5
  # Configuration of the options for Grover HTML to PDF conversion
6
6
  #
7
7
  class Configuration
8
- attr_accessor :options, :meta_tag_prefix, :ignore_path, :ignore_request,
8
+ attr_accessor :options, :meta_tag_prefix, :ignore_path, :ignore_request, :allow_restricted_meta_option,
9
9
  :root_url, :use_pdf_middleware, :use_png_middleware,
10
10
  :use_jpeg_middleware, :js_runtime_bin,
11
11
  :node_env_vars, :allow_file_uris, :allow_local_network_access
@@ -15,6 +15,7 @@ class Grover
15
15
  @meta_tag_prefix = 'grover-'
16
16
  @ignore_path = nil
17
17
  @ignore_request = nil
18
+ @allow_restricted_meta_option = false
18
19
  @root_url = nil
19
20
  @use_pdf_middleware = true
20
21
  @use_png_middleware = false
@@ -8,6 +8,21 @@ class Grover
8
8
  # Build options from Grover.configuration, meta_options, and passed-in options
9
9
  #
10
10
  class OptionsBuilder < Hash
11
+ #
12
+ # Presentation-only options that are safe to accept from untrusted HTML meta tags.
13
+ # Anything not listed here (e.g. executable_path, launch_args, browser_ws_endpoint,
14
+ # path, root_path, front/back_cover_path, display_url, cookies, credentials) is
15
+ # ignored when it comes from a meta tag, to prevent option injection from page content.
16
+ #
17
+ META_OPTION_ALLOWLIST = %w[
18
+ capture_beyond_viewport clip display_header_footer emulate_media focus footer_template format full_page
19
+ geolocation header_template height hover landscape margin media_features omit_background outline page_ranges
20
+ prefer_css_page_size print_background quality scale tagged timezone viewport vision_deficiency width
21
+ ].freeze
22
+ private_constant :META_OPTION_ALLOWLIST
23
+ META_OPTION_RESTRICTED_LIST = %w[front_cover_path back_cover_path].freeze
24
+ private_constant :META_OPTION_RESTRICTED_LIST
25
+
11
26
  def initialize(options, uri)
12
27
  super()
13
28
  @uri = uri
@@ -29,17 +44,49 @@ class Grover
29
44
  #
30
45
  def meta_options
31
46
  meta_opts = {}
32
-
33
47
  meta_tags.each do |meta|
34
- tag_name = meta['name'] && meta['name'][/#{Grover.configuration.meta_tag_prefix}([a-z_-]+)/, 1]
35
- next unless tag_name
36
-
37
- Utils.deep_assign meta_opts, tag_name.split('-'), meta['content']
48
+ keys = allowed_meta_keys meta['name'], meta['content']
49
+ Utils.deep_assign meta_opts, keys, meta['content'] if keys
38
50
  end
39
-
40
51
  meta_opts
41
52
  end
42
53
 
54
+ def allowed_meta_keys(name, value)
55
+ # Check that the meta tag actually has a name
56
+ return unless name
57
+
58
+ # Extract out the meta tag name, removing the "prefix" (default `grover-`) eg `grover-format` => `format`
59
+ tag_name = name[/\A#{Regexp.escape(Grover.configuration.meta_tag_prefix)}([a-z_-]{1,100})\z/, 1]
60
+ return if tag_name.nil? # No match
61
+ return if /\A[_-]/.match?(tag_name) || /[_-]\z/.match?(tag_name) # Starts or ends with _/- (invalid)
62
+
63
+ # Split the tag by `-` to allow support for nested meta attributes e.g. `grover-margin-top` => `['margin', 'top']`
64
+ keys = tag_name.split('-')
65
+
66
+ # Only return the keys if they're allowed (either explicitly from the allow list, or if the configuration
67
+ # allows restricted options. Disallows any "control" options)
68
+ keys if allow_meta_key? tag_name, keys.first, value
69
+ end
70
+
71
+ def allow_meta_key?(tag_name, primary_key, value) # rubocop:disable Metrics/MethodLength
72
+ # Allow any meta tags where the primary part of the key matches the allow list
73
+ return true if META_OPTION_ALLOWLIST.include? primary_key
74
+
75
+ # Deny any meta tags that don't match exactly to the restricted list
76
+ return false unless META_OPTION_RESTRICTED_LIST.include? tag_name
77
+
78
+ # Check the `allow_restricted_meta_option` configuration whether the restricted meta option is allowed
79
+ case Grover.configuration.allow_restricted_meta_option
80
+ when Proc then Grover.configuration.allow_restricted_meta_option.call tag_name, value
81
+ when true, false then Grover.configuration.allow_restricted_meta_option
82
+ else
83
+ raise UnsafeConfigurationError, Utils.squish(<<~ERROR)
84
+ Grover.configuration.allow_restricted_meta_option must be true, false
85
+ or a Proc that takes the meta tag name and value and returns true or false
86
+ ERROR
87
+ end
88
+ end
89
+
43
90
  def meta_tags
44
91
  Nokogiri::HTML(@uri).xpath('//meta')
45
92
  end
@@ -81,7 +81,9 @@ class Grover
81
81
  end
82
82
 
83
83
  def call_js_method(method, url_or_html, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
84
+ url_or_html = sanitize_unicode_control_characters url_or_html
84
85
  stdin.puts JSON.dump([method, url_or_html, options])
86
+
85
87
  input = stdout.gets
86
88
  raise Errno::EPIPE, "Can't read from worker" if input.nil?
87
89
 
@@ -120,5 +122,10 @@ class Grover
120
122
  stderr.close
121
123
  wait_thr.join
122
124
  end
125
+
126
+ # The line/paragraph separator characters break the JS JSON parser, so replace them with regular newlines
127
+ def sanitize_unicode_control_characters(html)
128
+ html.gsub(/[\u{2028}\u{2029}]/, "\n")
129
+ end
123
130
  end
124
131
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Grover
4
- VERSION = '1.2.10'
4
+ VERSION = '2.0.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.10
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-02 00:00:00.000000000 Z
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri