Skip to content

Commit

Permalink
Fix request safety issue in set_locale (#315)
Browse files Browse the repository at this point in the history
* fix request safety issue in set_locale

* fix instructions to suggest request safe way to set locale
  • Loading branch information
3pns authored Aug 23, 2023
1 parent b592dd7 commit 58630bc
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions bullet_train-api/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-fields/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-has_uuid/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-integrations/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-obfuscates_id/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-roles/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-scope_questions/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-sortable/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-themes-light/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train-themes/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
7 changes: 3 additions & 4 deletions bullet_train/app/controllers/concerns/controllers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ def enforce_invitation_only
end
end

def set_locale
I18n.locale = [
def set_locale(&action)
locale = [
current_user&.locale,
current_user&.current_team&.locale,
http_accept_language.compatible_language_from(I18n.available_locales),
I18n.default_locale.to_s
].compact.find { |potential_locale| I18n.available_locales.include?(potential_locale.to_sym) }
yield
I18n.locale = I18n.default_locale
I18n.with_locale(locale, &action)
end

# Whitelist the account namespace and prevent JavaScript
Expand Down
4 changes: 2 additions & 2 deletions bullet_train/config/locales/en/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down
4 changes: 2 additions & 2 deletions bullet_train/test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# <%= t("hello") %>
#
# To use a different locale, set it with `I18n.locale`:
# To use a different locale, set it with `I18n.with_locale`:
#
# I18n.locale = :es
# I18n.with_locale(:es, &block)
#
# This would use the information in config/locales/es.yml.
#
Expand Down

0 comments on commit 58630bc

Please sign in to comment.