trek 2.5.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 (232) hide show
  1. checksums.yaml +4 -4
  2. data/.node-version +1 -1
  3. data/AGENTS.md +18 -14
  4. data/CHANGELOG.md +88 -0
  5. data/Gemfile +5 -0
  6. data/Gemfile.lock +135 -124
  7. data/Makefile +9 -1
  8. data/README.md +17 -21
  9. data/app/components/trek/dashboard/tile_component/tile_component.css +9 -0
  10. data/app/components/trek/dashboard/tile_component/tile_component.html.slim +4 -2
  11. data/app/components/trek/dashboard/tile_component.rb +12 -2
  12. data/app/components/trek/form/content_editor_component/content_editor_component.css +9 -8
  13. data/app/components/trek/form/content_editor_component/content_editor_component.html.slim +19 -93
  14. data/app/components/trek/form/content_editor_component/content_editor_component.js +94 -251
  15. data/app/components/trek/form/content_editor_component/extensions/block_view.js +40 -0
  16. data/app/components/trek/form/content_editor_component/extensions/image_block.js +28 -26
  17. data/app/components/trek/form/content_editor_component/extensions/prompts_block.js +15 -26
  18. data/app/components/trek/form/content_editor_component/extensions/table_of_contents.js +1 -3
  19. data/app/components/trek/form/content_editor_component.rb +15 -0
  20. data/app/components/trek/form/image_field_component/image_field_component.css +40 -1
  21. data/app/components/trek/form/image_field_component/image_field_component.html.slim +0 -9
  22. data/app/components/trek/form/image_field_component/image_field_component.js +1 -2
  23. data/app/components/trek/form/image_field_component.rb +36 -9
  24. data/app/components/trek/form/password_strength_component/password_strength_component.css +2 -0
  25. data/app/components/trek/form/sound_field_component/sound_field_component.js +1 -2
  26. data/app/components/trek/security_check_component/security_check_component.css +69 -0
  27. data/app/components/trek/security_check_component/security_check_component.html.slim +19 -0
  28. data/app/components/trek/security_check_component/security_check_component.js +15 -0
  29. data/app/components/trek/security_check_component.rb +19 -0
  30. data/app/content/trek/content/cleaner.rb +53 -0
  31. data/app/content/trek/content/document.rb +2 -2
  32. data/app/content/trek/content/list_item.rb +1 -1
  33. data/app/content/trek/content/paragraph.rb +7 -2
  34. data/app/content/trek/content/prompts_block.rb +1 -2
  35. data/app/content/trek/content.rb +6 -0
  36. data/app/controllers/concerns/trek/auditable.rb +5 -2
  37. data/app/controllers/concerns/trek/localization.rb +6 -2
  38. data/app/controllers/concerns/trek/reorderable.rb +24 -6
  39. data/app/controllers/trek/csp_reports_controller.rb +14 -1
  40. data/app/controllers/trek/panels/prompts_controller.rb +1 -1
  41. data/app/controllers/trek/resource_controller.rb +5 -5
  42. data/app/jobs/trek/purge_expired_sessions_job.rb +4 -3
  43. data/app/jobs/trek/purge_expired_sign_in_codes_job.rb +15 -0
  44. data/app/models/concerns/trek/contentable.rb +15 -1
  45. data/app/models/concerns/trek/users/sign_in_code.rb +70 -0
  46. data/app/models/concerns/trek/users/sign_in_lockout.rb +4 -2
  47. data/app/policies/trek/security_check_policy.rb +8 -0
  48. data/app/uploaders/image_uploader.rb +77 -15
  49. data/app/views/trek/panels/links/_form.html.slim +2 -4
  50. data/app/views/trek/panels/prompts/create.turbo_stream.slim +0 -1
  51. data/config/locales/trek.en.yml +97 -11
  52. data/config/locales/trek.fr.yml +97 -11
  53. data/docs/.vitepress/config.mjs +5 -1
  54. data/docs/background-jobs.md +74 -0
  55. data/docs/content-editor.md +219 -0
  56. data/docs/customize.md +2 -0
  57. data/docs/deploy.md +69 -6
  58. data/docs/getting-started.md +16 -18
  59. data/docs/index.md +2 -2
  60. data/docs/personal-data.md +61 -0
  61. data/docs/proxies.md +2 -2
  62. data/docs/reference/components.md +4 -4
  63. data/docs/reference/concerns/contentable.md +10 -4
  64. data/docs/reference/concerns/formattable.md +1 -1
  65. data/docs/reference/concerns/fragmentable.md +12 -6
  66. data/docs/reference/concerns/orderable.md +2 -2
  67. data/docs/reference/concerns/search-engine-optimizable.md +1 -1
  68. data/docs/reference/concerns/searchable.md +14 -1
  69. data/docs/reference/concerns/sectionable.md +7 -3
  70. data/docs/reference/concerns/versionable.md +1 -1
  71. data/docs/reference/controllers.md +33 -1
  72. data/docs/reference/environment-variables.md +3 -2
  73. data/docs/reference/formatters.md +4 -31
  74. data/docs/reference/forms.md +7 -4
  75. data/docs/reference/generators/admin-user.md +1 -1
  76. data/docs/reference/generators/audit-log.md +12 -13
  77. data/docs/reference/generators/install.md +23 -11
  78. data/docs/reference/generators/public-accounts.md +159 -0
  79. data/docs/reference/generators/scaffold.md +28 -9
  80. data/docs/reference/generators/security-headers.md +5 -4
  81. data/docs/reference/generators/taxonomies.md +1 -1
  82. data/docs/reference/icons.md +5 -3
  83. data/docs/reference/index.md +10 -7
  84. data/docs/reference/models/current.md +5 -3
  85. data/docs/reference/models/page-version.md +1 -1
  86. data/docs/reference/models/user.md +7 -8
  87. data/docs/reference/uploaders.md +25 -0
  88. data/docs/reference/validators.md +1 -1
  89. data/docs/security.md +30 -9
  90. data/docs/what-is-trek.md +4 -4
  91. data/lib/generators/trek/USAGE +1 -1
  92. data/lib/generators/trek/audit_log_generator.rb +62 -0
  93. data/lib/generators/trek/helpers.rb +17 -0
  94. data/lib/generators/trek/install/admin/routes_generator.rb +2 -1
  95. data/lib/generators/trek/install/admin/security_check_generator.rb +36 -0
  96. data/lib/generators/trek/install/admin/users_generator.rb +6 -1
  97. data/lib/generators/trek/install/audit_log_generator.rb +9 -45
  98. data/lib/generators/trek/install/ci_generator.rb +19 -0
  99. data/lib/generators/trek/install/good_job_generator.rb +161 -0
  100. data/lib/generators/trek/install/model_specs_generator.rb +3 -3
  101. data/lib/generators/trek/install/postcss_generator.rb +1 -2
  102. data/lib/generators/trek/install/ruby_generator.rb +1 -1
  103. data/lib/generators/trek/install/scalingo_generator.rb +7 -45
  104. data/lib/generators/trek/install/translations_generator.rb +1 -2
  105. data/lib/generators/trek/install_generator.rb +18 -2
  106. data/lib/generators/trek/public_accounts_generator.rb +294 -0
  107. data/lib/generators/trek/scaffold_generator.rb +22 -21
  108. data/lib/generators/trek/scalingo_generator.rb +67 -0
  109. data/lib/generators/trek/templates/.gitlab-ci.yml +10 -1
  110. data/lib/generators/trek/templates/AGENTS.md +2 -2
  111. data/lib/generators/trek/templates/Makefile +1 -0
  112. data/lib/generators/trek/templates/config/i18n-tasks.yml.erb +0 -8
  113. data/lib/generators/trek/templates/controllers/admin/password_renewals_controller.rb +11 -0
  114. data/lib/generators/trek/templates/controllers/admin/security_checks_controller.rb +11 -0
  115. data/lib/generators/trek/templates/controllers/admin/user_sessions_controller.rb +1 -1
  116. data/lib/generators/trek/templates/controllers/admin/users_controller.rb +0 -8
  117. data/lib/generators/trek/templates/controllers/concerns/trek/authentication.rb +13 -4
  118. data/lib/generators/trek/templates/initializers/content_security_policy.rb +3 -2
  119. data/lib/generators/trek/templates/initializers/good_job.rb.tt +40 -0
  120. data/lib/generators/trek/templates/jobs/mail_delivery_job.rb +6 -0
  121. data/lib/generators/trek/templates/layouts/admin.html.slim +10 -0
  122. data/lib/generators/trek/templates/mailers/admin_mailer.rb +4 -2
  123. data/lib/generators/trek/templates/package.json.tt +1 -1
  124. data/lib/generators/trek/templates/policies/security_check.rb +4 -0
  125. data/lib/generators/trek/templates/public_accounts/api/authentication.rb.tt +76 -0
  126. data/lib/generators/trek/templates/public_accounts/api/controllers/accounts_controller.rb.tt +22 -0
  127. data/lib/generators/trek/templates/public_accounts/api/controllers/active_sessions_controller.rb.tt +21 -0
  128. data/lib/generators/trek/templates/public_accounts/api/controllers/base_controller.rb.tt +71 -0
  129. data/lib/generators/trek/templates/public_accounts/api/controllers/email_changes_controller.rb.tt +55 -0
  130. data/lib/generators/trek/templates/public_accounts/api/controllers/email_confirmations_controller.rb.tt +19 -0
  131. data/lib/generators/trek/templates/public_accounts/api/controllers/password_resets_controller.rb.tt +28 -0
  132. data/lib/generators/trek/templates/public_accounts/api/controllers/passwords_controller.rb.tt +17 -0
  133. data/lib/generators/trek/templates/public_accounts/api/controllers/registrations_controller.rb.tt +17 -0
  134. data/lib/generators/trek/templates/public_accounts/api/controllers/sessions_controller.rb.tt +57 -0
  135. data/lib/generators/trek/templates/public_accounts/api/controllers/sign_in_codes_controller.rb.tt +20 -0
  136. data/lib/generators/trek/templates/public_accounts/jobs/sign_in_code_job.rb.tt +23 -0
  137. data/lib/generators/trek/templates/public_accounts/locales/accounts.en.yml.tt +215 -0
  138. data/lib/generators/trek/templates/public_accounts/locales/accounts.fr.yml.tt +215 -0
  139. data/lib/generators/trek/templates/public_accounts/mailers/account_mailer.rb.tt +85 -0
  140. data/lib/generators/trek/templates/public_accounts/migrations/create_accounts.rb.tt +46 -0
  141. data/lib/generators/trek/templates/public_accounts/models/account/session.rb.tt +47 -0
  142. data/lib/generators/trek/templates/public_accounts/models/account/sign_in_code.rb.tt +8 -0
  143. data/lib/generators/trek/templates/public_accounts/models/account.rb.tt +87 -0
  144. data/lib/generators/trek/templates/public_accounts/spec/api/accounts_spec.rb.tt +90 -0
  145. data/lib/generators/trek/templates/public_accounts/spec/api/sign_ins_spec.rb.tt +188 -0
  146. data/lib/generators/trek/templates/public_accounts/spec/models/account/sign_in_code_spec.rb.tt +66 -0
  147. data/lib/generators/trek/templates/public_accounts/spec/models/account_spec.rb.tt +129 -0
  148. data/lib/generators/trek/templates/public_accounts/spec/web/accounts_spec.rb.tt +126 -0
  149. data/lib/generators/trek/templates/public_accounts/spec/web/authentication_spec.rb.tt +43 -0
  150. data/lib/generators/trek/templates/public_accounts/spec/web/sign_ins_spec.rb.tt +237 -0
  151. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.html.slim.tt +10 -0
  152. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.text.erb.tt +8 -0
  153. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.html.slim.tt +13 -0
  154. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.text.erb.tt +8 -0
  155. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.html.slim.tt +9 -0
  156. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.text.erb.tt +11 -0
  157. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.html.slim.tt +11 -0
  158. data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.text.erb.tt +11 -0
  159. data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.html.slim.tt +10 -0
  160. data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.text.erb.tt +8 -0
  161. data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.html.slim.tt +10 -0
  162. data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.text.erb.tt +8 -0
  163. data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.html.slim.tt +10 -0
  164. data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.text.erb.tt +8 -0
  165. data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.html.slim.tt +13 -0
  166. data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.text.erb.tt +8 -0
  167. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.html.slim.tt +9 -0
  168. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.text.erb.tt +11 -0
  169. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.html.slim.tt +9 -0
  170. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.text.erb.tt +11 -0
  171. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.html.slim.tt +23 -0
  172. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.text.erb.tt +14 -0
  173. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.html.slim.tt +24 -0
  174. data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.text.erb.tt +14 -0
  175. data/lib/generators/trek/templates/public_accounts/web/authentication.rb.tt +97 -0
  176. data/lib/generators/trek/templates/public_accounts/web/controllers/accounts_controller.rb.tt +33 -0
  177. data/lib/generators/trek/templates/public_accounts/web/controllers/active_sessions_controller.rb.tt +15 -0
  178. data/lib/generators/trek/templates/public_accounts/web/controllers/base_controller.rb.tt +52 -0
  179. data/lib/generators/trek/templates/public_accounts/web/controllers/email_changes_controller.rb.tt +85 -0
  180. data/lib/generators/trek/templates/public_accounts/web/controllers/email_confirmations_controller.rb.tt +19 -0
  181. data/lib/generators/trek/templates/public_accounts/web/controllers/password_resets_controller.rb.tt +41 -0
  182. data/lib/generators/trek/templates/public_accounts/web/controllers/passwords_controller.rb.tt +23 -0
  183. data/lib/generators/trek/templates/public_accounts/web/controllers/registrations_controller.rb.tt +24 -0
  184. data/lib/generators/trek/templates/public_accounts/web/controllers/sessions_controller.rb.tt +85 -0
  185. data/lib/generators/trek/templates/public_accounts/web/controllers/sign_in_codes_controller.rb.tt +32 -0
  186. data/lib/generators/trek/templates/public_accounts/web/views/_flash.html.slim.tt +5 -0
  187. data/lib/generators/trek/templates/public_accounts/web/views/accounts/show.html.slim.tt +52 -0
  188. data/lib/generators/trek/templates/public_accounts/web/views/email_changes/edit.html.slim.tt +17 -0
  189. data/lib/generators/trek/templates/public_accounts/web/views/email_changes/new.html.slim.tt +15 -0
  190. data/lib/generators/trek/templates/public_accounts/web/views/password_resets/edit.html.slim.tt +29 -0
  191. data/lib/generators/trek/templates/public_accounts/web/views/password_resets/new.html.slim.tt +15 -0
  192. data/lib/generators/trek/templates/public_accounts/web/views/passwords/edit.html.slim.tt +28 -0
  193. data/lib/generators/trek/templates/public_accounts/web/views/registrations/new.html.slim.tt +30 -0
  194. data/lib/generators/trek/templates/public_accounts/web/views/sessions/new.html.slim.tt +40 -0
  195. data/lib/generators/trek/templates/public_accounts/web/views/sign_in_codes/new.html.slim.tt +17 -0
  196. data/lib/generators/trek/templates/spec/jobs/mail_delivery_job_spec.rb +11 -0
  197. data/lib/generators/trek/templates/spec/mailers/admin_mailer_spec.rb +10 -0
  198. data/lib/generators/trek/templates/spec/requests/admin/fragments_spec.rb +1 -1
  199. data/lib/generators/trek/templates/spec/requests/admin/good_job_spec.rb +34 -0
  200. data/lib/generators/trek/templates/spec/requests/admin/pages_spec.rb +12 -1
  201. data/lib/generators/trek/templates/spec/requests/admin/panels_spec.rb +16 -0
  202. data/lib/generators/trek/templates/spec/requests/admin/password_renewal_spec.rb +2 -2
  203. data/lib/generators/trek/templates/spec/requests/admin/security_checks_spec.rb +24 -0
  204. data/lib/generators/trek/templates/spec/requests/admin/sign_in_lockout_spec.rb +1 -1
  205. data/lib/generators/trek/templates/spec/requests/admin/sign_in_notices_spec.rb +33 -0
  206. data/lib/generators/trek/templates/spec/requests/admin/user_sessions_spec.rb +2 -2
  207. data/lib/generators/trek/templates/views/admin/pages/_form.html.slim +1 -1
  208. data/lib/generators/trek/templates/views/admin/security_checks/show.html.slim +9 -0
  209. data/lib/generators/trek/templates/views/admin/user_password_resets/edit.html.slim +6 -1
  210. data/lib/generators/trek/templates/views/admin/user_sessions/new.html.slim +4 -1
  211. data/lib/generators/trek/templates/views/admin/users/index.html.slim +1 -1
  212. data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.html.slim +1 -1
  213. data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.text.erb +1 -1
  214. data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.html.slim +1 -1
  215. data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.text.erb +1 -1
  216. data/lib/tasks/trek/attachments.rake +12 -0
  217. data/lib/tasks/trek/security.rake +10 -0
  218. data/lib/trek/csp_reports.rb +22 -4
  219. data/lib/trek/disposable_email_domains.rb +9 -20
  220. data/lib/trek/engine.rb +5 -0
  221. data/lib/trek/json_compatibility.rb +20 -0
  222. data/lib/trek/security_check.rb +241 -0
  223. data/lib/trek/stored_image_metadata.rb +88 -0
  224. data/lib/trek/typography_formatter.rb +35 -4
  225. data/lib/trek/version.rb +1 -1
  226. data/lib/trek.rb +2 -1
  227. data/package.json +16 -13
  228. data/postcss.config.js +0 -3
  229. data/trek.gemspec +6 -7
  230. data/vitest.config.mjs +24 -0
  231. data/yarn.lock +1667 -1010
  232. metadata +122 -33
