demarches-normaliennes/app/validators/mon_avis_embed_validator.rb

10 lines
716 B
Ruby
Raw Normal View History

2019-07-17 17:13:08 +02:00
class MonAvisEmbedValidator < ActiveModel::Validator
def validate(record)
# We need to ensure the embed code is not any random string in order to avoid injections
r = Regexp.new('<a href="https://monavis|voxusagers.numerique.gouv.fr/Demarches/\d+.*key=[[:alnum:]]+.*">\s*<img src="https://monavis|voxusagers.numerique.gouv.fr/(monavis-)?static/bouton-blanc|bleu.png|svg" alt="Je donne mon avis" (title="Je donne mon avis sur cette démarche" )?/>\s*</a>', Regexp::MULTILINE)
2019-07-17 17:13:08 +02:00
if record.monavis_embed.present? && !r.match?(record.monavis_embed)
2023-03-15 17:23:19 +01:00
record.errors.add :base, :invalid, message: "Le code fourni ne correspond pas au format des codes MonAvis reconnus par la plateforme."
2019-07-17 17:13:08 +02:00
end
end
end