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
@@ -15,17 +15,38 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins through the API", type: :r
15
15
  end
16
16
 
17
17
  def sign_in(email)
18
+ <% if invite_only? -%>
19
+ # Accounts are the app's to create: signing in never does
20
+ <%= class_name %>.find_or_create_by!(email:)
21
+ <% end -%>
18
22
  ask_for_code(email)
19
23
  post <%= route_prefix %>_sign_in_code_path, params: { email:, otp: emailed_code }, as: :json
20
24
  response.parsed_body["token"]
21
25
  end
22
26
 
27
+ <% if invite_only? -%>
28
+ it "emails no code to an email without an account, with the same answer as for an account", :aggregate_failures do
29
+ <%= class_name %>.create!(email: "ada@example.com")
30
+
31
+ expect(ask_for_code("grace@example.com")).to eq(ask_for_code("ada@example.com")).and eq(202)
32
+ expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[ada@example.com])
33
+ end
34
+
35
+ it "creates no account for an email without one", :aggregate_failures do
36
+ ask_for_code("grace@example.com")
37
+
38
+ expect { post <%= route_prefix %>_sign_in_code_path, params: { email: "grace@example.com", otp: "123456" }, as: :json }
39
+ .not_to change(<%= class_name %>, :count)
40
+ expect(response).to have_http_status(:unauthorized)
41
+ end
42
+ <% else -%>
23
43
  it "emails a code to an email without an account, with the same answer as for an account", :aggregate_failures do
24
44
  <%= class_name %>.create!(email: "ada@example.com")
25
45
 
26
46
  expect(ask_for_code("grace@example.com")).to eq(ask_for_code("ada@example.com")).and eq(202)
27
47
  expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[grace@example.com ada@example.com])
28
48
  end
49
+ <% end -%>
29
50
 
30
51
  it "refuses an invalid email", :aggregate_failures do
31
52
  post <%= route_prefix %>_sign_in_path, params: { email: "not an email" }, as: :json
@@ -34,6 +55,14 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins through the API", type: :r
34
55
  expect(response.parsed_body["error"]).to eq("invalid_email")
35
56
  end
36
57
 
58
+ <% if invite_only? -%>
59
+ it "signs in to the email's account and answers with a token", :aggregate_failures do
60
+ token = sign_in("ada@example.com")
61
+
62
+ expect(response).to have_http_status(:created)
63
+ expect(token).to be_present
64
+ end
65
+ <% else -%>
37
66
  it "creates the account and answers with a token", :aggregate_failures do
38
67
  token = nil
39
68
 
@@ -41,6 +70,7 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins through the API", type: :r
41
70
  expect(response).to have_http_status(:created)
42
71
  expect(token).to be_present
43
72
  end
73
+ <% end -%>
44
74
 
45
75
  it "refuses a wrong code", :aggregate_failures do
46
76
  ask_for_code("ada@example.com")
@@ -38,6 +38,16 @@ RSpec.describe <%= class_name %>::SignInCode, type: :model do
38
38
  end
39
39
  end
40
40
 
41
+ # Each holds an email, and nothing else deletes the last ones requested
42
+ it "is deleted by the nightly purge once expired" do
43
+ current = described_class.issue("grace@example.com")
44
+ described_class.issue("ada@example.com").update!(expires_at: 1.minute.ago)
45
+
46
+ Trek::PurgeExpiredSignInCodesJob.perform_now("<%= class_name %>::SignInCode")
47
+
48
+ expect(described_class.all).to contain_exactly(current)
49
+ end
50
+
41
51
  it "stops working after too many wrong tries, even with the right code" do
42
52
  code = described_class.issue("ada@example.com").code
43
53
  wrong = code.succ.rjust(6, "0")[-6..]
@@ -20,7 +20,15 @@ RSpec.describe <%= class_name %>, type: :model do
20
20
 
21
21
  expect(described_class.new(email: "ADA@example.com")).not_to be_valid
22
22
  end
