decidim-cdtb 0.6.2 → 0.6.4
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 +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +18 -0
- data/config/initializers/cdtb_rack_attack.rb +30 -4
- data/lib/cdtb/ip_parser.rb +2 -0
- data/lib/decidim/cdtb/tasks.rb +1 -0
- data/lib/decidim/cdtb/upgrades/hero_content_block_settings.rb +105 -0
- data/lib/decidim/cdtb/version.rb +1 -1
- data/lib/tasks/upgrade.rake +24 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cb170af8cd1724e42f2651540d6a91f19d4706075bf8cd65d7db7c5b1ac93267
|
|
4
|
+
data.tar.gz: d1b78848891c940476824c5812a5d0dfd5e36e93efa9f615ab8a8835d58919de
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 61af8001a2df666fdeb281be62817dd7d4e0727804a243f6dd00fc6b0a360c3225c5edeef2a6e06e6141be5454d05741806d7e61c9f87daa8dd7935734570426
|
|
7
|
+
data.tar.gz: e2ae5dd5539898e812f9a40853d5463e72db5a7ef224e42f4ceedb4b7234b798b30b2b7f1798af65a7e7df9d35aad8bbd327848c34c038a7d1ae50abfa8227df
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [0.6.4] - 2026-11-17 (minor)
|
|
2
|
+
|
|
3
|
+
- Add `cdtb:upgrades:hero_content_block_settings:export` and `cdtb:upgrades:hero_content_block_settings:restore` tasks to back up hero content block settings before upgrading to Decidim v0.31 and restore the ones lost by the CTA migration.
|
|
4
|
+
|
|
5
|
+
## [0.6.3] - 2026-10-16 (minor)
|
|
6
|
+
|
|
7
|
+
- Implement RackAttack feature to block abuse reported IPs.
|
|
8
|
+
- Improve RackAttack tests and compatibility with subnets.
|
|
9
|
+
|
|
1
10
|
## [0.6.2] - 2026-09-10 (minor)
|
|
2
11
|
|
|
3
12
|
- Prevent `cdtb:users:remove` from enqueueing the admin block notification email when blocking users while preserving auditability and moderation records.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -303,6 +303,24 @@ See the [Installation](#installation) chapter to install a GitHub Action on your
|
|
|
303
303
|
|
|
304
304
|
TO-DO also check that all migrations have been executed and the schema.rb does not change
|
|
305
305
|
|
|
306
|
+
#### Back up and restore hero content block settings
|
|
307
|
+
|
|
308
|
+
When upgrading to Decidim v0.31, the `MoveCtaToHeroContentBlock` migration overwrites the settings of the hero content blocks with the CTA values, losing every other setting.
|
|
309
|
+
|
|
310
|
+
Before upgrading (before running the migrations), export the raw settings of every hero content block to a new JSON file (the export will not overwrite an existing file):
|
|
311
|
+
|
|
312
|
+
```
|
|
313
|
+
bin/rake 'cdtb:upgrades:hero_content_block_settings:export[/secure/path/hero-settings.json]'
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
Keep the file securely outside the application repository. After upgrading, restore the missing keys with:
|
|
317
|
+
|
|
318
|
+
```
|
|
319
|
+
bin/rake 'cdtb:upgrades:hero_content_block_settings:restore[/secure/path/hero-settings.json]'
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
The restore uses the original content block and organization IDs and scope, so run it against the same database. It preserves any current setting, including the migrated CTA values, and only adds keys absent from the current settings. Each block is restored in its own transaction; a missing or mismatched block is reported and skipped, and the restore continues with the rest. An invalid backup aborts the restore. A backup from before the affected migration is required to recover settings that were lost.
|
|
323
|
+
|
|
306
324
|
## Development
|
|
307
325
|
|
|
308
326
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "cdtb/ip_parser"
|
|
4
|
+
require "csv"
|
|
5
|
+
require "ipaddr"
|
|
4
6
|
|
|
5
7
|
unless ENV["CDTB_RACK_ATTACK_DISABLED"].to_i.positive? || %w[development test].include?(Rails.env)
|
|
6
8
|
require "rack/attack"
|
|
@@ -36,14 +38,38 @@ unless ENV["CDTB_RACK_ATTACK_DISABLED"].to_i.positive? || %w[development test].i
|
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
Rack::Attack.blocklist("cdtb:block all PHP RQs") do |request|
|
|
39
|
-
request.path.end_with?("
|
|
41
|
+
request.path.end_with?(".php")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
ABUSE_REPORTED_IPS_CSV_PATH= ENV.fetch("ABUSE_REPORTED_IPS_CSV_PATH", nil)
|
|
45
|
+
if ABUSE_REPORTED_IPS_CSV_PATH.present? && File.exist?(ABUSE_REPORTED_IPS_CSV_PATH)
|
|
46
|
+
ABUSE_REPORTED_IPS_CACHE_KEY= ["cdtb", "reported_ips", ABUSE_REPORTED_IPS_CSV_PATH].freeze
|
|
47
|
+
Rack::Attack.blocklist("block IPs in reported website") do |request|
|
|
48
|
+
ip= Cdtb::IpParser.extract_ip(request)
|
|
49
|
+
|
|
50
|
+
reported_ips = Rails.cache.fetch(ABUSE_REPORTED_IPS_CACHE_KEY, expires_in: 1.hour) do
|
|
51
|
+
if ABUSE_REPORTED_IPS_CSV_PATH.present? && File.exist?(ABUSE_REPORTED_IPS_CSV_PATH)
|
|
52
|
+
CSV.read(ABUSE_REPORTED_IPS_CSV_PATH).flatten.compact.map(&:strip).reject(&:blank?)
|
|
53
|
+
else
|
|
54
|
+
[]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
reported_ips.any? { |reported_ip| ip.start_with?(reported_ip) }
|
|
59
|
+
end
|
|
40
60
|
end
|
|
41
61
|
|
|
42
62
|
if ENV["RACK_ATTACK_BLOCKED_IPS"].present?
|
|
43
|
-
blocked_ips_and_subnets= ENV["RACK_ATTACK_BLOCKED_IPS"].split(",")
|
|
63
|
+
blocked_ips_and_subnets= ENV["RACK_ATTACK_BLOCKED_IPS"].split(",").filter_map do |ip_or_subnet|
|
|
64
|
+
IPAddr.new(ip_or_subnet.strip)
|
|
65
|
+
rescue IPAddr::InvalidAddressError
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
44
68
|
Rack::Attack.blocklist("cdtb:block all unaccepted IPs") do |request|
|
|
45
|
-
|
|
46
|
-
blocked_ips_and_subnets.any? { |ip_or_subnet|
|
|
69
|
+
request_ip= IPAddr.new(Cdtb::IpParser.extract_ip(request))
|
|
70
|
+
blocked_ips_and_subnets.any? { |ip_or_subnet| ip_or_subnet.include?(request_ip) }
|
|
71
|
+
rescue IPAddr::InvalidAddressError
|
|
72
|
+
false
|
|
47
73
|
end
|
|
48
74
|
end
|
|
49
75
|
end
|
data/lib/cdtb/ip_parser.rb
CHANGED
data/lib/decidim/cdtb/tasks.rb
CHANGED
|
@@ -13,3 +13,4 @@ require "decidim/cdtb/spam/spam_users_detector"
|
|
|
13
13
|
require "decidim/cdtb/storage/local_sharding"
|
|
14
14
|
require "decidim/cdtb/storage/set_local_on_blobs"
|
|
15
15
|
require "decidim/cdtb/upgrades/validate_migrations_task"
|
|
16
|
+
require "decidim/cdtb/upgrades/hero_content_block_settings"
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Cdtb
|
|
7
|
+
module Upgrades
|
|
8
|
+
# Exports and restores the raw settings of every hero content block.
|
|
9
|
+
#
|
|
10
|
+
# The `MoveCtaToHeroContentBlock` migration from Decidim v0.31 overwrites the hero
|
|
11
|
+
# content block settings with only the CTA values, losing every other setting.
|
|
12
|
+
# Export the settings before migrating and restore them afterwards.
|
|
13
|
+
module HeroContentBlockSettings
|
|
14
|
+
VERSION = 1
|
|
15
|
+
|
|
16
|
+
# Writes a versioned JSON file with the settings of all hero content blocks.
|
|
17
|
+
# Refuses to overwrite an existing file and makes it readable only by the owner.
|
|
18
|
+
#
|
|
19
|
+
# Returns the number of exported content blocks.
|
|
20
|
+
def self.export(path)
|
|
21
|
+
records = Decidim::ContentBlock.where(manifest_name: "hero").order(:id)
|
|
22
|
+
.pluck(:id, :decidim_organization_id, :scope_name, :settings)
|
|
23
|
+
.map do |id, organization_id, scope_name, settings|
|
|
24
|
+
{ id:, organization_id:, scope_name:, settings: }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
File.open(path, "wx", 0o600) do |file|
|
|
28
|
+
file.write(JSON.pretty_generate(version: VERSION, content_blocks: records))
|
|
29
|
+
file.write("\n")
|
|
30
|
+
end
|
|
31
|
+
records.size
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Restores the keys missing from the current settings of each hero content block.
|
|
35
|
+
# Current values (including the migrated CTA values) are always preserved.
|
|
36
|
+
# Each block is restored in its own transaction: a failing block is reported and
|
|
37
|
+
# the restore continues with the rest. An invalid backup aborts the whole restore.
|
|
38
|
+
#
|
|
39
|
+
# Returns the number of successfully processed content blocks.
|
|
40
|
+
def self.restore(path)
|
|
41
|
+
backup = JSON.parse(File.read(path))
|
|
42
|
+
records = validate_backup!(backup)
|
|
43
|
+
|
|
44
|
+
records.count do |record|
|
|
45
|
+
Decidim::ContentBlock.transaction { restore_record!(record) }
|
|
46
|
+
true
|
|
47
|
+
rescue StandardError => e
|
|
48
|
+
puts "Block [#{record["id"]}] failed due to: #{e.message} - #{e.backtrace&.first(5)&.join("\n")}"
|
|
49
|
+
false
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.restore_record!(record)
|
|
54
|
+
block = Decidim::ContentBlock.lock.find_by!(
|
|
55
|
+
id: record.fetch("id"),
|
|
56
|
+
decidim_organization_id: record.fetch("organization_id"),
|
|
57
|
+
scope_name: record.fetch("scope_name"),
|
|
58
|
+
manifest_name: "hero"
|
|
59
|
+
)
|
|
60
|
+
current = block[:settings]
|
|
61
|
+
raise ArgumentError, "Invalid current settings for hero block #{block.id}" unless current.nil? || current.is_a?(Hash)
|
|
62
|
+
|
|
63
|
+
current ||= {}
|
|
64
|
+
restored = (record.fetch("settings") || {}).merge(current)
|
|
65
|
+
return if restored == current
|
|
66
|
+
|
|
67
|
+
block.update_columns(settings: restored, updated_at: Time.current)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.validate_backup!(backup)
|
|
71
|
+
raise ArgumentError, "Invalid hero settings backup format or version" unless valid_format?(backup)
|
|
72
|
+
|
|
73
|
+
records = backup.fetch("content_blocks")
|
|
74
|
+
raise ArgumentError, "Invalid hero settings backup record" unless records.all? { |record| valid_record?(record) }
|
|
75
|
+
|
|
76
|
+
ids = records.map { |record| record.fetch("id") }
|
|
77
|
+
raise ArgumentError, "Duplicate hero content block IDs in backup" unless ids.uniq.size == ids.size
|
|
78
|
+
|
|
79
|
+
records
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.valid_format?(backup)
|
|
83
|
+
backup.is_a?(Hash) && backup["version"] == VERSION && backup["content_blocks"].is_a?(Array)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.valid_record?(record)
|
|
87
|
+
record.is_a?(Hash) &&
|
|
88
|
+
positive_integer?(record["id"]) &&
|
|
89
|
+
positive_integer?(record["organization_id"]) &&
|
|
90
|
+
record["scope_name"].is_a?(String) && !record["scope_name"].empty? &&
|
|
91
|
+
valid_settings?(record)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.valid_settings?(record)
|
|
95
|
+
record.key?("settings") && (record["settings"].nil? || record["settings"].is_a?(Hash))
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.positive_integer?(value)
|
|
99
|
+
value.is_a?(Integer) && value.positive?
|
|
100
|
+
end
|
|
101
|
+
private_class_method :restore_record!, :validate_backup!, :valid_format?, :valid_record?, :valid_settings?, :positive_integer?
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
data/lib/decidim/cdtb/version.rb
CHANGED
data/lib/tasks/upgrade.rake
CHANGED
|
@@ -16,5 +16,29 @@ namespace :cdtb do
|
|
|
16
16
|
task.execute!
|
|
17
17
|
raise("There are pending migrations") if task.pending_migrations?
|
|
18
18
|
end
|
|
19
|
+
|
|
20
|
+
namespace :hero_content_block_settings do
|
|
21
|
+
require "decidim/cdtb/upgrades/hero_content_block_settings"
|
|
22
|
+
|
|
23
|
+
desc <<~EODESC
|
|
24
|
+
Export all hero content block settings to a new JSON file. Run it before upgrading to Decidim v0.31.
|
|
25
|
+
EODESC
|
|
26
|
+
task :export, [:path] => :environment do |_task, args|
|
|
27
|
+
raise ArgumentError, "Provide a JSON file path" if args.path.blank?
|
|
28
|
+
|
|
29
|
+
count = Decidim::Cdtb::Upgrades::HeroContentBlockSettings.export(args.path)
|
|
30
|
+
puts "Exported settings for #{count} hero content blocks to #{args.path}"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
desc <<~EODESC
|
|
34
|
+
Restore missing hero content block settings from a JSON backup generated with the export task.
|
|
35
|
+
EODESC
|
|
36
|
+
task :restore, [:path] => :environment do |_task, args|
|
|
37
|
+
raise ArgumentError, "Provide a JSON file path" if args.path.blank?
|
|
38
|
+
|
|
39
|
+
count = Decidim::Cdtb::Upgrades::HeroContentBlockSettings.restore(args.path)
|
|
40
|
+
puts "Successfully processed #{count} hero content blocks from #{args.path}"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
19
43
|
end
|
|
20
44
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: decidim-cdtb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Oliver Valls
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: decidim
|
|
@@ -105,6 +105,7 @@ files:
|
|
|
105
105
|
- lib/decidim/cdtb/task.rb
|
|
106
106
|
- lib/decidim/cdtb/tasks.rb
|
|
107
107
|
- lib/decidim/cdtb/tasks_utils.rb
|
|
108
|
+
- lib/decidim/cdtb/upgrades/hero_content_block_settings.rb
|
|
108
109
|
- lib/decidim/cdtb/upgrades/install_gem_migrations_step.rb
|
|
109
110
|
- lib/decidim/cdtb/upgrades/upgrade_modules_task.rb
|
|
110
111
|
- lib/decidim/cdtb/upgrades/validate_migrations_task.rb
|