Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email format validation does not accept non-ascii characters #880

Open
g-radam opened this issue Dec 4, 2023 · 0 comments
Open

Email format validation does not accept non-ascii characters #880

g-radam opened this issue Dec 4, 2023 · 0 comments

Comments

@g-radam
Copy link
Contributor

g-radam commented Dec 4, 2023

OpenAPI schemas which specify a field that is of format "email" fails if the email address contains non-ascii characters.

AVJ Mentions support for Internationalized email addresses "idn-email" via the avj-2019 plugin, but I'm not sure if OpenAPI is using this. Also see: https://en.wikipedia.org/wiki/Email_address#Internationalization and https://github.com/luzlab/ajv-formats-draft2019

Reproduce Via:
Schema:

emailAddress:
  type: string
  format: email
  description: An Email address
  example: [email protected]

Non-Ascii Email (notice the " i " looking letter is not ASCII in the local part"Maria"):

Yields result:

Bad Request: request/body/emailAddress must match format "email"
    at Object.POST-/api/v1/person-application/json (<project>/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js:118:31)
    at RequestValidator.validate (<project>/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js:44:41)
    at <project>/node_modules/express-openapi-validator/dist/openapi.validator.js:233:53
    at <project>/node_modules/express-openapi-validator/dist/openapi.validator.js:166:28
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

I would assume this would pass validation.

To get this working, I simply overloaded the email format validator with:

OpenApiValidator.middleware({
        apiSpec: ...,
        formats: [
            // In-built email validation fails with unicode characters
            // See: https://en.wikipedia.org/wiki/Email_address#Internationalization
            {
                name: "email",
                validate: (email: string): boolean => {
                    return email.match(
                        /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
                    ) != null;
                }
            }
        ]
    }),

Email validation code yanked from stackoverflow: https://stackoverflow.com/questions/3844431/are-email-addresses-allowed-to-contain-non-alphanumeric-characters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant