grover 1.2.9 → 2.0.0

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: 4f53cae1260aa636889607929ff735fc876e1d439ad656f18a71dab32ef911a2
4
- data.tar.gz: e51808378914be56bf9c9d320633ec0db33db7646d83a7edb229c7517d36540a
3
+ metadata.gz: 4b648a18f5dde412a89f2d19732416d7846037e4e3d86b5eab201e30a3079c86
4
+ data.tar.gz: ccbde1ea60e9adb699bb8836dbda73fbba6453f4e0a5193c0c1a1f73201f09b9
5
5
  SHA512:
6
- metadata.gz: dde098900baf3045083d5e60fdf431438c9fe599d373a44d21102c3bc9c2a5370fd6a7dadb580d74fd559f5cafcf2239c5249bd9849c4f2057f410b9fc88ce7f
7
- data.tar.gz: b3a33f90d6658bde15d1bf9179f86fbb769d72c5c5ee3212f509aeda195def9233eb4a91ee4c17824d0646acd7e5d07907efa96374793ec23458ce4a60a1dffa
6
+ metadata.gz: 4d43ee6e339c38473da2be84cc72f87da2f9dfbc93f1d1e266f45924046b868bdacde29246d298b85debf2d598dc5c4096aba56833041386eab8eb7f4c487786
7
+ data.tar.gz: e10ad55ebd9efaa7e49ccdaf6d3b36aba5842db01f4fe940e1b4ed07172e5a34346acdb85fba718dad69200511177c6faae4d1515d94da690e31134e4350aa67
@@ -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
@@ -34,7 +34,7 @@ function GroverError(name, errors) {
34
34
  GroverError.prototype = Error.prototype;
35
35
 
36
36
  const _processPage = (async (convertAction, uriOrHtml, options) => {
37
- let browser, page, tmpDir, wsConnection = false;
37
+ let browser, context, page, tmpDir, wsConnection = false;
38
38
  const requestErrors = [], pageErrors = [];
39
39
 
40
40
  const captureRequestError = (request) => {
@@ -114,7 +114,12 @@ const _processPage = (async (convertAction, uriOrHtml, options) => {
114
114
  browser = await puppeteer.launch(launchParams);
115
115
  }
116
116
 
117
- page = await browser.newPage();
117
+ if (wsConnection) {
118
+ context = await browser.createBrowserContext();
119
+ page = await context.newPage();
120
+ } else {
121
+ page = await browser.newPage();
122
+ }
118
123
 
119
124
  // Basic auth
120
125
  const username = options.username; delete options.username
@@ -349,7 +354,7 @@ const _processPage = (async (convertAction, uriOrHtml, options) => {
349
354
  } finally {
350
355
  if (browser) {
351
356
  if (wsConnection) {
352
- if (page) await page.close();
357
+ if (context) await context.close();
353
358
  await browser.disconnect();
354
359
  } else {
355
360
  await browser.close();
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Grover
4
- VERSION = '1.2.9'
4
+ VERSION = '2.0.0'
5
5
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.9
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-11-02 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: nokogiri
@@ -51,6 +52,7 @@ licenses:
51
52
  metadata:
52
53
  allowed_push_host: https://rubygems.org
53
54
  rubygems_mfa_required: 'true'
55
+ post_install_message:
54
56
  rdoc_options: []
55
57
  require_paths:
56
58
  - lib
@@ -68,7 +70,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
72
  requirements: []
71
- rubygems_version: 3.6.9
73
+ rubygems_version: 3.3.7
74
+ signing_key:
72
75
  specification_version: 4
73
76
  summary: A Ruby gem to transform HTML into PDF, PNG or JPEG by wrapping the NodeJS
74
77
  Google Puppeteer driver for Chromium