restify-cors-middleware is a library providing W3C-compliant Cross-Origin Resource Sharing (CORS) middleware specifically designed for Restify servers. The current stable version is 1.1.1. This library offers robust control over allowed origins, headers, and preflight requests. Key differentiators include its dynamic handling of `Access-Control-Allow-Origin` by returning the matched origin rather than a simple wildcard, which enhances security. It supports flexible origin specification using strings, wildcards, and regular expressions. The package maintains compatibility with a broad range of Restify versions (2.6.x - 7.x.x) through its peer dependency. While a specific release cadence isn't documented, historical releases show updates addressing security concerns, such as the upgrade to Restify 4.1.x to fix a `negotiator` module vulnerability in version 0.0.7. It is predominantly used in CommonJS environments.
npm install restify-cors-middlewareVerified import paths — ran on the pinned version, not inferred.
This code sets up a basic Restify server with CORS protection, configuring allowed origins, headers, and demonstrating how to apply the preflight and actual CORS middleware.
Upgrade `restify-cors-middleware` to at least v0.0.7 and ensure your `restify` peer dependency is 4.1.X or newer to address known vulnerabilities.
Specify the exact list of allowed origins (strings or regular expressions) to limit cross-origin access to trusted domains only. Avoid `origins: ['*']` unless absolutely necessary and understood.
Ensure that clients making cross-origin requests always include the `Origin` header. For testing purposes, manually add an `Origin` header to simulate browser behavior.
Configure your reverse proxy to include `Vary: Origin` in its caching policy. This ensures that different CORS responses (based on `Origin`) are cached separately, preventing unexpected behavior or security issues.
Verify that your client-side code sends an `Origin` header for cross-origin requests. Double-check the `origins` configuration in `corsMiddleware` to ensure the client's origin is correctly specified (using exact strings or a matching regular expression).
Update your reverse proxy configuration to include `Vary: Origin` in its caching directives. This instructs the proxy to store separate cached versions for requests originating from different domains.
Upgrade `restify-cors-middleware` to the latest stable version (1.1.1 or newer). Additionally, ensure your `restify` peer dependency is `4.1.X` or higher, as recommended by the `restify-cors-middleware` documentation since v0.0.7.