Security
Background
Open Data Capture is designed to be secure by design. Here, we provide a detailed overview of all major aspects of our security system, including authentication and authorization.
Assumptions
This security model is based on protecting the database from unauthorized users through network-based attacks. To this end, it is assumed that:
- The platform administrator is trusted
- The platform administrator understands this material
- The server is not compromised
Authentication
The API uses JSON Web Tokens (JWTs) to identify and authorize users. When a client sends a request to /auth/login, their password is compared to a hashed and salted version stored in our database. If the password is valid, a JWT is created with a payload that encodes various data about the user, including any relevant permissions. This payload is then signed using a private key stored in the environment variable SECRET_KEY and set to expire in one hour. The signed payload is returned to the client in the form of a JWT.
The web app signs in differently in one respect: it sends the header odc-interactive-sign-in: true with its login request, and the token it receives expires after the idle limit instead, which is 15 minutes unless AUTH_IDLE_TIMEOUT_MINUTES sets another value between 5 and 60. While the person signed in is using the keyboard, pointer or touch screen, the web app exchanges its token for a new one through POST /auth/refresh. Once they stop, the token expires and they are asked for their password again, with the page they were working on kept as it was.
Any client may renew a token that has not expired in the same way, by sending it to POST /auth/refresh as a bearer token. The new token lasts as long as the one it replaces was issued for, and its permissions are read from the database again. A token is no longer renewed once:
- 12 hours have passed since the password was entered, or the number of hours set by
AUTH_ABSOLUTE_TIMEOUT_HOURS, between 1 and 12 - the account’s password has been changed, or an admin has required it to be reset
- the account has been disabled or archived, even if it has since been enabled or unarchived
In each case the user has to sign in with their password again. A token that was issued before one of these events can still be used until it expires, but not renewed.
On requests to protected endpoints within the API, the client must present the JWT as proof of their identity. Following web standards, the access token is attached to each request header in the following format: Authorization: Bearer {TOKEN}. The server then verifies the integrity of the payload by checking the signature against the private key. If the signature is valid, the server can trust that the payload has not been tampered with and can use the data contained within it to determine whether the now authenticated user is authorized to perform certain actions. If the signature is invalid, or otherwise malformed, the server returns HTTP status 401.
Authorization
Once a user is authenticated, attribute-based access control is used to determine access to protected resources. In the source code, endpoints are assigned a given route access (either public or protected). If a route is protected, it is assigned an action (create, delete, manage, read, update) and subject, which corresponds to either a database model (e.g., User) or a special value all. For example, POST /v1/users may be associated with the action create and the subject User. If there is no route access explicitly defined for a given route, the default is manage and all.
When creating a user, a default set of permissions may be assigned based on the following roles:
- Admin
- Group Manager
- Standard User
If no role is specified, then the user is created with no permissions. In this case, an admin can apply additional, custom permissions to this user, or link them to a permission set. For step-by-step instructions and the permissions each page needs, see How to Configure Permissions.
After validating the access token, the server checks whether the user has authorization to perform the selected action (create, read, update, or delete) on the subject (e.g., Group). These are specified on a per-route basis. If a route does not have any required permission set specified, the user must have permission to manage all resources to have access (i.e., the platform admin). If the user does not have adequate permissions, HTTP status 403 is returned.
User Roles
Admin
By default, an admin user has full access (i.e., create, read, update, and delete) to all resources. Only an admin can create, modify or delete users, or change their permissions; no additional permission extends this to anyone else. For security reasons, it is recommended to create only one admin, and to grant group managers additional permissions when required. Each additional permission applies either to one group or to all groups, so choose the narrowest scope that serves. Granting Manage (All) makes the user an administrator.
Permission Sets
When several users need the same additional permissions, an admin can define them once as a named permission set and link each of those users to it, instead of granting them user by user. A user holds at most one permission set, and either a permission set or their own additional permissions, never both: choosing one for a user removes the other.
Each rule in a set applies either to every group, or to the groups of whoever holds the set, which is resolved each time they sign in or their token is renewed. Resources that belong to no group, such as instruments, can only be granted for every group. The set is linked rather than copied, so editing it changes what every holder may do from the next time their token is renewed or they sign in, and deleting it leaves its holders with only their role. A permission set cannot contain Manage (All), since one edit to it would make every holder an administrator, and an admin cannot hold a set. Only an admin can see, create, change or delete permission sets.
Group Manager
Group managers can change the settings of their own groups and manage the remote assignments in them, and read the data collected for their groups (e.g., subjects, sessions and instrument records). They cannot create or archive a group. They can see the users in their groups, but cannot create, modify or delete them.
Standard User
By default, standard users are not able to access any data. They are only allowed to administer instruments, and download the record once complete (as it is stored in memory at that time).
Archived Groups
Groups are never deleted. Instead, an admin can archive a group from Manage Groups, and unarchive it again at any time. From the next time their token is renewed or they sign in, members of an archived group can no longer select it or access its data, and any additional permission limited to that group stops applying. A member who is already signed in keeps their access until their token is next renewed or expires, at most an hour later. The group’s subjects, records, uploaded files and series instruments are kept, and its series instruments stay private to it. Admins still see an archived group, marked as archived, and can still read its data.
Instrument Creation
Given that instruments can contain arbitrary JavaScript code, the security of the entire system relies on the assumption that only trusted users are able to create instruments. You should assume that anyone able to create instruments is able to execute arbitrary code on your server, with the permissions of the running process. If you are running Open Data Capture in a Docker container (as is recommended), this could lead to the compromise of your entire server (for details, read see this).