odoo-security
Security audit of Odoo code
Audit Odoo addons for the framework-specific ways access control and injection
go wrong. When a feature needs more access than the rules give, it gets the
minimum, in the narrowest scope, with a comment saying why; a silent widening
(a bare sudo(), a loosened access row) is a finding.
Process
- Review the
security/data files against Access control. - Sweep the addon for every pattern in the table below; judge each hit against
its section. A hit is not a finding by itself; the sections say what makes
it one.
- Report each finding with severity, exact file/line, and a minimal fix.
The audit is complete when every pattern has been swept and every hit judged.
| Sweep for | Judge against |
| --- | --- |
| sudo(, with_user(, with_company( | Don't over-sudo |
| cr.execute, SQL(, .format/%/f-string near SQL | Use the ORM; parameterize SQL |
| domain built from request, RPC, or stored field values | Domain injection |
| public (non-_) model methods | Default to private methods |
| @http.route(, bare @route( | Routes: auth, POST, CSRF |
| innerHTML, insertAdjacentHTML, markup( (JS), Markup( (Python), t-raw (dead code) | Prevent XSS |
| password/token/secret fields | Field-level access |
| related= crossing models (sudo-computed by default) | Field-level access |
| open( | Open files with file_open |
| eval(, exec(, safe_eval | eval is evil |
| pickle | Never pickle |
| getattr(, setattr( on records | getattr/setattr |
| == comparing a secret/token | Timing attacks |
| def f(..., x=[]) / x={} | Mutable default arguments |
| methods returning rich objects | Don't return complex objects |
| a check on vals in def create( before super().create( | Access control |
Access control
Mechanics of ir.access rows and field groups are in the odoo-guidelines
skill (Access rights,
Fields); audit security/ with
the attacker's reading:
- Flag any
c/u/doperation granted tobase.group_everyone,
base.group_portal, or base.group_public; scrutinize even r on models
holding personal or business-sensitive data.
- A permission row's
operationset is what it grants; a *restriction*
(group-less) row covering only some operations leaves the others governed by
the permission rows alone: if any permission grants write, an attacker
can alter records the read restriction hides (write checks write access
only, never read). Cover all CRUD operations that need restricting.
- Multi-company models need a company restriction row; sensitive models need
domain restrictions, not just group permissions.
- A
createoverride that validatesvalsbeforesuper().create(), for
instance to restrict some fields or values to a group, can be bypassed when
it reads only vals. The ORM adds values after that code, in
_prepare_create_values, called by BaseModel.create:
- the
default_<field>keys of the context: a caller who is refused
book_id in vals passes default_book_id in the context instead;
- the
ir.defaultrecords: an internal user can add their own; - what an override of
default_getadds, and the precomputed fields. - To prevent this, move the check to an override of
_prepare_create_values
and check what its super() returns: the check still runs before the
record is created. Those values already hold the context defaults and the
ir.default ones, so there is no need to handle them by hand.
- A raise after
super().create()is too late: the record is created
already, so if the caller catches the AccessError, the creation is not
reverted.
- In
_prepare_create_values, the values also hold thedefault=of the
field. A check that only tests that a value is there, as
if vals.get('book_id'):, then also refuses a create where the caller gave
no book_id and the field has a default. Check which book the value is
(may the user set that one?), not whether the key is there.
writehas no such gap: it adds no default.
Field-level access
- A field's
groupsattribute removes it from views andfields_getand
raises on explicit read/write. Use it for sensitive fields instead of
relying on the UI to hide them.
- Passwords and API tokens: restrict them with
groups="base.group_system",
or groups=fields.NO_ACCESS to hide the field from everyone, admins
included. A token field on a model with a permissive access row is readable
by any user via search_read.
relatedfields are sudo-computed by default (related_sudo=True): a
related field reaching sensitive data through a user-writable Many2one
lets the user point the M2o at an arbitrary record and read the related
value with elevated rights; set related_sudo=False on sensitive chains.
(readonly=False write-through is a separate concern: it runs in the
user's environment and is access-checked.)
