Add support for regex in allowed origins for the CORS filter. by alexdupre · Pull Request #13778 · playframework/playframework

Pull Request Checklist

Helpful things

Purpose

This PR adds the ability to use regular expressions when defining the allowed origins for the CORS filter.

Background Context

Sometimes explicitely declaring all allowed origins is not possible, for example when dynamic hostnames under the same domain are used. Currently the only option is to allow any origin, but this is sub-optimal. I've evaluated two different approaches to solve the issue:

  1. add support for wildcards (simpler, but still limited)
  2. add support for regular expressions (more complex, but more flexible)

Considering that other common web frameworks (like Node Express) already support regular expressions, I've opted for the second solution.

The change has been done in a full backward compatible way, so the CORS behavior is unchanged unless the new allowedOriginsAsRegex config parameter is set to true.