Skip to content

Commit

Permalink
Merge pull request #9634 from colinux/fix-regex-timeout
Browse files Browse the repository at this point in the history
Sécurité (champ regex): timeout plus agressif à 1 seconde
  • Loading branch information
colinux authored Oct 24, 2023
2 parents 3487a5f + d87c7ca commit 5d3d4cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
13 changes: 12 additions & 1 deletion app/assets/stylesheets/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
input[type=number],
input[type=datetime-local],
textarea,
input[type=tel], {
input[type=tel] {
@media (max-width: $two-columns-breakpoint) {
width: 100%;
}
Expand Down Expand Up @@ -538,6 +538,17 @@
}
}

.type-de-champ-expression-reguliere {
display: flex;
align-items: center;

&:before,
&:after {
font-weight: bold;
content: "/";
}
}

[data-react-component-value^="ComboMultiple"] {
margin-bottom: $default-fields-spacer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
.cell.mt-1
= form.label :expression_reguliere, for: dom_id(type_de_champ, :expression_reguliere) do
= t('.expression_reguliere.labels.regex')
= form.text_field :expression_reguliere, class: "fr-input small-margin small", id: dom_id(type_de_champ, :expression_reguliere)
.type-de-champ-expression-reguliere
= form.text_field :expression_reguliere, class: "fr-input small-margin small", id: dom_id(type_de_champ, :expression_reguliere)

.cell.mt-1
= form.label :expression_reguliere_exemple_text, for: dom_id(type_de_champ, :expression_reguliere_exemple_text) do
Expand Down
2 changes: 1 addition & 1 deletion app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def routable?
def invalid_regexp?
return false if expression_reguliere.blank?
return false if expression_reguliere_exemple_text.blank?
return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: 2.0))
return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: ExpressionReguliereValidator::TIMEOUT))

self.errors.add(:expression_reguliere_exemple_text, I18n.t('errors.messages.mismatch_regexp'))
true
Expand Down
4 changes: 3 additions & 1 deletion app/validators/expression_reguliere_validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class ExpressionReguliereValidator < ActiveModel::Validator
TIMEOUT = 1.second.freeze

def validate(record)
if record.value.present?
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0))
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: TIMEOUT))
record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: record.expression_reguliere_error_message)
end
end
Expand Down

0 comments on commit 5d3d4cb

Please sign in to comment.