23
- <% if code? -%>
23
+ <% if invite_only? -%>
24
+
25
+ it "finds the account an email signs in to, whatever its case, without creating any", :aggregate_failures do
26
+ <%= file_name %> = create_<%= file_name %>("ada@example.com")
27
+
28
+ expect(described_class.for_sign_in("Ada@example.com")).to eq(<%= file_name %>)
29
+ expect(described_class.for_sign_in("grace@example.com")).to be_nil
30
+ end
31
+ <% elsif code? -%>
24
32
 
25
33
  it "creates the account of a verified email once" do
26
34
  first = described_class.for_verified_email!("ada@example.com")
@@ -44,6 +52,18 @@ RSpec.describe <%= class_name %>, type: :model do
44
52
 
45
53
  expect { <%= file_name %>.destroy }.to change(<%= class_name %>::Session, :count).by(-1)
46
54
  end
55
+ <% if code? -%>
56
+
57
+ it "deletes the sign-in codes waiting for its email with it, which it doesn't own" do
58
+ <%= file_name %> = create_<%= file_name %>("ada@example.com")
59
+ <%= class_name %>::SignInCode.issue("ada@example.com")
60
+ other = <%= class_name %>::SignInCode.issue("grace@example.com")
61
+
62
+ <%= file_name %>.destroy
63
+
64
+ expect(<%= class_name %>::SignInCode.all).to contain_exactly(other)
65
+ end
66
+ <% end -%>
47
67
 
48
68
  it "loses its expired sessions to the daily purge" do
49
69
  <%= file_name %> = create_<%= file_name %>("ada@example.com")
@@ -19,6 +19,10 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins", type: :request do
19
19
  end
20
20
 
21
21
  def sign_in(email)
22
+ <% if invite_only? -%>
23
+ # Accounts are the app's to create: signing in never does
24
+ <%= class_name %>.find_or_create_by!(email:)
25
+ <% end -%>
22
26
  ask_for_code(email)
23
27
  post <%= route_prefix %>_sign_in_code_path, params: { otp: emailed_code }
24
28
  end
@@ -29,12 +33,28 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins", type: :request do
29
33
  expect(response).to have_http_status(:ok)
30
34
  end
31
35
 
36
+ <% if invite_only? -%>
37
+ it "emails no code to an email without an account, with the same answer as for an account", :aggregate_failures do
38
+ <%= class_name %>.create!(email: "ada@example.com")
39
+
40
+ expect(answer_to_code_request("grace@example.com")).to eq(answer_to_code_request("ada@example.com"))
41
+ expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[ada@example.com])
42
+ end
43
+
44
+ it "creates no account for an email without one", :aggregate_failures do
45
+ ask_for_code("grace@example.com")
46
+
47
+ expect { post <%= route_prefix %>_sign_in_code_path, params: { otp: "123456" } }.not_to change(<%= class_name %>, :count)
48
+ expect(response).to have_http_status(:unprocessable_content)
49
+ end
50
+ <% else -%>
32
51
  it "emails a code to an email without an account, with the same answer as for an account", :aggregate_failures do
33
52
  <%= class_name %>.create!(email: "ada@example.com")
34
53
 
35
54
  expect(answer_to_code_request("grace@example.com")).to eq(answer_to_code_request("ada@example.com"))
36
55
  expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[grace@example.com ada@example.com])
37
56
  end
57
+ <% end -%>
38
58
 
39
59
  it "refuses an invalid email" do
40
60
  post <%= route_prefix %>_sign_in_path, params: { email: "not an email" }
@@ -42,6 +62,7 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins", type: :request do
42
62
  expect(response).to have_http_status(:unprocessable_content)
43
63
  end
44
64
 
65
+ <% unless invite_only? -%>
45
66
  it "creates the account and signs in with the emailed code", :aggregate_failures do
46
67
  ask_for_code("grace@example.com")
47
68
 
@@ -50,6 +71,7 @@ RSpec.describe "<%= human_name.pluralize %>' sign-ins", type: :request do
50
71
  expect(cookies[<%= accounts_module %>::Authentication::COOKIE]).to be_present
51
72
  end
52
73
 
74
+ <% end -%>
53
75
  it "signs in to the email's account", :aggregate_failures do
54
76
  <%= file_name %> = <%= class_name %>.create!(email: "ada@example.com")
55
77
  ask_for_code("Ada@Example.com")
@@ -25,7 +25,7 @@ module <%= accounts_module %>::Authentication
25
25
  def require_<%= file_name %>
26
26
  return if resume_<%= file_name %>_session
27
27
 
28
- session[:return_to_after_<%= file_name %>_sign_in] = request.url if request.get?
28
+ session[:return_to_after_<%= file_name %>_sign_in] = request.url if request.get? || request.head?
29
29
  redirect_to <%= route_prefix %>_sign_in_path, notice: t("<%= accounts_path %>.authentication.sign_in_needed")
30
30
  end
31
31
 
@@ -1,4 +1,7 @@
1
- <% if code? -%>
1
+ <% if invite_only? -%>
2
+ # Step 2 of signing in: the emailed code, which signs in to the email's account; an email without
3
+ # one was sent no code. Also signs out.
4
+ <% elsif code? -%>
2
5
  # Step 2 of signing in: the emailed code, which signs in to the email's account, created if it had
3
6
  # none. Also signs out.
4
7
  <% else -%>
@@ -16,8 +19,12 @@ class <%= controllers_module %>::SessionsController < <%= controllers_module %>:
16
19
  def create
17
20
  return render_sign_in_form t("<%= accounts_path %>.authentication.rate_limited"), status: :too_many_requests if too_many_failed_sign_ins_from_ip?
18
21
 
22
+ <% if invite_only? -%>
23
+ if <%= class_name %>::SignInCode.redeem(@email, params[:otp]) && (@<%= file_name %> = <%= class_name %>.for_sign_in(@email))
24
+ <% else -%>
19
25
  if <%= class_name %>::SignInCode.redeem(@email, params[:otp])
20
26
  @<%= file_name %> = <%= class_name %>.for_verified_email!(@email)
27
+ <% end -%>
21
28
  session.delete(:<%= file_name %>_sign_in_email)
22
29
  url = after_<%= file_name %>_sign_in_url
23
30
  start_<%= file_name %>_session_for @<%= file_name %>
@@ -1,5 +1,10 @@
1
+ <% if invite_only? -%>
2
+ # Step 1 of signing in: an email, to which a code is sent if it has an account. Whether or not it
3
+ # has one, the answer is the same.
4
+ <% else -%>
1
5
  # Step 1 of signing in: an email, to which a code is sent. Whether or not it has an account, the
2
6
  # answer is the same; the code creates the account on its first use.
7
+ <% end -%>
3
8
  class <%= controllers_module %>::SignInCodesController < <%= controllers_module %>::BaseController
4
9
  rate_limit to: 10, within: 15.minutes, only: :create, with: -> { render_email_form t("<%= accounts_path %>.authentication.rate_limited"), status: :too_many_requests }
5
10
 
@@ -2,9 +2,12 @@
2
2
 
3
3
  h1 = t(".title")
4
4
 
5
+ / Without Turbo: password managers offer to save the password in an element they add to the
6
+ next page's body, which a Turbo visit would replace
5
7
  = form_with model: @<%= file_name %>,
6
8
  url: <%= route_prefix %>_password_reset_path,
7
- method: :patch do |f|
9
+ method: :patch,
10
+ data: { turbo: false } do |f|
8
11
  - if @<%= file_name %>.errors.any?
9
12
  ul(role="alert")
10
13
  - @<%= file_name %>.errors.full_messages.each do |message|
@@ -8,7 +8,10 @@ ruby:
8
8
  }
9
9
  p = t(".intro", **rules)
10
10
 
11
- = form_with model: @<%= file_name %>, url: <%= route_prefix %>_password_path, method: :patch do |f|
11
+ / Without Turbo: password managers offer to save the password in an element they add to the
12
+ next page's body, which a Turbo visit would replace
13
+ = form_with model: @<%= file_name %>, url: <%= route_prefix %>_password_path, method: :patch,
14
+ data: { turbo: false } do |f|
12
15
  - if @<%= file_name %>.errors.any?
13
16
  ul(role="alert")
14
17
  - @<%= file_name %>.errors.full_messages.each do |message|
@@ -2,7 +2,9 @@
2
2
 
3
3
  h1 = t(".title")
4
4
 
5
- = form_with model: @<%= file_name %>, url: <%= route_prefix %>_sign_up_path do |f|
5
+ / Without Turbo: password managers offer to save the password in an element they add to the
6
+ next page's body, which a Turbo visit would replace
7
+ = form_with model: @<%= file_name %>, url: <%= route_prefix %>_sign_up_path, data: { turbo: false } do |f|
6
8
  - if @<%= file_name %>.errors.any?
7
9
  ul(role="alert")
8
10
  - @<%= file_name %>.errors.full_messages.each do |message|
@@ -19,7 +19,9 @@ p = t(".intro", email: @email, validity:)
19
19
  p = link_to t(".other_email"), <%= route_prefix %>_sign_in_path
20
20
  <% else -%>
21
21
 
22
- = form_with url: <%= route_prefix %>_sign_in_path do |f|
22
+ / Without Turbo: password managers offer to save the password in an element they add to the
23
+ next page's body, which a Turbo visit would replace
24
+ = form_with url: <%= route_prefix %>_sign_in_path, data: { turbo: false } do |f|
23
25
  = f.label :email, <%= class_name %>.human_attribute_name(:email)
24
26
  = f.email_field :email,
25
27
  value: @email,
@@ -43,6 +43,17 @@ RSpec.describe "Admin pages", type: :request do
43
43
  expect(response).to have_http_status(:ok)
44
44
  end
45
45
 
46
+ it "renders the edit form of a page made of sections, without versions" do
47
+ page = pages.create(title: "Our offices", sectioned: true)
48
+ # Sections are the app's own Section model, which Trek doesn't generate: none here
49
+ page.define_singleton_method(:sections_instances) { [] }
50
+ allow(Page).to receive(:find).and_return(page)
51
+
52
+ get edit_admin_page_path(page)
53
+
54
+ expect(response).to have_http_status(:ok)
55
+ end
56
+
46
57
  it "updates a page" do
47
58
  patch admin_page_path(pages.team), params: { page: { title: "The team" } }
48
59
 
@@ -0,0 +1,24 @@
1
+ require "rails_helper"
2
+
3
+ RSpec.describe "Admin security check", type: :request do
4
+ def sign_in(user)
5
+ post admin_user_session_path, params: { email: user.email, password: users.password }
6
+ end
7
+
8
+ it "checks the settings, and this request, for an admin" do
9
+ sign_in(users.create(role: :admin))
10
+
11
+ get admin_security_check_path
12
+
13
+ # The test request comes over HTTP, as no proxy forwards the scheme
14
+ expect(response.body).to include("X-Forwarded-Proto")
15
+ end
16
+
17
+ it "is closed to an editor" do
18
+ sign_in(users.create(role: :editor))
19
+
20
+ get admin_security_check_path
21
+
22
+ expect(response).to redirect_to(admin_root_path)
23
+ end
24
+ end
@@ -16,6 +16,12 @@ RSpec.describe "Admin sign-in notices", type: :request do
16
16
  delete admin_user_session_path
17
17
  end
18
18
 
19
+ # A cookie jar reading, or writing, the browser's trek_known_users cookie as the app does
20
+ def cookie_jar(value = nil)
21
+ env = Rails.application.env_config.merge("HTTP_COOKIE" => value ? "trek_known_users=#{Rack::Utils.escape(value)}" : "")
22
+ ActionDispatch::Request.new(env).cookie_jar
23
+ end
24
+
19
25
  describe "the previous sign-in" do
20
26
  it "keeps the sign-in notice short" do
21
27
  sign_in(editor)
@@ -84,6 +90,33 @@ RSpec.describe "Admin sign-in notices", type: :request do
84
90
  expect { sign_in(editor) }.to have_enqueued_mail(AdminMailer, :new_sign_in)
85
91
  end
86
92
 
93
+ it "is sent from a browser that didn't sign in for 13 months" do
94
+ sign_in(editor)
95
+ sign_out
96
+ travel 13.months + 1.day
97
+
98
+ expect { sign_in(editor) }.to have_enqueued_mail(AdminMailer, :new_sign_in)
99
+ end
100
+
101
+ # The browser's users can't read who else signed in from it, e.g. on a shared computer
102
+ it "remembers the browser's users in an encrypted cookie" do
103
+ sign_in(editor)
104
+
105
+ expect(cookie_jar(cookies["trek_known_users"]).encrypted[:trek_known_users]).to eq [editor.id]
106
+ end
107
+
108
+ # The cookie was signed only before it was encrypted. Its value isn't escaped: the test's cookie
109
+ # jar sends it as it is, and the app wouldn't unescape the "=" a user id of two digits or more
110
+ # pads it with.
111
+ it "isn't sent from a browser remembered by its signed cookie" do
112
+ sign_in(editor)
113
+ sign_out
114
+ jar = cookie_jar.tap { |writer| writer.signed[:trek_known_users] = [editor.id] }
115
+ cookies["trek_known_users"] = jar["trek_known_users"]
116
+
117
+ expect { sign_in(editor) }.not_to have_enqueued_mail(AdminMailer, :new_sign_in)
118
+ end
119
+
87
120
  it "isn't sent when the app turns it off" do
88
121
  allow(User).to receive(:new_sign_in_email).and_return(false)
89
122
  sign_in(editor)
@@ -36,7 +36,7 @@
36
36
  = f.image_field :image
37
37
 
38
38
  - if @object.sectioned?
