trek 2.6.0 → 2.7.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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/AGENTS.md +12 -11
  3. data/CHANGELOG.md +51 -0
  4. data/Gemfile.lock +124 -126
  5. data/Makefile +7 -1
  6. data/README.md +17 -21
  7. data/app/components/trek/form/content_editor_component/content_editor_component.css +4 -8
  8. data/app/components/trek/form/image_field_component/image_field_component.js +1 -2
  9. data/app/components/trek/form/password_strength_component/password_strength_component.css +2 -0
  10. data/app/components/trek/form/sound_field_component/sound_field_component.js +1 -2
  11. data/app/components/trek/security_check_component/security_check_component.css +69 -0
  12. data/app/components/trek/security_check_component/security_check_component.html.slim +19 -0
  13. data/app/components/trek/security_check_component/security_check_component.js +15 -0
  14. data/app/components/trek/security_check_component.rb +19 -0
  15. data/app/content/trek/content/cleaner.rb +1 -1
  16. data/app/content/trek/content/list_item.rb +1 -1
  17. data/app/content/trek/content/paragraph.rb +7 -2
  18. data/app/controllers/concerns/trek/auditable.rb +5 -2
  19. data/app/controllers/concerns/trek/localization.rb +6 -2
  20. data/app/jobs/trek/purge_expired_sign_in_codes_job.rb +15 -0
  21. data/app/policies/trek/security_check_policy.rb +8 -0
  22. data/app/uploaders/image_uploader.rb +77 -15
  23. data/config/locales/trek.en.yml +71 -0
  24. data/config/locales/trek.fr.yml +71 -0
  25. data/docs/.vitepress/config.mjs +1 -0
  26. data/docs/background-jobs.md +10 -5
  27. data/docs/deploy.md +69 -6
  28. data/docs/getting-started.md +15 -17
  29. data/docs/index.md +1 -1
  30. data/docs/personal-data.md +61 -0
  31. data/docs/proxies.md +2 -2
  32. data/docs/reference/components.md +2 -2
  33. data/docs/reference/concerns/fragmentable.md +12 -6
  34. data/docs/reference/concerns/orderable.md +2 -2
  35. data/docs/reference/concerns/search-engine-optimizable.md +1 -1
  36. data/docs/reference/concerns/searchable.md +14 -1
  37. data/docs/reference/concerns/sectionable.md +7 -3
  38. data/docs/reference/concerns/versionable.md +1 -1
  39. data/docs/reference/controllers.md +33 -1
  40. data/docs/reference/environment-variables.md +2 -2
  41. data/docs/reference/formatters.md +2 -1
  42. data/docs/reference/forms.md +6 -3
  43. data/docs/reference/generators/admin-user.md +1 -1
  44. data/docs/reference/generators/audit-log.md +12 -13
  45. data/docs/reference/generators/install.md +20 -9
  46. data/docs/reference/generators/public-accounts.md +21 -3
  47. data/docs/reference/generators/scaffold.md +28 -9
  48. data/docs/reference/generators/security-headers.md +4 -3
  49. data/docs/reference/generators/taxonomies.md +1 -1
  50. data/docs/reference/icons.md +5 -3
  51. data/docs/reference/index.md +10 -8
  52. data/docs/reference/models/current.md +5 -3
  53. data/docs/reference/models/page-version.md +1 -1
  54. data/docs/reference/models/user.md +5 -8
  55. data/docs/reference/uploaders.md +25 -0
  56. data/docs/reference/validators.md +1 -1
  57. data/docs/security.md +30 -9
  58. data/docs/what-is-trek.md +3 -3
  59. data/lib/generators/trek/USAGE +1 -1
  60. data/lib/generators/trek/audit_log_generator.rb +8 -0
  61. data/lib/generators/trek/helpers.rb +17 -0
  62. data/lib/generators/trek/install/admin/routes_generator.rb +2 -1
  63. data/lib/generators/trek/install/admin/security_check_generator.rb +36 -0
  64. data/lib/generators/trek/install/ci_generator.rb +19 -0
  65. data/lib/generators/trek/install/postcss_generator.rb +1 -2
  66. data/lib/generators/trek/install/translations_generator.rb +1 -2
  67. data/lib/generators/trek/install_generator.rb +8 -7
  68. data/lib/generators/trek/public_accounts_generator.rb +34 -7
  69. data/lib/generators/trek/scaffold_generator.rb +22 -21
  70. data/lib/generators/trek/scalingo_generator.rb +13 -1
  71. data/lib/generators/trek/templates/.gitlab-ci.yml +10 -1
  72. data/lib/generators/trek/templates/AGENTS.md +2 -2
  73. data/lib/generators/trek/templates/Makefile +1 -0
  74. data/lib/generators/trek/templates/config/i18n-tasks.yml.erb +0 -8
  75. data/lib/generators/trek/templates/controllers/admin/security_checks_controller.rb +11 -0
  76. data/lib/generators/trek/templates/controllers/concerns/trek/authentication.rb +13 -4
  77. data/lib/generators/trek/templates/initializers/good_job.rb.tt +7 -5
  78. data/lib/generators/trek/templates/layouts/admin.html.slim +5 -0
  79. data/lib/generators/trek/templates/policies/security_check.rb +4 -0
  80. data/lib/generators/trek/templates/public_accounts/api/controllers/sessions_controller.rb.tt +13 -1
  81. data/lib/generators/trek/templates/public_accounts/api/controllers/sign_in_codes_controller.rb.tt +5 -0
  82. data/lib/generators/trek/templates/public_accounts/jobs/sign_in_code_job.rb.tt +10 -0
  83. data/lib/generators/trek/templates/public_accounts/locales/accounts.en.yml.tt +10 -0
  84. data/lib/generators/trek/templates/public_accounts/locales/accounts.fr.yml.tt +10 -0
  85. data/lib/generators/trek/templates/public_accounts/models/account.rb.tt +16 -2
  86. data/lib/generators/trek/templates/public_accounts/spec/api/sign_ins_spec.rb.tt +30 -0
  87. data/lib/generators/trek/templates/public_accounts/spec/models/account/sign_in_code_spec.rb.tt +10 -0
  88. data/lib/generators/trek/templates/public_accounts/spec/models/account_spec.rb.tt +21 -1
  89. data/lib/generators/trek/templates/public_accounts/spec/web/sign_ins_spec.rb.tt +22 -0
  90. data/lib/generators/trek/templates/public_accounts/web/authentication.rb.tt +1 -1
  91. data/lib/generators/trek/templates/public_accounts/web/controllers/sessions_controller.rb.tt +8 -1
  92. data/lib/generators/trek/templates/public_accounts/web/controllers/sign_in_codes_controller.rb.tt +5 -0
  93. data/lib/generators/trek/templates/public_accounts/web/views/password_resets/edit.html.slim.tt +4 -1
  94. data/lib/generators/trek/templates/public_accounts/web/views/passwords/edit.html.slim.tt +4 -1
  95. data/lib/generators/trek/templates/public_accounts/web/views/registrations/new.html.slim.tt +3 -1
  96. data/lib/generators/trek/templates/public_accounts/web/views/sessions/new.html.slim.tt +3 -1
  97. data/lib/generators/trek/templates/spec/requests/admin/pages_spec.rb +11 -0
  98. data/lib/generators/trek/templates/spec/requests/admin/security_checks_spec.rb +24 -0
  99. data/lib/generators/trek/templates/spec/requests/admin/sign_in_notices_spec.rb +33 -0
  100. data/lib/generators/trek/templates/views/admin/pages/_form.html.slim +1 -1
  101. data/lib/generators/trek/templates/views/admin/security_checks/show.html.slim +9 -0
  102. data/lib/generators/trek/templates/views/admin/user_password_resets/edit.html.slim +6 -1
  103. data/lib/generators/trek/templates/views/admin/user_sessions/new.html.slim +4 -1
  104. data/lib/tasks/trek/attachments.rake +12 -0
  105. data/lib/tasks/trek/security.rake +10 -0
  106. data/lib/trek/disposable_email_domains.rb +9 -20
  107. data/lib/trek/engine.rb +5 -0
  108. data/lib/trek/json_compatibility.rb +20 -0
  109. data/lib/trek/security_check.rb +241 -0
  110. data/lib/trek/stored_image_metadata.rb +88 -0
  111. data/lib/trek/version.rb +1 -1
  112. data/lib/trek.rb +2 -1
  113. data/package.json +5 -6
  114. data/postcss.config.js +0 -3
  115. data/trek.gemspec +5 -6
  116. data/yarn.lock +983 -1042
  117. metadata +33 -31