@@ -10,7 +10,8 @@ module Trek
10
10
 
11
11
  def set_locale
12
12
  old_locale = I18n.locale
13
- I18n.locale = locale_for_request
13
+ # The default locale when no source gives one: plain I18n refuses a nil locale.
14
+ I18n.locale = locale_for_request || I18n.default_locale
14
15
 
15
16
  yield
16
17
  ensure
@@ -29,8 +30,11 @@ module Trek
29
30
  locale_from_params || locale_from_current_user || locale_from_headers
30
31
  end
31
32
 
33
+ # Only one of the app's locales: any other value (`?locale=xx`, an array) would make
34
+ # I18n.locale= raise I18n::InvalidLocale, and the request end in a 500.
32
35
  def locale_from_params
33
- params[:locale]
36
+ locale = params[:locale]
37
+ locale if I18n.locale_available?(locale)
34
38
  end
35
39
 
36
40
  def locale_from_current_user
@@ -13,14 +13,12 @@ module Trek
13
13
  def reorder
14
14
  authorize!
15
15
  objects = reorderable_objects.index_by(&:id)
16
+ ordered = reorder_params.sort_by { |_id, index| index }.filter_map { |id, _index| objects[id.to_i] }
16
17
 
17
- # Saving the positions one record at a time repeats acts_as_list's queries: Prosopite, in
18
- # development and test, would take them for an N+1
18
+ # Saving the records one at a time repeats queries, which Prosopite, in development and test, would
19
+ # take for an N+1
19
20
  without_n_plus_one_detection do
