/packages/fxa-email-service/src/types/validate/mod.rs

https://github.com/mozilla/fxa · Rust · 89 lines · 52 code · 14 blank · 23 comment · 1 complexity · e4ef2c8c2e0fc6b9bd5f7f94f39ff9f2 MD5 · raw file

  1. // This Source Code Form is subject to the terms of the Mozilla Public
  2. // License, v. 2.0. If a copy of the MPL was not distributed with this
  3. // file, you can obtain one at https://mozilla.org/MPL/2.0/.
  4. //! Common validation logic.
  5. //!
  6. //! All functions are predicates,
  7. //! returning `true` if a value is valid
  8. //! and `false` if it isn't.
  9. //! Note that the intention is not to provide
  10. //! perfect validation in each case.
  11. //! Mostly it is to rule out obvious mistakes
  12. //! when setting values in config
  13. //! or wiring in request parameters.
  14. use regex::Regex;
  15. use rusoto_core::Region;
  16. #[cfg(test)]
  17. mod test;
  18. lazy_static! {
  19. static ref AWS_ACCESS_FORMAT: Regex = Regex::new("^[A-Z0-9]+$").unwrap();
  20. static ref AWS_SECRET_FORMAT: Regex = Regex::new("^[A-Za-z0-9+/=]+$").unwrap();
  21. static ref BASE_URI_FORMAT: Regex = Regex::new(
  22. r"^https?://[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*(?::[0-9]+)?/(?:[A-Za-z0-9-]+/)*$"
  23. ).unwrap();
  24. static ref EMAIL_ADDRESS_FORMAT: Regex = Regex::new(
  25. r"^[a-zA-Z0-9.\pL\pN!#$%&'*+/=?^_`{|}~-]{1,64}@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)+$"
  26. ).unwrap();
  27. static ref HOST_FORMAT: Regex = Regex::new(r"^[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*$").unwrap();
  28. static ref SENDER_NAME_FORMAT: Regex =
  29. Regex::new(r"^[A-Za-z0-9-]+(?: [A-Za-z0-9-]+)*$").unwrap();
  30. static ref SENDGRID_API_KEY_FORMAT: Regex = Regex::new("^[A-Za-z0-9._-]+$").unwrap();
  31. static ref SENTRY_DSN_FORMAT: Regex =
  32. Regex::new(r"^https?://[A-Za-z0-9+/:=\.]+@[A-Za-z0-9+/:=\.]+/[0-9]+$").unwrap();
  33. static ref SQS_URL_FORMAT: Regex =
  34. Regex::new(r"^https://sqs\.[a-z0-9-]+\.amazonaws\.com/[0-9]+/[A-Za-z0-9-]+$").unwrap();
  35. }
  36. /// Validate an AWS region.
  37. pub fn aws_region(value: &str) -> bool {
  38. value.parse::<Region>().is_ok()
  39. }
  40. /// Validate an AWS access key.
  41. pub fn aws_access(value: &str) -> bool {
  42. AWS_ACCESS_FORMAT.is_match(value)
  43. }
  44. /// Validate an AWS secret key.
  45. pub fn aws_secret(value: &str) -> bool {
  46. AWS_SECRET_FORMAT.is_match(value)
  47. }
  48. /// Validate a base URI.
  49. pub fn base_uri(value: &str) -> bool {
  50. BASE_URI_FORMAT.is_match(value)
  51. }
  52. /// Validate an email address.
  53. pub fn email_address(value: &str) -> bool {
  54. value.len() < 254 && EMAIL_ADDRESS_FORMAT.is_match(value)
  55. }
  56. /// Validate a host name or IP address.
  57. pub fn host(value: &str) -> bool {
  58. HOST_FORMAT.is_match(value)
  59. }
  60. /// Validate a sender name.
  61. pub fn sender_name(value: &str) -> bool {
  62. SENDER_NAME_FORMAT.is_match(value)
  63. }
  64. /// Validate a Sendgrid API key.
  65. pub fn sendgrid_api_key(value: &str) -> bool {
  66. SENDGRID_API_KEY_FORMAT.is_match(value)
  67. }
  68. /// Validate a Sentry DSN.
  69. pub fn sentry_dsn(value: &str) -> bool {
  70. SENTRY_DSN_FORMAT.is_match(value)
  71. }
  72. /// Validate an AWS SQS URL.
  73. pub fn sqs_url(value: &str) -> bool {
  74. SQS_URL_FORMAT.is_match(value)
  75. }