@@ -0,0 +1,36 @@
1
+ require "rails/generators/actions"
2
+ require "generators/trek/helpers"
3
+
4
+ module Trek
5
+ module Generators
6
+ module Install
7
+ module Admin
8
+ class SecurityCheckGenerator < Rails::Generators::Base
9
+ include Trek::Generators::Helpers
10
+
11
+ desc "Create the admin's Security page, for admins: Trek::SecurityCheck, on the request too."
12
+
13
+ source_root File.expand_path("../../templates", __dir__)
14
+
15
+ def add_route
16
+ route "resource :security_check, only: :show", namespace: :admin
17
+ end
18
+
19
+ def copy_controller
20
+ template "controllers/admin/security_checks_controller.rb",
21
+ "app/controllers/admin/security_checks_controller.rb"
22
+ end
23
+
24
+ def copy_view
25
+ template "views/admin/security_checks/show.html.slim",
26
+ "app/views/admin/security_checks/show.html.slim"
27
+ end
28
+
29
+ def copy_policy
30
+ template "policies/security_check.rb", "app/policies/admin/security_check_policy.rb"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -5,6 +5,10 @@ module Trek
5
5
  module Generators
6
6
  module Install
7
7
  class CiGenerator < Rails::Generators::Base
8
+ # The Node majors devops/ci-ruby publishes images for, and the one CI falls back to
9
+ NODE_MAJORS = %w[20 22 24 26].freeze
10
+ DEFAULT_NODE_MAJOR = "24".freeze
11
+
8
12
  include Trek::Generators::Helpers