20
- reorder_params.each do |id, position|
21
- object = objects[id.to_i]
22
- object.update(position:) unless object.position == position
23
- end
21
+ model.transaction { without_list_updates { place_in_order(ordered) } }
24
22
  end
25
23
 
26
24
  redirect_to reorder_success_redirect, notice: reorder_notice
@@ -49,6 +47,26 @@ module Trek
49
47
  [:admin, *model_collection_route]
50
48
  end
51
49
 
50
+ # The JS posts the whole list in its new order. A tree ordered by closure_tree (`numeric_order`, which
51
+ # gives `append_sibling`) renumbers the siblings at each save: the first record takes the place of the
52
+ # current first one, and each other record goes after the previous one. Otherwise, each record gets its
53
+ # position from the top of the list, 1 unless acts_as_list's `top_of_list` says otherwise.
54
+ def place_in_order(ordered)
55
+ if ordered.first.respond_to?(:append_sibling)
56
+ current_first = ordered.min_by(&:position)
57
+ current_first.prepend_sibling(ordered.first) unless current_first == ordered.first
58
+ ordered.each_cons(2) { |previous, object| previous.append_sibling(object) }
59
+ else
60
+ top = ordered.first.respond_to?(:acts_as_list_top) ? ordered.first.acts_as_list_top : 0
61
+ ordered.each.with_index(top) { |object, position| object.update(position:) unless object.position == position }
62
+ end
63
+ end
64
+
65
+ # acts_as_list would shift the other records at each save, moving those already placed
66
+ def without_list_updates(&)
67
+ model.respond_to?(:acts_as_list_no_update) ? model.acts_as_list_no_update(&) : yield
68
+ end
69
+
52
70
  def without_n_plus_one_detection(&)
53
71
  defined?(Prosopite) ? Prosopite.pause(&) : yield
54
72
  end
@@ -17,6 +17,8 @@ module Trek
17
17
  # A report is a few hundred bytes; the Reporting API may group a few
18
18
  MAX_BODY = 64.kilobytes
19
19
  NEW_VIOLATIONS_PER_HOUR = 20
20
+ # AppSignal drops a tag of 256 characters or more, without a warning
21
+ TAG_LENGTH = 255
20
22
 
21
23
  # Browsers send no CSRF token; the endpoint only logs and reports, with no session nor user
22
24
  skip_forgery_protection
@@ -40,10 +42,21 @@ module Trek
40
42
  ::Appsignal.report_error(CspViolation.new(violation.to_s)) do
41
43
  ::Appsignal.set_namespace("csp")
42
44
  ::Appsignal.set_action(incident(violation))
43
- ::Appsignal.add_tags(page: violation.page, disposition: violation.disposition)
45
+ ::Appsignal.add_tags(tags(violation))
44
46
  end
45
47
  end
46
48
 
49
+ # What tells the violation apart, within its incident: where, what, and in which browser
50
+ def tags(violation)
51
+ {
52
+ page: violation.page,
53
+ disposition: violation.disposition,
54
+ source: violation.source,
55
+ sample: violation.sample,
56
+ user_agent: request.user_agent
57
+ }.compact_blank.transform_values { |value| value.truncate(TAG_LENGTH) }
58
+ end
59
+
47
60
  # AppSignal groups the violations by action: the directive and the blocked origin
48
61
  def incident(violation)
49
62
  "#{violation.directive} #{violation.blocked}"
@@ -34,7 +34,7 @@ module Trek
34
34
  end
35
35
 
36
36
  def object_params
37
- params.require(:prompt).permit(:turbo_frame_id, :color, :href, :text, :target, :title)
37
+ params.require(:prompt).permit(:turbo_frame_id, :href, :text, :target, :title)
38
38
  end
39
39
  end
40
40
  end
@@ -37,9 +37,9 @@ module Trek
37
37
  else
