API CORS - Cross Origin Resource Sharing Rules, CORS Headers and Preflight
by Modvanta https://modvantaapps.tech/$ 21.94

API CORS for Odoo 19
Your React app on app.example.com calls Odoo on erp.example.com and the browser stops it with a CORS error. This module lets you say, per API, which websites may call it, with which methods and headers. Everything else stays blocked, and you can see who was turned away.
Written and supported by Modvanta - modvantaapps@gmail.com
What it does
Nothing is allowed until you add a rule. Then only what the rule says.
Rules per API Pick JSON-2, JSON-RPC, XML-RPC or the web session login and the paths are filled in for you. Or type your own path patterns, one per line. | Origins the way you think of them https://app.example.com, https://*.example.com for every subdomain, http://localhost:3000 for development, or a star for public read-only endpoints. | Preflight answered The OPTIONS preflight is answered before Odoo routes the call, so JSON-RPC routes and POST-only XML-RPC routes work from the browser too. |
Headers on errors too A 401 or 404 from the API carries the CORS headers, so your front end can read the error instead of seeing a network failure. | Refused origins log Every refused call is counted per origin, path, method and reason per day. When a developer says it does not work, you look it up in ten seconds. | Safe when you save A star origin with credentials, the null origin, a wildcard on a bare TLD or an origin with a path are refused before they reach production. |
Screens
All taken on an Odoo 19 test database with the module installed.
One rule per front end
Each rule says which API paths it covers and which origins may call them. Rules are tried in order, and the first one that fits the origin, method and headers answers.

Allowed origins, methods, headers and cache
Exact origins, wildcard subdomains and localhost with a port. Tick the methods, list the request headers the app sends and the response headers it needs to read, and set how long the browser may cache the preflight.

Test an origin before you deploy
Type an origin, a path and a method. You see the preflight answer and the headers of the real call, exactly as the browser would get them, and which rule matched.

And see why something is refused
No rule for the path, origin not in the list, method not ticked or a header not allowed. The reason is spelled out.

Credentials when you need cookies
For an app that logs in with the Odoo session, tick Allow Credentials. The exact origin is sent back, never a star, and the form reminds you which sites can actually share the cookie.

Mistakes are stopped on save
Allowing every website with your users' cookies is the classic CORS hole. The rule cannot be saved like that.

Refused origins, counted
One line per day, origin, path, method and reason, with a hit counter and the last IP. Old lines are removed after the number of days you choose.

Settings
A pause switch that stops every cross-origin answer at once, the refused-origin log on or off, and how many days to keep it.

How it works
| 1 | Install the module. From that moment no other website can call your APIs from a browser, same as before. |
| 2 | Open Settings, API CORS, CORS Rules and add a rule: pick the API, paste the origins of your app, tick the methods. |
| 3 | Press Test an Origin to check the answer, or open the health URL /mdv_api_cors/health?path=/json/2/res.partner/read from your app's console. |
| 4 | Deploy the front end. If a call is refused, the Refused Origins list tells you the origin, the path and the reason. |
FAQ
Compatibility and support
Tested on Odoo 19.0 Community. It depends only on base_setup and web and works inside Odoo's own HTTP layer, with no server patch and no extra Python package.
Questions before you buy, or something not working? Write to modvantaapps@gmail.com.
| Availability |
Odoo Online
Odoo.sh
On Premise
|
| Lines of code | 781 |
| Technical Name |
mdv_api_cors |
| License | OPL-1 |
| Website | https://modvantaapps.tech/ |
Odoo Proprietary License v1.0 This software and associated files (the "Software") may only be used (executed, modified, executed after modifications) if you have purchased a valid license from the authors, typically via Odoo Apps, or if you have received a written agreement from the authors of the Software (see the COPYRIGHT file). You may develop Odoo modules that use the Software as a library (typically by depending on it, importing it and using its resources), but without copying any source code or material from the Software. You may distribute those modules under the license of your choice, provided that this license is compatible with the terms of the Odoo Proprietary License (For example: LGPL, MIT, or proprietary licenses similar to this one). It is forbidden to publish, distribute, sublicense, or sell copies of the Software or modified copies of the Software. The above copyright notice and this permission notice must be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Please log in to comment on this module