9
13
 
10
14
  desc "Create the Gitlab CI configuration."
@@ -15,6 +19,21 @@ module Trek
15
19
  template ".gitlab-ci.yml",
16
20
  ".gitlab-ci.yml"
17
21
  end
22
+
23
+ private
24
+
25
+ # CI builds and tests on the Node the app deploys with: the trek-buildpack reads it from the
26
+ # .node-version `rails new` writes.
27
+ def node_major
28
+ path = File.join(destination_root, ".node-version")
29
+ major = File.read(path)[/\d+/] if File.exist?(path)
30
+ return DEFAULT_NODE_MAJOR if major.nil?
31
+ return major if NODE_MAJORS.include?(major)
32
+
33
+ say_status :warning, "no ci-ruby image for Node #{major} (.node-version): CI runs on Node " \
34
+ "#{DEFAULT_NODE_MAJOR}", :yellow
35
+ DEFAULT_NODE_MAJOR
36
+ end
18
37
  end
19
38
  end
20
39
  end
@@ -9,7 +9,6 @@ module Trek
9
9
  postcss: "^8.4.18",
10
10
  "postcss-flexbugs-fixes": "^5.0.2",
11
11
  "postcss-import": "^15.0.0",
12
- "postcss-inline-svg": "^5.0.0",
13
12
  "postcss-modules": "^5.0.0",
14
13
  "postcss-preset-env": "^7.8.2"
15
14
  }
@@ -21,7 +20,7 @@ module Trek
21
20
  source_root File.expand_path("../../../../", __dir__)
22
21
 
23
22
  def install_dependencies
24
- install_npm_dependencies(NPM_DEPENDENCIES)
23
+ install_npm_dependencies(NPM_DEPENDENCIES, dev: true)
25
24
  end
26
25
 
27
26
  def copy_postcss_config
@@ -13,11 +13,10 @@ module Trek
13
13
  source_root File.expand_path("../templates", __dir__)
14
14
 
15
15
  def add_gem
16
- install_needed = %w[rails-i18n i18n-tasks deepl-rb mobility].any? { |name| !in_gemfile?(name) }
16
+ install_needed = %w[rails-i18n i18n-tasks mobility].any? { |name| !in_gemfile?(name) }
17
17
 
18
18
  gem "rails-i18n", ">= 6.0.0" unless in_gemfile?("rails-i18n")
19
19
  gem "i18n-tasks", "~> 1.0" unless in_gemfile?("i18n-tasks")
20
- gem "deepl-rb", "~> 3.0" unless in_gemfile?("deepl-rb")
21
20
  gem "mobility", "~> 1.3.2" unless in_gemfile?("mobility")
22
21
  bundle_install if install_needed
23
22
  end
@@ -148,6 +148,7 @@ module Trek
148
148
  generate_inline "trek:install:admin:user_password_resets"
149
149
  generate_inline "trek:install:admin:user_sessions"
150
150
  generate_inline "trek:install:admin:users"
151
+ generate_inline "trek:install:admin:security_check"
151
152
  end
152
153
 
153
154
  def install_assets
@@ -179,7 +180,7 @@ module Trek
179
180
  say ""
180
181
  say "Create an admin user with:"
181
182
  say ""
182
- say " rails g trek:admin:user --email admin@example.com --password ************"
183
+ say " bin/rails g trek:admin:user --email admin@example.com --password ************"
183
184
  say ""
184
185
  say "Then start the server:"
185
186
  say ""
@@ -187,11 +188,11 @@ module Trek
187
188
  say ""
188
189
  say "More generators, to run when the app needs them:"
189
190
  say ""
190
- say " rails g trek:scaffold Book title intro:text # a model and its admin panel"
191
- say " rails g trek:taxonomies # tags and their categories"
192
- say " rails g trek:audit_log # a log of the admin's requests"
193
- say " rails g trek:public_accounts Customer # accounts for the site's users, apart from the admin's"
194
- say " rails g trek:scalingo # the files deploying to Scalingo"
191
+ say " bin/rails g trek:scaffold Book title intro:text # a model and its admin panel"
192
+ say " bin/rails g trek:taxonomies # tags and their categories"
193
+ say " bin/rails g trek:audit_log # a log of the admin's requests"
194
+ say " bin/rails g trek:public_accounts Customer # accounts for the site's users, apart from the admin's"
195
+ say " bin/rails g trek:scalingo # the files deploying to Scalingo"
195
196
  say ""
196
197
  say "All of Trek's generators and their options: https://trek.etamin.studio/reference/#generators"
197
198
  say ""
@@ -199,7 +200,7 @@ module Trek
199
200
 
200
201
  private
201
202
 
202
- # Runs a generator in this process rather than in a new `rails generate`, which boots the app
203
+ # Runs a generator in this process rather than in a new `bin/rails generate`, which boots the app
203
204
  # again: for Trek's 50-odd generators, that took about two minutes. Other gems' generators keep
204
205
  # their own process (`generate`), as they may need a gem this run just added to the Gemfile.
205
206
  def generate_inline(name)