38
38
  format.html do
39
39
  flash.now[:alert] = t("admin.errors.message", count: @object.errors.count)
40
- render :new, status: :unprocessable_entity
40
+ render :new, status: :unprocessable_content
41
41
  end
42
- format.json { render json: @object.errors, status: :unprocessable_entity }
42
+ format.json { render json: @object.errors, status: :unprocessable_content }
43
43
  end
44
44
  end
45
45
  end
@@ -54,9 +54,9 @@ module Trek
54
54
  else
55
55
  format.html do
56
56
  flash.now[:alert] = t("admin.errors.message", count: @object.errors.count)
57
- render :edit, status: :unprocessable_entity
57
+ render :edit, status: :unprocessable_content
58
58
  end
59
- format.json { render json: @object.errors, status: :unprocessable_entity }
59
+ format.json { render json: @object.errors, status: :unprocessable_content }
60
60
  end
61
61
  end
62
62
  end
@@ -66,7 +66,7 @@ module Trek
66
66
  unless @object.destroy
67
67
  return respond_to do |format|
68
68
  format.html { redirect_back_or_to [:admin, @object], alert: @object.errors.full_messages.to_sentence }
69
- format.json { render json: @object.errors, status: :unprocessable_entity }
69
+ format.json { render json: @object.errors, status: :unprocessable_content }
70
70
  end
71
71
  end
72
72
 
@@ -3,11 +3,12 @@
3
3
  module Trek
4
4
  # Deletes the signed-in browsers past their idle timeout or lifetime (Trek::Users::SessionLifetime):
5
5
  # they can't sign anyone in anymore. Schedule it daily with the app's Active Job backend, e.g. a
6
- # GoodJob cron entry.
6
+ # GoodJob cron entry. Purges User::Session, or the session model named, e.g. a public account's
7
+ # ("Customer::Session").
7
8
  # rubocop:disable Rails/ApplicationJob -- the gem can't rely on the host app's ApplicationJob
8
9
  class PurgeExpiredSessionsJob < ActiveJob::Base
9
- def perform
10
- ::User::Session.expired.in_batches(of: 1000).delete_all
10
+ def perform(session_class = "User::Session")
11
+ session_class.constantize.expired.in_batches(of: 1000).delete_all
11
12
  end
12
13
  end
13
14
  # rubocop:enable Rails/ApplicationJob
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trek
4
+ # Deletes the sign-in codes past their validity (Trek::Users::SignInCode): they can't sign anyone in
5
+ # anymore, and each holds an email. A new code deletes the expired ones too, but nothing deletes the
6
+ # last ones requested. Schedule it daily with the app's Active Job backend, e.g. a GoodJob cron
7
+ # entry, with the code model named ("Customer::SignInCode").
8
+ # rubocop:disable Rails/ApplicationJob -- the gem can't rely on the host app's ApplicationJob
9
+ class PurgeExpiredSignInCodesJob < ActiveJob::Base
10
+ def perform(code_class)
11
+ code_class.constantize.expired.in_batches(of: 1000).delete_all
12
+ end
13
+ end
14
+ # rubocop:enable Rails/ApplicationJob
15
+ end
@@ -2,6 +2,10 @@ module Trek
2
2
  module Contentable
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ before_validation :clean_content
7
+ end
8
+
5
9
  def parsed_content
6
10
  return if content.blank?
7
11
 
@@ -20,8 +24,9 @@ module Trek
20
24
  content_document&.to_html
21
25
  end
22
26
 
27
+ # The plain text, with its blocks separated by a blank line and its hard breaks by a newline
23
28
  def content_text
24
- content_document&.text_content
29
+ content_document&.to_text
25
30
  end
26
31
 
27
32
  def prosemirror_content?
@@ -31,5 +36,14 @@ module Trek
31
36
  else false
32
37
  end
33
38
  end
39
+
40
+ private
41
+
42
+ # Before saving, removes what rendering skips: empty paragraphs, empty list items and lists left empty
43
+ # (Trek::Content::Cleaner). The content editor submits the content as a JSON string: it's stored as a
44
+ # document. Translated content is cleaned in the current locale.
45
+ def clean_content
46
+ self.content = Trek::Content.clean(parsed_content) if prosemirror_content?
47
+ end
34
48
  end
