shrine-zip-guard 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '08688c20b1b8190ce44c95079e5d71a302b42f18f635876f1f8265381650e086'
4
+ data.tar.gz: 3c5fa541d7cfe9620b405e33ce7457ede9df3f37503583492a0e0089e7ee0f35
5
+ SHA512:
6
+ metadata.gz: d5571ca6c940dc80e911a826bc4231aa6fbb712841d5038a355a4fa8ed0664caf0fa6e506e27d90e73784e77ee690f60d44e5e0904560c7a3ff1d8d8a8fa0252
7
+ data.tar.gz: 3228249ed0d1246cc04976fd2a7fb7b600bdfe3b20ca5ea343c8c60bda85a540ea1b42808445c2cc4b7a663e8e4908aa91ef3d579128f827f13314888bd0c1e0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ Naming/FileName:
13
+ Exclude:
14
+ - lib/shrine-zip-guard.rb
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - spec/**/*.rb
19
+ - shrine-zip-guard.gemspec
20
+
21
+ Style/Documentation:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [0.1.0] - 2026-08-06
6
+
7
+ ### Added
8
+ - Initial release of `shrine-zip-guard`.
9
+ - `Shrine::Plugins::ZipGuard.with_zip` helper for safe ZIP processing.
10
+ - Automatic validation via `plugin :zip_guard, required_files: [...]`.
11
+ - `EncryptedZipError` for password-protected archives.
12
+ - `MissingFileError` for missing required files, with glob pattern support.
13
+ - Configurable error fallback handler.
14
+ - Default Rails error reporter integration for unexpected errors.
15
+ - English I18n translations.
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Martin Dobrovodský
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # Shrine::ZipGuard
2
+
3
+ A [Shrine](https://shrinerb.com/) plugin for safe ZIP derivative processing.
4
+
5
+ It catches common ZIP errors during derivative creation — password-protected
6
+ archives and missing required files — and converts them into friendly, I18n-ready
7
+ error messages attached to `attacher.errors`.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "shrine-zip-guard"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### Automatic validation
26
+
27
+ If you only need to validate that the uploaded ZIP contains certain files, enable
28
+ the plugin with `required_files`:
29
+
30
+ ```ruby
31
+ class XmlUploader < Shrine
32
+ plugin :derivatives
33
+ plugin :zip_guard, required_files: ["**/*.xml"]
34
+ end
35
+ ```
36
+
37
+ This will automatically check that the ZIP is not password-protected and that it
38
+ contains at least one file matching each glob pattern.
39
+
40
+ ### Custom processing with `with_zip`
41
+
42
+ If you need to run your own logic (for example extracting files, checking
43
+ specific paths, or adding custom error rules), use the `with_zip` helper inside
44
+ your `Attacher.derivatives` block:
45
+
46
+ ```ruby
47
+ class XmlUploader < Shrine
48
+ plugin :derivatives
49
+ plugin :zip_guard
50
+
51
+ Attacher.derivatives do |original|
52
+ Shrine::Plugins::ZipGuard.with_zip(original, required_files: ["**/*.xml"]) do |zip_file|
53
+ xmls = zip_file.glob("*.xml")
54
+
55
+ # Your custom rule — for example requiring the file to be at the root level
56
+ raise ZipUploadErrors::NestedFileError if xmls.empty? && zip_file.glob("**/*.xml").any?
57
+
58
+ if xmls.size == 1
59
+ extracted = Tempfile.new(["extracted", ".xml"], binmode: true)
60
+ extracted.write(xmls.first.get_input_stream.read)
61
+ extracted.rewind
62
+ { extracted_xml: extracted }
63
+ else
64
+ {}
65
+ end
66
+ end
67
+ end
68
+ end
69
+ ```
70
+
71
+ ## Error messages
72
+
73
+ All user-facing errors are translated via I18n. The gem ships with English
74
+ defaults. Add your own translations under the `shrine.zip_guard` key:
75
+
76
+ ```yaml
77
+ en:
78
+ shrine:
79
+ zip_guard:
80
+ password_protected: "ZIP file is password protected. Please upload an unprotected file."
81
+ missing_file: "Archive does not contain required file matching '%{pattern}'."
82
+ processing_failed: "File could not be processed. Please try again or contact support."
83
+ ```
84
+
85
+ ## Custom fallback for unexpected errors
86
+
87
+ By default, unexpected `ZipGuard::Error` subclasses are reported to the Rails error
88
+ reporter (if available) and a generic `processing_failed` message is added to
89
+ `attacher.errors`. You can override this with your own handler:
90
+
91
+ ```ruby
92
+ plugin :zip_guard,
93
+ required_files: ["**/*.xml"],
94
+ fallback: lambda { |error, attacher|
95
+ Sentry.capture_exception(error)
96
+ attacher.errors << "Custom processing error: #{error.message}"
97
+ }
98
+ ```
99
+
100
+ ## Supported versions
101
+
102
+ - Ruby >= 3.1
103
+ - Shrine >= 3.0, < 4.0
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,6 @@
1
+ en:
2
+ shrine:
3
+ zip_guard:
4
+ password_protected: "ZIP file is password protected. Please upload an unprotected file."
5
+ missing_file: "Archive does not contain required file matching '%{pattern}'."
6
+ processing_failed: "File could not be processed. Please try again or contact support."
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shrine"
4
+ require "zip"
5
+
6
+ class Shrine
7
+ module Plugins
8
+ module ZipGuard
9
+ class Error < StandardError; end
10
+ class EncryptedZipError < Error; end
11
+
12
+ class MissingFileError < Error
13
+ attr_reader :pattern
14
+
15
+ def initialize(pattern)
16
+ @pattern = pattern
17
+ super("Archive does not contain required file matching '#{pattern}'")
18
+ end
19
+ end
20
+
21
+ class << self
22
+ def with_zip(original, required_files: nil, &)
23
+ return yield(nil) unless zip?(original)
24
+
25
+ Zip::File.open(original.path) do |zip_file|
26
+ raise EncryptedZipError if zip_file.any?(&:encrypted?)
27
+
28
+ if required_files
29
+ Array(required_files).each do |pattern|
30
+ raise MissingFileError, pattern unless zip_file.glob(pattern).any?
31
+ end
32
+ end
33
+
34
+ yield zip_file
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def zip?(original)
41
+ return false unless original.respond_to?(:path)
42
+
43
+ File.extname(original.path).downcase == ".zip"
44
+ end
45
+ end
46
+
47
+ module AttacherMethods
48
+ def create_derivatives(*args, **options, &)
49
+ super
50
+ rescue ZipGuard::EncryptedZipError
51
+ errors << I18n.t("shrine.zip_guard.password_protected")
52
+ {}
53
+ rescue ZipGuard::MissingFileError => e
54
+ errors << I18n.t("shrine.zip_guard.missing_file", pattern: e.pattern)
55
+ {}
56
+ rescue ZipGuard::Error => e
57
+ handle_zip_guard_error(e)
58
+ {}
59
+ end
60
+
61
+ private
62
+
63
+ def handle_zip_guard_error(error)
64
+ fallback = shrine_class.opts[:zip_guard][:fallback]
65
+
66
+ if fallback
67
+ fallback.call(error, self)
68
+ else
69
+ report_fallback(error)
70
+ errors << I18n.t("shrine.zip_guard.processing_failed")
71
+ end
72
+ end
73
+
74
+ def report_fallback(error)
75
+ return unless defined?(Rails) && Rails.respond_to?(:error) && Rails.error.respond_to?(:report)
76
+
77
+ Rails.error.report(error)
78
+ end
79
+ end
80
+
81
+ def self.configure(uploader, **opts)
82
+ uploader.opts[:zip_guard] = {
83
+ fallback: opts[:fallback],
84
+ required_files: opts[:required_files]
85
+ }
86
+
87
+ return unless opts[:required_files]
88
+
89
+ uploader::Attacher.derivatives do |original|
90
+ ZipGuard.with_zip(original, required_files: opts[:required_files]) do |_zip_file|
91
+ {}
92
+ end
93
+ end
94
+ end
95
+
96
+ def self.load_dependencies(uploader, **)
97
+ uploader.plugin :derivatives
98
+ uploader.plugin :validation
99
+ end
100
+ end
101
+
102
+ register_plugin(:zip_guard, ZipGuard)
103
+ end
104
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShrineZipGuard
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shrine"
4
+ require "i18n"
5
+
6
+ require_relative "shrine-zip-guard/version"
7
+ require_relative "shrine/plugins/zip_guard"
8
+
9
+ I18n.load_path += Dir[File.expand_path("../../config/locales/*.yml", __dir__)]
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-zip-guard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Dobrovodský
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '4.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '4.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: shrine
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '4.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '4.0'
67
+ description: Shrine plugin that catches encrypted ZIPs and missing files during derivatives.
68
+ email:
69
+ - dobrovodsky.martin@gmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".rspec"
75
+ - ".rubocop.yml"
76
+ - CHANGELOG.md
77
+ - CODE_OF_CONDUCT.md
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - config/locales/en.yml
82
+ - lib/shrine-zip-guard.rb
83
+ - lib/shrine-zip-guard/version.rb
84
+ - lib/shrine/plugins/zip_guard.rb
85
+ homepage: https://github.com/MaNi4/shrine-zip-guard
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ homepage_uri: https://github.com/MaNi4/shrine-zip-guard
90
+ changelog_uri: https://github.com/MaNi4/shrine-zip-guard/blob/main/CHANGELOG.md
91
+ rubygems_mfa_required: 'true'
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 3.1.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.5.16
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Shrine plugin for safe ZIP derivative processing
111
+ test_files: []