@@ -9,7 +9,8 @@ module Trek
9
9
  #
10
10
  # They sign in one way, picked with --sign-in:
11
11
  #
12
- # - `code` (default): a code emailed to them, which creates the account the first time;
12
+ # - `code` (default): a code emailed to them, which creates the account the first time, unless
13
+ # --invite-only: then only the accounts the app created sign in;
13
14
  # - `password`: a password chosen on signing up, the email confirmed by a link, and reset by one.
14
15
  #
15
16
  # By default for a website: pages, and sessions kept in a cookie. With --api, for a mobile app:
@@ -26,10 +27,12 @@ module Trek
26
27
  include Trek::Generators::Helpers
27
28
 
28
29
  desc "Create accounts for the site's or the app's users, apart from the admin's: " \
29
- "rails g trek:public_accounts Customer [--sign-in=code|password] [--api] [--path=customers] [--model-only]"
30
+ "bin/rails g trek:public_accounts Customer [--sign-in=code|password] [--invite-only] [--api] [--path=customers] [--model-only]"
30
31
 
31
32
  class_option :sign_in, type: :string, default: "code", enum: SIGN_INS,
32
33
  desc: "How accounts sign in: with a code emailed to them, or with a password"
34
+ class_option :invite_only, type: :boolean, default: false,
35
+ desc: "Only the accounts the app created sign in: a code never creates one (with --sign-in=code)"
33
36
  class_option :api, type: :boolean, default: false,
34
37
  desc: "JSON endpoints and JWTs for a mobile app, instead of pages and a cookie"
35
38
  class_option :path, type: :string, default: nil,
@@ -47,9 +50,12 @@ module Trek
47
50
  end
48
51
 
49
52
  def check_options
50
- return unless model_only? && (api? || options[:path].present?)
53
+ if model_only? && (api? || options[:path].present?)
54
+ raise Thor::Error, "--model-only adds no endpoints nor routes: drop --api and --path."
55
+ end
56
+ return unless invite_only? && password?
51
57
 
52
- raise Thor::Error, "--model-only adds no endpoints nor routes: drop --api and --path."
58
+ raise Thor::Error, "--invite-only only applies to --sign-in=code."
53
59
  end
54
60
 
55
61
  def add_jwt_gem
@@ -75,9 +81,9 @@ module Trek
75
81
 
76
82
  def add_current_attributes
77
83
  inject_into_class "app/models/current.rb", "Current", <<-RUBY
78
- # The signed-in #{human_name.downcase} (#{model_only? ? "set by the app's own sign-in" : "#{accounts_module}::Authentication"})
79
- attribute :#{file_name}_session
80
- delegate :#{file_name}, to: :#{file_name}_session, allow_nil: true
84
+ # The signed-in #{human_name.downcase} (#{model_only? ? "set by the app's own sign-in" : "#{accounts_module}::Authentication"})
85
+ attribute :#{file_name}_session
86
+ delegate :#{file_name}, to: :#{file_name}_session, allow_nil: true
81
87
 
82
88
  RUBY
83
89
  end
@@ -135,6 +141,26 @@ delegate :#{file_name}, to: :#{file_name}_session, allow_nil: true
135
141
  route routes unless model_only?
136
142
  end
137
143
 
144
+ # Its sessions expire as the admin's do (Trek::Users::SessionLifetime), and are purged the same way
145
+ def schedule_session_purge
146
+ schedule_in_good_job_cron :"purge_expired_#{file_name}_sessions",
147
+ job_class: "Trek::PurgeExpiredSessionsJob",
148
+ args: ["#{class_name}::Session"],
149
+ cron: "30 3 * * *",
150
+ description: "Delete the #{human_name.downcase} sessions past their idle timeout or lifetime"
151
+ end
152
+
153
+ # Each code holds an email, and only a new one deletes the expired ones
154
+ def schedule_sign_in_code_purge
155
+ return unless code?
156
+
157
+ schedule_in_good_job_cron :"purge_expired_#{file_name}_sign_in_codes",
158
+ job_class: "Trek::PurgeExpiredSignInCodesJob",
159
+ args: ["#{class_name}::SignInCode"],
160
+ cron: "45 3 * * *",
161
+ description: "Delete the #{human_name.downcase} sign-in codes past their validity"
162
+ end
163
+
138
164
  def copy_specs
139
165
  return unless File.directory?(File.join(destination_root, "spec"))
140
166
 
@@ -153,6 +179,7 @@ delegate :#{file_name}, to: :#{file_name}_session, allow_nil: true
153
179
 
154
180
  def code? = options[:sign_in] == "code"
155
181
  def password? = options[:sign_in] == "password"
182
+ def invite_only? = options[:invite_only]
156
183
  def api? = options[:api]
157
184
  def model_only? = options[:model_only]
158
185
  # Pages and a cookie: neither --api nor --model-only
@@ -23,7 +23,7 @@ module Trek
23
23
  super
24
24
  end
25
25
 
26
- # `rails g model Shop::Product` writes app/models/shop.rb as a `module Shop`
26
+ # `bin/rails g model Shop::Product` writes app/models/shop.rb as a `module Shop`
27
27
  # namespace file; if a `Shop` model already lives there, it gets replaced.