35
49
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trek
4
+ module Users
5
+ # A one-time code emailed to sign in, or to confirm a new email address: 6 digits, valid for
6
+ # `validity` (10 minutes), for `max_attempts` tries (5), then single use. Stored as an HMAC of the
7
+ # code keyed on the app's secret_key_base, never in clear. Kept by email rather than by account,
8
+ # so an email without an account gets one too: asking for a code answers the same whether or not
9
+ # the email has an account (ANSSI guide on passwords, R14), and verifying it creates the account.
10
+ #
11
+ # A new code for an email replaces its earlier ones for the same purpose. Needs the `email`,
12
+ # `purpose`, `code_digest`, `attempts` and `expires_at` columns, and the account's foreign key for
13
+ # an email change.
14
+ module SignInCode
15
+ extend ActiveSupport::Concern
16
+
17
+ PURPOSES = %w[sign_in email_change].freeze
18
+ LENGTH = 6
19
+
20
+ included do
21
+ class_attribute :validity, default: 10.minutes
22
+ class_attribute :max_attempts, default: 5
23
+
24
+ # The code in clear, on the record `issue` returns only: for the email, never stored.
25
+ attribute :code, :string
26
+
27
+ normalizes :email, with: ->(email) { email.strip.downcase }
28
+
29
+ validates :email, presence: true
30
+ validates :purpose, inclusion: { in: PURPOSES }
31
+
32
+ scope :expired, -> { where(expires_at: ...Time.current) }
33
+ end
34
+
35
+ class_methods do
36
+ # A new code for `email`, replacing the earlier ones for `purpose`. Its `code` is the one to send.
37
+ def issue(email, purpose: "sign_in", **attributes)
38
+ code = SecureRandom.random_number(10**LENGTH).to_s.rjust(LENGTH, "0")
39
+
40
+ transaction do
41
+ where(email: normalize_value_for(:email, email), purpose:).delete_all
42
+ # Codes nobody used: they can't sign anyone in anymore
43
+ expired.delete_all
44
+ create!(email:, purpose:, code_digest: code_digest(code), expires_at: validity.from_now, **attributes)
45
+ end.tap { it.code = code }
46
+ end
47
+
48
+ # The code of `email` for `purpose` when `code` matches it, used up; nil otherwise. Every try
49
+ # counts, the right one included, and a code past max_attempts can't sign in anymore.
50
+ def redeem(email, code, purpose: "sign_in", **attributes)
51
+ record = where(email: normalize_value_for(:email, email), purpose:, **attributes)
52
+ .where(expires_at: Time.current..).order(created_at: :desc).first
53
+ return unless record
54
+
55
+ # Counted in the database first, so parallel tries can't go past max_attempts
56
+ counted = where(id: record.id).where(attempts: ...max_attempts).update_all("attempts = attempts + 1") # rubocop:disable Rails/SkipsModelValidations
57
+ return if counted.zero?
58
+ return unless ActiveSupport::SecurityUtils.secure_compare(record.code_digest, code_digest(code.to_s.gsub(/\D/, "")))
59
+
60
+ # Single use: only one of parallel right tries gets it
61
+ record if where(id: record.id).delete_all == 1
62
+ end
63
+
64
+ def code_digest(code)
65
+ OpenSSL::HMAC.hexdigest("SHA256", Rails.application.key_generator.generate_key("trek/sign_in_codes"), code)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -10,7 +10,8 @@ module Trek
10
10
  #
11
11
  # While locked, signing in fails even with the right password, and the attempts don't count. A
12
12
  # successful sign-in or a new password (e.g. through a reset) unlocks it. The user is emailed
13
- # when it first locks (`sign_in_lockout_email`).
13
+ # when it first locks (`sign_in_lockout_email`), by `sign_in_lockout_mailer`'s `sign_in_locked`
14
+ # (AdminMailer).
14
15
  #
15
16
  # Needs the `failed_sign_in_count` and `sign_in_locked_until` columns.
16
17
  module SignInLockout
@@ -21,6 +22,7 @@ module Trek
21
22
  class_attribute :sign_in_lockout_duration, default: 1.minute
22
23
  class_attribute :sign_in_lockout_max, default: 1.hour
23
24
  class_attribute :sign_in_lockout_email, default: true
25
+ class_attribute :sign_in_lockout_mailer, default: "AdminMailer"
24
26
 
25
27
  before_save :unlock_sign_in, if: :password_digest_changed?
26
28
  end
@@ -41,7 +43,7 @@ module Trek
41
43
  update_column(:sign_in_locked_until, sign_in_lockout_delay.from_now) # rubocop:disable Rails/SkipsModelValidations
42
44
  # Once per series of failures, not on each failure that extends it
43
45
  first_lockout = failed_sign_in_count == sign_in_lockout_after
44
- AdminMailer.sign_in_locked(self).deliver_later if sign_in_lockout_email && first_lockout
46
+ sign_in_lockout_mailer.constantize.sign_in_locked(self).deliver_later if sign_in_lockout_email && first_lockout
45
47
  end
46
48
 
47
49
  def record_successful_sign_in!
@@ -0,0 +1,8 @@
1
+ module Trek
2
+ # The security check lists the app's weak spots: admins only (Admin::SecurityChecksController)
3
+ class SecurityCheckPolicy < Trek::ApplicationPolicy
4
+ def show?
5
+ user.admin?
6
+ end
7
+ end
8
+ end
@@ -1,28 +1,90 @@
1
1
  require "image_processing/vips"
2
2
 
3
3
  class ImageUploader < Shrine
4
+ TYPES = %w[image/jpeg image/png image/webp].freeze
5
+ # libvips' default (75) visibly degrades a photo
6
+ QUALITY = 90
7
+ # The sizes (thumbnails…), in WebP whatever the original's type
8
+ SIZE_QUALITY = 80
9
+
4
10
  plugin :blurhash
5
11
  plugin :determine_mime_type
6
12
  plugin :remote_url, max_size: nil
7
13
  plugin :store_dimensions
8
14
  plugin :validation_helpers
9
- # plugin :derivation_endpoint,
10
- # secret_key: Rails.application.credentials.dig(:shrine, :derivation_endpoint, :secret_key),
11
- # prefix: "derivations/image",
12
- # upload: true,
13
- # upload_location: lambda {
14
- # # e.g. "derivatives/9a7d1bfdad24a76f9cfaff137fe1b5c7/thumbnail-1000-800"
15
- # ["derivatives", File.basename(source.id, ".*"), [name, *args].join("-")].join("/")
16
- # }
17
- #
18
- # derivation :thumbnail do |file, width, height|
19
- # ImageProcessing::Vips
20
- # .source(file)
21
- # .resize_to_limit!(width.to_i, height.to_i)
22
- # end
15
+ # The sizes Trek::ThumbnailHelper asks for (`derivation_url(:thumbnail, width, height)`), made on
16
+ # their first request, then stored and served from the store. The secret signs their URLs: from
17
+ # the credentials trek:install:attachments writes, or derived from secret_key_base.
18
+ plugin :derivation_endpoint,
19
+ secret_key: Rails.application.credentials.dig(:shrine, :derivation_endpoint, :secret_key) ||
20
+ Rails.application.key_generator.generate_key("shrine/derivation_endpoint"),
21
+ prefix: "derivations/image",
22
+ upload: true,
23
+ upload_location: lambda {
24
+ # e.g. "derivatives/9a7d1bfdad24a76f9cfaff137fe1b5c7/thumbnail-1000-800"
25
+ [shrine_class.sizes_location(source.id), [name, *args].join("-")].join("/")
26
+ }
27
+
28
+ derivation :thumbnail do |file, width, height|
29
+ ImageProcessing::Vips
30
+ .source(file)
31
+ .convert("webp")
32
+ .saver(Q: SIZE_QUALITY, **shrine_class.metadata_saver_options)
33
+ .resize_to_limit!(width.to_i, height.to_i)
34
+ end
23
35
 