39
- - vf.object.sections_instances.each do |section|
39
+ - @object.sections_instances.each do |section|
40
40
  = render Trek::Form::FieldsetComponent.new( \
41
41
  title: "#{section.position}. #{section.model_name.human}",
42
42
  classnames: "section",
@@ -0,0 +1,9 @@
1
+ = render Trek::HeadingComponent.new( \
2
+ title: t(".title"),
3
+ intro: t(".intro",
4
+ time: l(@checked_at, format: :long),
5
+ trek: Trek::VERSION,
6
+ rails: Rails.version),
7
+ )
8
+
9
+ = render Trek::SecurityCheckComponent.new(results: @check.results)
@@ -1,5 +1,10 @@
1
1
  = render Trek::GateComponent.new
2
- = form_with url: admin_user_password_reset_path(@token), method: :put do |f|
2
+ / Without Turbo: password managers offer to save the password in an
3
+ element they add to the next page's body, which a Turbo visit would
4
+ replace
5
+ = form_with url: admin_user_password_reset_path(@token),
6
+ method: :put,
7
+ data: { turbo: false } do |f|
3
8
  ruby:
4
9
  rules = t(
5
10
  ".rules",
@@ -1,5 +1,8 @@
1
1
  = render Trek::GateComponent.new
2
- = form_with url: admin_user_session_path do |f|
2
+ / Without Turbo: password managers offer to save the credentials in an
3
+ element they add to the next page's body, which a Turbo visit would
4
+ replace
5
+ = form_with url: admin_user_session_path, data: { turbo: false } do |f|
3
6
  = f.group :email, model_name: User.model_name
4
7
  / "webauthn" offers the site's passkeys in the field's autofill
5
8
  = f.email_field :email,
@@ -14,5 +14,17 @@ namespace :trek do
14
14
 
15
15
  puts statuses.empty? ? "No attachment left in the cache storage." : statuses.map { "#{_2} #{_1}" }.join(", ")
16
16
  end
17
+
18
+ desc "Strip the metadata of the images stored before ImageUploader stripped it (DRY_RUN=1 only lists them)"
19
+ task strip_image_metadata: :environment do
20
+ statuses = Hash.new(0)
21
+
22
+ Trek::StoredImageMetadata.new.strip(dry_run: ENV["DRY_RUN"].present?) do |entry|
23
+ statuses[entry.status] += 1
24
+ puts [entry.status, entry.id].join("\t")
25
+ end
26
+
27
+ puts statuses.empty? ? "No stored image carries metadata." : statuses.map { "#{_2} #{_1}" }.join(", ")
28
+ end
17
29
  end
18
30
  end
@@ -0,0 +1,10 @@
1
+ namespace :trek do
2
+ namespace :security do
3
+ desc "Check the app's configuration against Trek's security recommendations (exits 1 on a failure)"
4
+ task check: :environment do
5
+ check = Trek::SecurityCheck.new
6
+ print check.to_text
7
+ exit 1 if check.failed?
8
+ end
9
+ end
10
+ end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trek
4
- # The disposable email domains of the valid_email2 gem, which keeps them up to date. Its file,
5
- # sorted and one domain per line, is kept as a single String (2.6 MB) and searched by bisection:
6
- # valid_email2's own Set of its 180,000 domains grows the process by about 35 MB.
4
+ # The disposable email domains of the valid_email2 gem, which keeps them up to date. They are
5
+ # searched in its sorted file (ValidEmail2::SortedDomainList, about 3 MB) rather than in its own
6
+ # Set of 180,000 domains, which grows the process by about 35 MB.
7
7
  module DisposableEmailDomains
8
8
  # Labels of the longest parent domain checked, as in valid_email2: an address at
9
9
  # inbox.mailinator.com is refused because mailinator.com is listed.
@@ -15,13 +15,17 @@ module Trek
15
15
  domain = domain.to_s.downcase.strip
16
16
  return false if domain.empty? || allowed?(domain)
17
17
 
18
- candidates(domain).any? { |candidate| listed?(candidate) }
18
+ candidates(domain).any? { |candidate| list.include?(candidate) }
19
19
  end
20
20
 
21
21
  private
22
22
 
23
+ # valid_email2's own list when the app chose the sorted one too
24
+ # (ValidEmail2.disposable_domains_lookup = :sorted_list), rather than a second copy
23
25
  def list
24
- @list ||= File.binread(ValidEmail2::DISPOSABLE_FILE).freeze
26
+ return ValidEmail2.disposable_domains if ValidEmail2.disposable_domains_lookup == :sorted_list
27
+
28
+ @list ||= ValidEmail2::SortedDomainList.new(ValidEmail2::DISPOSABLE_FILE)
25
29
  end
26
30
 
27
31
  # The domain and its parents with two labels or more, up to MAX_DEPTH labels
@@ -33,21 +37,6 @@ module Trek
33
37
  def allowed?(domain)
34
38
  candidates(domain).any? { |candidate| ValidEmail2.allow_list.include?(candidate) }
35
39
  end
36
-
37
- def listed?(domain)
38
- domain = domain.b
39
- low, high = 0, list.bytesize
40
- while low < high
41
- middle = (low + high) / 2
42
- start = (list.rindex("\n", middle - 1) || -1) + 1
43
- stop = list.index("\n", start) || list.bytesize
44
- line = list.byteslice(start, stop - start)
45
- return true if line == domain
46
-
47
- line < domain ? low = stop + 1 : high = start
48
- end
49
- false
50
- end
51
40
  end
52
41
  end
53
42
  end
data/lib/trek/engine.rb CHANGED
@@ -31,6 +31,11 @@ module Trek
31
31
  app.deprecators[:trek] = Trek.deprecator
32
32
  end
33
33
 
34
+ # json 3 breaks ActiveSupport before Rails 8.1.4: fail at boot with what to do, not later
35
+ initializer "trek.json_compatibility" do
36
+ Trek::JsonCompatibility.check!
37
+ end
38
+
34
39
  # The default content schema holds node type classes: rebuild it after code reloading
35
40
  config.to_prepare do
36
41
  Trek::Content.clear_default_schema
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trek
4
+ # json 3's `JSON.parse` takes keyword options only and rejects unknown ones, so `ActiveSupport::JSON.decode`
5
+ # raises with it before Rails 8.1.4: 8.0 passes `quirks_mode: true`, 8.1.0 to 8.1.3.1 a positional hash. Among
6
+ # what breaks, `db/schema.rb` for jsonb columns with a default. Checked when the app boots, as a gemspec can't
7
+ # make the json it accepts depend on the app's Rails.
8
+ module JsonCompatibility
9
+ class Error < StandardError; end
10
+
11
+ FIRST_RAILS = Gem::Version.new("8.1.4")
12
+
13
+ def self.check!(json: JSON::VERSION, active_support: ActiveSupport.version)
14
+ return if Gem::Version.new(json).segments.first < 3 || Gem::Version.new(active_support) >= FIRST_RAILS
15
+
16
+ raise Error, "json #{json} needs Rails #{FIRST_RAILS} or later (this app has #{active_support}): " \
17
+ "upgrade Rails, or add `gem \"json\", \"< 3\"` to the Gemfile and run `bundle update json`."
18
+ end
19
+ end
20
+ end