28
28
  def warn_about_shadowed_models
29
29
  model_namespaces.each do |ns|
@@ -68,7 +68,7 @@ module Trek
68
68
  # controller/policy), a bare `Shop` under `module Admin` resolves to it.
69
69
  def add_models_to_menu
70
70
  inject_into_file dashboard_file_path,
71
- "\n::#{class_name},",
71
+ "\n ::#{class_name},",
72
72
  after: "MODELS_IN_MENU = ["
73
73
  rubocop_autocorrect dashboard_file_path
74
74
  end
@@ -108,20 +108,6 @@ module Trek
108
108
  end
109
109
  end
110
110
 
111
- def translate_other_locale_files
112
- unless ENV["DEEPL_AUTH_KEY"]
113
- Rails.logger.debug "DEEPL_AUTH_KEY not found. Skipping translation."
114
- return
115
- end
116
-
117
- if other_locales.empty?
118
- Rails.logger.debug "No other locales found. Skipping translation."
119
- return
120
- end
121
-
122
- system "i18n-tasks translate-missing --backend=deepl --from=en #{other_locales.map(&:to_s).join(" ")}"
123
- end
124
-
125
111
  def cleanup
126
112
  return if I18n.available_locales.include?(:en)
127
113
 
@@ -172,11 +158,7 @@ module Trek
172
158
  # TODO: uncomment to attach files to the model, or remove if not needed
173
159
  # include RecordImageUploader::Attachment(:image)
174
160
 
175
- # TODO: choose which attribute to use as title in the admin panel
176
- # def to_s
177
- # title || super
178
- # end
179
-
161
+ #{to_s_method}
180
162
  # TODO: choose which attribute to use as image in the admin panel, or remove if not needed
181
163
  # def image
182
164
  # image_url || super
@@ -184,6 +166,25 @@ module Trek
184
166
  RUBY
185
167
  end
186
168
 
169
+ # The record's title in the admin (lists, breadcrumbs…): its first string attribute, or a TODO
170
+ # without one.
171
+ def to_s_method
172
+ title = attributes.find { it.type == :string }
173
+ return <<~RUBY unless title
174
+ # TODO: choose which attribute to use as title in the admin panel
175
+ # def to_s
176
+ # title.presence || super
177
+ # end
178
+ RUBY
179
+
180
+ <<~RUBY
181
+ # The title in the admin panel
182
+ def to_s
183
+ #{title.name}.presence || super
184
+ end
185
+ RUBY
186
+ end
187
+
187
188
  def other_locales
188
189
  I18n.available_locales.reject { |locale| locale == :en }
189
190
  end
@@ -4,7 +4,7 @@ require "generators/trek/helpers"
4
4
  module Trek
5
5
  module Generators
6
6
  class ScalingoGenerator < Rails::Generators::Base
7
- BUILDPACK_URL = "https://github.com/etaminstudio/trek-buildpack/archive/refs/tags/v0.1.0.tar.gz".freeze
7
+ BUILDPACK_URL = "https://github.com/etaminstudio/trek-buildpack/archive/refs/tags/v0.1.4.tar.gz".freeze
8
8
 
9
9
  include Trek::Generators::Helpers
10
10
 
@@ -16,6 +16,18 @@ module Trek
16
16
  remove_file ".buildpacks"
17
17
  end
18
18
 