24
36
  Attacher.validate do
25
37
  validate_max_size 20.megabytes
26
- validate_max_dimensions [8000, 8000] if validate_mime_type %w[image/jpeg image/png]
38
+ validate_max_dimensions [8000, 8000] if validate_mime_type TYPES
39
+ end
40
+
41
+ # Shrine deletes an image when its record is destroyed, or the image replaced or removed, but
42
+ # not the sizes it stored: they go with it, as they're published too.
43
+ class Attacher
44
+ def destroy
45
+ super
46
+ file.storage.delete_prefixed("#{shrine_class.sizes_location(file.id)}/") if file
47
+ end
48
+ end
49
+
50
+ # Where the sizes of the image stored as `id` are
51
+ def self.sizes_location(id)
52
+ "derivatives/#{File.basename(id, ".*")}"
53
+ end
54
+
55
+ # Strips the metadata libvips writes back, keeping the color profile (libvips 8.15 or later; older
56
+ # ones strip that too)
57
+ def self.metadata_saver_options
58
+ Vips.at_least_libvips?(8, 15) ? { keep: :icc } : { strip: true }
59
+ end
60
+
61
+ # A photo's metadata would be published with it: its EXIF (the GPS location, the camera's serial
62
+ # number…), XMP and IPTC. It is stripped from a new file before it is stored, in the cache too,
63
+ # by re-encoding the image after applying its orientation, keeping its color profile. A file
64
+ # promoted from the cache is stored as it is.
65
+ def upload(io, **options)
66
+ return super if io.is_a?(Shrine::UploadedFile)
67
+
68
+ mime_type = self.class.determine_mime_type(io)
69
+ return super unless TYPES.include?(mime_type)
70
+
71
+ filename = extract_filename(io)
72
+ stripped = Shrine.with_file(io) { |file| strip_metadata(file, mime_type) }
73
+ io.close if options.fetch(:close, true)
74
+ metadata = { "filename" => filename }.merge(options[:metadata].is_a?(Hash) ? options[:metadata] : {})
75
+ super(stripped, **options, metadata:, delete: true)
76
+ end
77
+
78
+ private
79
+
80
+ def strip_metadata(file, mime_type)
81
+ format = mime_type.delete_prefix("image/").sub("jpeg", "jpg")
82
+ ImageProcessing::Vips
83
+ .source(file)
84
+ # Every frame of an animated WebP, not only the first
85
+ .loader(**(format == "webp" ? { n: -1 } : {}))
86
+ .convert(format)
87
+ .saver(**self.class.metadata_saver_options, **(format == "png" ? {} : { Q: QUALITY }))
88
+ .call
27
89
  end
28
90
  end
@@ -20,10 +20,8 @@ h1.c-trek--layout-panel-title = t("admin.panels.links.form.title")
20
20
  = f.select :target,
21
21
  options_for_select(@object.class::TARGETS, @object.target)
22
22
 
23
- / TODO: re-enable when tiptap supports `title`
24
- / see https://github.com/ueberdosis/tiptap/pull/2204
25
- / = f.group :title
26
- / = f.text_field :title
23
+ = f.group :title
24
+ = f.text_field :title
27
25
 
28
26
  = render Trek::Form::ActionsComponent.new do |c|
29
27
  ruby:
@@ -7,7 +7,6 @@
7
7
  data-trek--form--content-editor-target="contentInserter"
8
8
  )
9
9
  prompts-block(
10
- color=@object.color
11
10
  href=@object.href
12
11
  target=@object.target
13
12
  text=@object.text
@@ -6,23 +6,23 @@ en:
6
6
  alt: Alternative text
7
7
  credit: Credit
8
8
  file: Image
9
- size: Size
10
- text: Caption
9
+ size: Display width
10
+ text: Image caption
11
11
  trek/content_editor/link:
12
- href: Address
13
- target: Open in
14
- text: Text
12
+ href: Link to
13
+ target: Open this link in…
14
+ text: Link text
15
15
  title: Title
16
16
  trek/content_editor/prompt:
17
- href: Address
18
- target: Open in
19
- text: Text
17
+ href: Link to
18
+ target: Open this link in…
19
+ text: Button text
20
20
  title: Title
21
21
  options:
22
22
  trek/content_editor/image:
23
23
  size:
24
24
  default: As wide as the text
25
- larger: Wider
25
+ larger: Extends past the sides
26
26
  activerecord:
27
27
  attributes:
28
28
  audit_log:
@@ -94,6 +94,7 @@ en:
94
94
  browse: Browse…
95
95
  cancel: Cancel
96
96
  delete: Delete
97
+ download_original: Download original
97
98
  edit: Edit
98
99
  logout: Log out
99
100
  manage: Manage
@@ -178,6 +179,7 @@ en:
178
179
  intro: Fragment
179
180
  update:
180
181
  success: Fragment updated
182
+ jobs: Background jobs
181
183
  metadata: Metadata
182
184
  navigation: Navigation
183
185
  pages:
@@ -228,6 +230,9 @@ en:
228
230
  success: Passkey added
229
231
  destroy:
230
232
  success: Passkey deleted
233
+ password_renewals:
234
+ create:
235
+ success: Every user will have to choose a new password
231
236
  passwords_mailer:
232
237
  reset:
233
238
  subject: Reset your password
@@ -242,6 +247,10 @@ en:
242
247
  success: Section added
243
248
  update:
244
249
  success: Section updated
250
+ security_checks:
251
+ show:
252
+ intro: Checked %{time}, with Trek %{trek} and Rails %{rails}, against the recommended settings
253
+ title: Security
245
254
  seo: SEO
246
255
  settings: Settings
247
256
  user_password_resets:
@@ -289,8 +298,6 @@ en:
289
298
  last_used: Last used %{time} ago
290
299
  never_used: Never used
291
300
  title: Sign in without a password
292
- require_password_renewal:
293
- success: Every user will have to choose a new password
294
301
  search:
295
302
  alert: No user found with email
296
303
  sessions:
@@ -324,6 +331,18 @@ en:
324
331
  errors:
325
332
  messages:
326
333
  disposable: is a disposable email address, use a permanent one
334
+ helpers:
335
+ hint:
336
+ trek/content_editor/image:
337
+ alt: For accessibility and SEO, describe what the image shows, without repeating the caption
338
+ credit: The author's name
339
+ text: Shown under the image
340
+ trek/content_editor/link:
341
+ href: Choose a page or enter a URL
342
+ title: Shown on hover, useful for accessibility and SEO
343
+ trek/content_editor/prompt:
344
+ href: Choose a page or enter a URL
345
+ title: Shown on hover, useful for accessibility and SEO
327
346
  notices:
328
347
  authentication_needed: Please log in
329
348
  password_renewal_needed: "Your password must be renewed: choose a new one"
@@ -369,6 +388,73 @@ en:
369
388
  search_component:
370
389
  reset: Reset search
371
390
  search: Search
391
+ security_check:
392
+ admin_allowed_ips:
393
+ set: The admin is restricted to %{count} IP addresses or ranges (`ADMIN_ALLOWED_IPS`)
394
+ unset: The admin is open to every IP address (`ADMIN_ALLOWED_IPS` isn't set)
395
+ admins:
396
+ count: "%{privileged} users can sign in to the admin, %{admins} of them with the admin role"
397
+ audit_log:
398
+ not_installed: No audit log (`bin/rails g trek:audit_log`)
399
+ not_purged: The audit log's purge isn't scheduled (`Trek::AuditLog::PurgeJob` in GoodJob's cron)
400
+ purge_unknown: The audit log's purge can't be checked with this job backend (`Trek::AuditLog::PurgeJob`)
401
+ purged: Audit log installed, its purge scheduled
402
+ cache_store:
403
+ per_process: The cache store (`%{store}`) isn't shared by the app's processes, which the sign-in limits need
404
+ per_server: The cache store (`%{store}`) isn't shared by the app's servers, which the sign-in limits need
405
+ shared: Cache store shared by the app's processes (`%{store}`)
406
+ content_security_policy:
407
+ enforced: Content Security Policy enforced
408
+ none: No Content Security Policy (`bin/rails g trek:install:security_headers`)
409
+ report_only: The Content Security Policy only reports violations (`content_security_policy_report_only`)
410
+ cron:
411
+ disabled: "GoodJob's cron is off (`enable_cron`): scheduled jobs never run"
412
+ enabled: GoodJob's cron is on
413
+ other_backend: The scheduled jobs can't be checked with this job backend (`%{adapter}`)
414
+ dormant:
415
+ none: Every admin user signed in less than %{duration} ago
416
+ some:
417
+ one: "%{count} admin user hasn't signed in for %{duration}"
418
+ other: "%{count} admin users haven't signed in for %{duration}"
419
+ encryption:
420
+ configured: Active Record Encryption keys configured
421
+ not_configured: No Active Record Encryption keys (`bin/rails db:encryption:init`)
422
+ header: |-
423
+ Security check of %{app}, %{environment}, %{time}
424
+ Trek %{trek}, Rails %{rails}, Ruby %{ruby}
425
+ https:
426
+ no_hsts: HTTPS enforced, but without HSTS (`config.ssl_options`)
427
+ "off": HTTPS isn't enforced (`config.force_ssl`)
428
+ "on": HTTPS enforced, with HSTS for %{duration}
429
+ passkeys:
430
+ all: Every admin user has a passkey
431
+ password_only:
432
+ one: "%{count} admin user signs in with a password only, no passkey"
433
+ other: "%{count} admin users sign in with a password only, no passkey"
434
+ password_expiry:
435
+ after: Passwords expire after %{duration}
436
+ never: Passwords never expire (`password_expires_after`)
437
+ remote_ip:
438
+ cdn: "Rails sees your IP address as %{ip}, but the CDN forwards %{forwarded}: add its ranges to `trusted_proxies`"
439
+ seen: "Rails sees your IP address as %{ip}: check it's yours, not your network's proxy or the CDN's"
440
+ request_https:
441
+ http: "This page reached the app over HTTP: the proxy in front of it must forward the scheme (`X-Forwarded-Proto`)"
442
+ https: This page reached the app over HTTPS
443
+ session_purge:
444
+ not_scheduled: The expired sessions' purge isn't scheduled (`Trek::PurgeExpiredSessionsJob` in GoodJob's cron)
445
+ scheduled: Expired sessions' purge scheduled
446
+ unknown: The expired sessions' purge can't be checked with this job backend (`Trek::PurgeExpiredSessionsJob`)
447
+ summary: "%{ok} ok, %{info} for information, %{warning} warnings, %{failure} failures"
448
+ trusted_proxies:
449
+ default: "No trusted proxies besides the private networks (`trusted_proxies`): set them when a CDN is in front of the app"
450
+ set: "%{count} trusted proxies (`trusted_proxies`)"
451
+ security_check_component:
452
+ docs: Docs
453
+ statuses:
454
+ failure: Failure
455
+ info: Information
456
+ ok: OK
457
+ warning: Warning
372
458
  user_agents:
373
459
  browser_on_system: "%{browser} on %{system}"
374
460
  unknown: Unknown browser