19
+ # `rails new` adds Kamal, which a Scalingo app doesn't deploy with. Commenting the gem out drops
20
+ # it from the bundle along with its SSH dependencies (net-ssh, sshkit…), whose advisories would
21
+ # otherwise keep failing bundler-audit.
22
+ def comment_out_kamal
23
+ return unless in_gemfile?("kamal")
24
+
25
+ gsub_file "Gemfile",
26
+ /^(?:# Deploy this application anywhere as a Docker container[^\n]*\n)?gem "kamal"/,
27
+ %(# Unused: the app deploys on Scalingo.\n# gem "kamal")
28
+ bundle_install
29
+ end
30
+
19
31
  def create_aptfile
20
32
  create_file "Aptfile" do
21
33
  <<~TEXT
@@ -20,7 +20,7 @@ stages:
20
20
 
21
21
  # Defaults
22
22
  default:
23
- image: registry.git.etamin.studio/devops/ci-ruby/node-24:4.0.9
23
+ image: registry.git.etamin.studio/devops/ci-ruby/node-<%= node_major %>:4.0.9
24
24
 
25
25
  services:
26
26
  - postgres:16
@@ -158,6 +158,15 @@ bundler-audit:
158
158
  script:
159
159
  - bundle exec bundle-audit check --update
160
160
 
161
+ # Advisories and deprecations, whatever their severity as bundler-audit, on the npm packages the app
162
+ # bundles (`dependencies`), not on its tooling (`devDependencies`). Reads yarn.lock: nothing to
163
+ # install. To skip one that doesn't apply, add its ID to `npmAuditIgnoreAdvisories` in .yarnrc.yml:
164
+ # the number `yarn npm audit` shows, or `<package> (deprecation)`.
165
+ npm-audit:
166
+ <<: *security_definition
167
+ script:
168
+ - yarn npm audit --recursive --environment production
169
+
161
170
  # Software Bill of Materials (CycloneDX): the gems and npm packages in the lockfiles, with their licenses.
162
171
  # Uncomment the job when a client asks for one.
163
172
  # GitLab keeps each branch's latest one by default, past `expire_in`: hand over the one of the production branch.
@@ -4,7 +4,7 @@ Guidance for AI coding agents working in this repository.
4
4
 
5
5
  ## About this project
6
6
 
7
- This is a Ruby on Rails application whose back-office/admin is built with **Trek**, a Rails CMS engine. Trek was installed via `rails g trek:install` and provides the admin UI, authentication, authorization, attachments, and base content models. Project-specific work lives in `app/` — admin controllers under `app/controllers/admin/`, plus your own models, policies, and components. Prefer Trek's generators (`rails g trek:scaffold`, `rails g trek:admin:user`, …) over hand-writing admin plumbing.
7
+ This is a Ruby on Rails application whose back-office/admin is built with **Trek**, a Rails CMS engine. Trek was installed via `bin/rails g trek:install` and provides the admin UI, authentication, authorization, attachments, and base content models. Project-specific work lives in `app/` — admin controllers under `app/controllers/admin/`, plus your own models, policies, and components. Prefer Trek's generators (`bin/rails g trek:scaffold`, `bin/rails g trek:admin:user`, …) over hand-writing admin plumbing.
8
8
 
9
9
  ## Commands
10
10
 
@@ -15,7 +15,7 @@ This is a Ruby on Rails application whose back-office/admin is built with **Trek
15
15
  make test # run the RSpec suite (alias: make spec)
16
16
  make lint # rubocop + slim-lint + eslint + stylelint + prettier
17
17
  make fix # auto-correct lint/format issues
18
- make scan # brakeman + bundle-audit
18
+ make scan # brakeman + bundle-audit + yarn npm audit
19
19
 
20
20
  ## Conventions
21
21
 
@@ -29,6 +29,7 @@ fix:
29
29
  scan:
30
30
  bundle exec brakeman
31
31
  bundle exec bundle-audit check --update
32
+ yarn npm audit --recursive --environment production
32
33
 
33
34
  audit-fix:
34
35
  @out=$$(bundle exec bundle-audit check --update 2>&1); \
@@ -100,14 +100,6 @@ search:
100
100
  ## See this example of a custom scanner: https://github.com/glebm/i18n-tasks/wiki/A-custom-scanner-example
101
101
 
102
102
  ## Translation Services
103
- translation:
104
- deepl_api_key:
105
- deepl_host: "https://api-free.deepl.com"
106
- deepl_glossary_ids:
107
- - f28106eb-0e06-489e-82c6-8215d6f95089
108
- - 2c6415be-1852-4f54-9e1b-d800463496b4
109
- deepl_options:
110
- formality: prefer_less
111
103
  # translation:
112
104
  # # Google Translate
113
105
  # # Get an API key and set billing info at https://code.google.com/apis/console to use Google Translate
@@ -0,0 +1,11 @@
1
+ module Admin
2
+ # The app's security settings (Trek::SecurityCheck), checked on this very request too: whether it came
3
+ # over HTTPS, and the address Rails sees behind a CDN. For admins only (Admin::SecurityCheckPolicy).
4
+ class SecurityChecksController < Admin::BaseController
5
+ def show
6
+ authorize! :security_check, to: :show?, with: Admin::SecurityCheckPolicy
7
+ @check = Trek::SecurityCheck.new(request:)
8
+ @checked_at = Time.current
9
+ end
10
+ end
11
+ end
@@ -83,14 +83,23 @@ module Trek::Authentication
83
83
  ).deliver_later
84
84
  end
85
85
 
86
- # The users who signed in from this browser, in a signed cookie kept for years
86
+ # The last 20 users who signed in from this browser, in a cookie encrypted so that the browser's
87
+ # users can't read who else signed in from it, kept 13 months (the CNIL's yardstick for cookies)
88
+ # after the last of those sign-ins
87
89
  def signed_in_from_this_browser?(user)
88
- Array(cookies.signed[:trek_known_users]).include?(user.id)
90
+ known_user_ids.include?(user.id)
89
91
  end
90
92
 
91
93
  def remember_sign_in_from_this_browser(user)
92
- user_ids = (Array(cookies.signed[:trek_known_users]) | [user.id]).last(20)
93
- cookies.signed.permanent[:trek_known_users] = { value: user_ids, httponly: true, same_site: :lax }
94
+ cookies.encrypted[:trek_known_users] = {
95
+ value: (known_user_ids | [user.id]).last(20), expires: 13.months, httponly: true, same_site: :lax
96
+ }
97
+ end
98
+
99
+ # Also reads the signed cookie Trek wrote before encrypting it, so the upgrade doesn't email every
100
+ # user on their next sign-in. The fallback can go 13 months after the upgrade.
101
+ def known_user_ids
102
+ Array(cookies.encrypted[:trek_known_users] || cookies.signed[:trek_known_users])
94
103
  end
95
104
 
96
105
  def terminate_session
@@ -7,12 +7,14 @@ Rails.application.configure do
7
7
  # Cron runs wherever a scheduler runs, which in :async mode means every web container. Keep it
8
8
  # to production so a development machine never fires a scheduled job.
9
9
  config.good_job.enable_cron = Rails.env.production?
10
+ # Each entry runs a job on a schedule ("0 3 * * *": every day at 3am). trek:audit_log and
11
+ # trek:public_accounts add their purges at the top.
10
12
  config.good_job.cron = {
11
- # purge_old_sessions: {
12
- # cron: "0 3 * * *", # every day at 3am
13
- # class: "PurgeOldSessionsJob",
14
- # description: "Delete the user sessions that expired more than a month ago"
15
- # }
13
+ purge_expired_sessions: {
14
+ cron: "0 3 * * *",
15
+ class: "Trek::PurgeExpiredSessionsJob",
16
+ description: "Delete the admin's sessions past their idle timeout or lifetime"
17
+ }
16
18
  }
17
19
 
18
20
  # An error raised outside a job's own execution (in the scheduler thread) has no retry handler
@@ -61,6 +61,11 @@ html(lang=I18n.locale class=class_for_theme)
61
61
  href: "/admin/good_job",
62
62
  allowed_to: Current.user.admin?,
63
63
  )
64
+ s.with_item(
65
+ text: t("admin.security_checks.show.title"),
66
+ href: %i[admin security_check],
67
+ allowed_to: Current.user.admin?,
68
+ )
64
69
  end
65
70
  end
66
71
  = render Trek::ToasterComponent.new
@@ -0,0 +1,4 @@
1
+ module Admin
2
+ class SecurityCheckPolicy < Trek::SecurityCheckPolicy
3
+ end
4
+ end
@@ -1,4 +1,8 @@
1
- <% if code? -%>
1
+ <% if invite_only? -%>
2
+ # POST { email, otp }: signs in with the emailed code, to the email's account (an email without one
3
+ # was sent no code); answers with the token for the next requests. DELETE: signs out, ending the
4
+ # token's session.
5
+ <% elsif code? -%>
2
6
  # POST { email, otp }: signs in with the emailed code, to the email's account, created if it had none;
3
7
  # answers with the token for the next requests. DELETE: signs out, ending the token's session.
4
8
  <% else -%>
@@ -14,12 +18,20 @@ class <%= controllers_module %>::SessionsController < <%= controllers_module %>:
14
18
 
15
19
  <% if code? -%>
16
20
  email = normalized_email(params[:email])
21
+ <% if invite_only? -%>
22
+ unless <%= class_name %>::SignInCode.redeem(email, params[:otp]) && (@<%= file_name %> = <%= class_name %>.for_sign_in(email))
23
+ count_failed_sign_in_from_ip
24
+ return render_api_error :unauthorized, :invalid_code
25
+ end
26
+
27
+ <% else -%>
17
28
  unless <%= class_name %>::SignInCode.redeem(email, params[:otp])
18
29
  count_failed_sign_in_from_ip
19
30
  return render_api_error :unauthorized, :invalid_code
20
31
  end
21
32
 
22
33
  @<%= file_name %> = <%= class_name %>.for_verified_email!(email)
34
+ <% end -%>
23
35
  render_new_session @<%= file_name %>
24
36
  <% else -%>
25
37
  # Hashes the password sent even when no account matches, so the response takes as long
@@ -1,5 +1,10 @@
1
+ <% if invite_only? -%>
2
+ # POST { email }: emails a code to sign in with, if the email has an account. Whether or not it has
3
+ # one, the answer is the same (202).
4
+ <% else -%>
1
5
  # POST { email }: emails a code to sign in with. Whether or not the email has an account, the answer
2
6
  # is the same (202); the code creates the account on its first use.
7
+ <% end -%>
3
8
  class <%= controllers_module %>::SignInCodesController < <%= controllers_module %>::BaseController
4
9
  allow_unauthenticated_access
5
10
  rate_limit to: 10, within: 15.minutes, with: -> { render_rate_limited }
@@ -2,9 +2,19 @@
2
2
  # here, never in the job's arguments, which the queue keeps. Active Job runs it in the request's
3
3
  # locale.
4
4
  class <%= accounts_module %>::SignInCodeJob < ApplicationJob
5
+ <% if invite_only? -%>
6
+ # To sign in to `email`'s account, if it has one (<%= class_name %>.for_sign_in), or for `<%= file_name %>` to
7
+ # change its email to `email` (purpose: "email_change").
8
+ <% else -%>
5
9
  # To sign in to `email`'s account (created on the code's first use), or for `<%= file_name %>` to
6
10
  # change its email to `email` (purpose: "email_change").
11
+ <% end -%>
7
12
  def perform(email, purpose: "sign_in", <%= file_name %>: nil)
13
+ <% if invite_only? -%>
14
+ # Decided here rather than in the request, so its answer and timing are the same for every email
15
+ return if purpose == "sign_in" && !<%= class_name %>.for_sign_in(email)
16
+
17
+ <% end -%>
8
18
  sign_in_code = <%= class_name %>::SignInCode.issue(email, purpose:, <%= file_name %>:)
9
19
  mail = purpose == "email_change" ? :email_change_code : :sign_in_code
10
20
 
@@ -181,7 +181,11 @@ en:
181
181
  new:
182
182
  <% if code? -%>
183
183
  code: Code
184
+ <% if invite_only? -%>
185
+ intro: If %{email} has an account, we sent it a 6-digit code. It is valid for %{validity}.
186
+ <% else -%>
184
187
  intro: We sent a 6-digit code to %{email}. It is valid for %{validity}.
188
+ <% end -%>
185
189
  other_email: Use another email
186
190
  submit: Sign in
187
191
  title: Enter your code
@@ -195,9 +199,15 @@ en:
195
199
  sign_in_codes:
196
200
  create:
197
201
  invalid_email: Enter a valid email address.
202
+ <% if invite_only? -%>
203
+ sent: If %{email} has an account, a code has just been sent to it.
204
+ new:
205
+ intro: Enter your email. If it has an account, we'll send you a code to sign in.
206
+ <% else -%>
198
207
  sent: We sent a code to %{email}.
199
208
  new:
200
209
  intro: Enter your email. We'll send you a code to sign in, or to create your account if you don't have one yet.
210
+ <% end -%>
201
211
  submit: Send me a code
202
212
  title: Sign in
203
213
  <% end -%>
@@ -181,7 +181,11 @@ fr:
181
181
  new:
182
182
  <% if code? -%>
183
183
  code: Code
184
+ <% if invite_only? -%>
185
+ intro: Si %{email} a un compte, nous lui avons envoyé un code à 6 chiffres. Il est valable %{validity}.
186
+ <% else -%>
184
187
  intro: Nous avons envoyé un code à 6 chiffres à %{email}. Il est valable %{validity}.
188
+ <% end -%>
185
189
  other_email: Utiliser une autre adresse
186
190
  submit: Se connecter
187
191
  title: Saisissez votre code
@@ -195,9 +199,15 @@ fr:
195
199
  sign_in_codes:
196
200
  create:
197
201
  invalid_email: Saisissez une adresse e-mail valide.
202
+ <% if invite_only? -%>
203
+ sent: Si %{email} a un compte, nous venons de lui envoyer un code.
204
+ new:
205
+ intro: Saisissez votre adresse e-mail. Si elle a un compte, nous vous enverrons un code pour vous connecter.
206
+ <% else -%>
198
207
  sent: Nous avons envoyé un code à %{email}.
199
208
  new:
200
209
  intro: Saisissez votre adresse e-mail. Nous vous enverrons un code pour vous connecter, ou pour créer votre compte si vous n'en avez pas encore.
210
+ <% end -%>
201
211
  submit: Recevoir un code
202
212
  title: Se connecter
203
213
  <% end -%>
@@ -1,4 +1,8 @@
1
- <% if code? -%>
1
+ <% if invite_only? -%>
2
+ # A <%= human_name.downcase %>'s account, apart from the admin's User: it signs in with a code emailed to it
3
+ # (<%= class_name %>::SignInCode). Only the accounts the app created sign in (an admin, an invitation):
4
+ # signing in never creates one.
5
+ <% elsif code? -%>
2
6
  # A <%= human_name.downcase %>'s account, apart from the admin's User: it signs in with a code emailed to it
3
7
  # (<%= class_name %>::SignInCode), which creates the account the first time.
4
8
  <% else -%>
@@ -29,6 +33,9 @@ class <%= class_name %> < ApplicationRecord
29
33
  has_many :sessions, dependent: :delete_all
30
34
  <% if code? -%>
31
35
  has_many :sign_in_codes, dependent: :delete_all
36
+ # A sign-in code is its email's, not linked to an account, which may not exist yet: those waiting
37
+ # for the account's email go with it too.
38
+ after_destroy { <%= class_name %>::SignInCode.where(email:, purpose: "sign_in").delete_all }
32
39
  <% end -%>
33
40
 
34
41
  # Also applies to lookups (find_by(email:)), so signing in works whatever the case typed.
@@ -41,7 +48,14 @@ class <%= class_name %> < ApplicationRecord
41
48
 
42
49
  # The accounts nobody used since `time`, e.g. to delete them: they can only be <%= human_name.pluralize.downcase %>.
43
50
  scope :inactive_since, ->(time) { where(last_activity_at: ...time).or(where(last_activity_at: nil, created_at: ...time)) }
44
- <% if code? -%>
51
+ <% if invite_only? -%>
52
+
53
+ # The account `email` signs in to, nil if it has none: no code is sent to it then. Narrow it to
54
+ # let only some accounts in, e.g. `active.find_by(email:)`.
55
+ def self.for_sign_in(email)
56
+ find_by(email:)
57
+ end
58
+ <% elsif code? -%>
45
59
 
46
60
  # The account of `email`, created if it has none: the code it was sent proved the email is theirs.
47
61
  def self.for_verified_email!(email)