Shelftara Documentation 1.0.0

Security

How Shelftara protects a workspace

This page lists the measures that the code of 1.0.0 really implements, and names the ones it does not. Every claim below can be checked against the source: the password hashing in internal/auth, the middleware in internal/web/app.go and the filters in the handlers.

The passwords

  • A password is hashed with bcrypt at the default cost. The plain password never reaches the database and never leaves the server.
  • The hash is compared with bcrypt.CompareHashAndPassword, which is constant time for the same hash.
  • A password shorter than eight characters is refused by the hashing function, by the registration form, by the form of a user and by the creation of a workspace.
  • The column of the hash is excluded from the JSON representation of a user, so it cannot leak through an endpoint.
  • The short PIN of a manager is hashed with bcrypt as well, at the lowest cost, because it is checked on every void or discount rather than once at the sign in.
  • Changing the password of a user rehashes it; the old hash is replaced and never kept.

The CSRF protection

Shelftara uses the double submit cookie pattern, and it applies it to every request that changes something, without exception.

  • The server puts a random token in the cookie shelftara_csrf and keeps it in the context of the request.
  • The safe methods — GET, HEAD, OPTIONS and TRACE — are exempt.
  • Every other method must send the token in the header X-CSRF-Token, in the header X-Csrf-Token or in the form field _csrf.
  • The comparison is constant time, so a wrong token does not leak the right one by the time the answer takes.
  • A request without a valid token is refused with 403, before the handler runs. Nothing is written.
  • The token lives in a cookie that is not HttpOnly, because the page has to copy it into the header. It is tied to the origin by SameSite=Lax.

The sessions

  • The session identifier is twenty-four random bytes, written in hexadecimal. It is generated by the random source of the system.
  • The session is a row of the table sessions, with the user, the workspace, the address of the request and the user agent. The server can therefore close a session without waiting for the cookie to expire.
  • The cookie that carries it is HttpOnly, so a script of the page cannot read it, and SameSite=Lax, so it does not travel with a request that comes from another site.
  • The session is checked on every request. An expired session is deleted the moment it is met, and the expired sessions are also purged every thirty minutes by the server itself.
  • A user whose account has been disabled is refused even when the session is still valid.
  • The address of the request is never taken from a header of the request: it is the address of the socket, which means it cannot be forged by a visitor.

The isolation of the workspaces

This is the point that matters most in a multi-tenant installation: a workspace must never read the data of another one.

  • Every table of a business carries a tenant_id column with an index on it.
  • Every query of the panel filters on the tenant_id of the session. The generated lists, the forms, the reports, the dashboard and the assistant all start from that filter.
  • The records are loaded with the same filter: a request that carries the identifier of a record of another workspace finds nothing and answers 404.
  • The document numbers are counted per workspace, so the number of an invoice of one workspace never says anything about another one.
  • The platform layer is the only part that reads across the workspaces, and it accepts only an account with a tenant_id of zero and the role super_admin.
The workspace of a request is never taken from the body

The workspace comes from the session, or from the host, the path, the query string or the cookie of the shop. A visitor cannot select a workspace by sending a field: the handlers read the identifier of the workspace from the session and add it to the query themselves.

The permissions

  • Thirty-one permission codes describe what a user may do. The role of the user decides the set of codes, and the set is computed on every request.
  • Each handler checks the code it needs before it does anything. A missing code answers 403 with a plain sentence, whatever the module.
  • The navigation is filtered by the same codes, so a user is not even offered a page they may not open. The filter is a convenience; the check in the handler is the protection.
  • The six built-in roles have fixed sets of permissions, and a custom role stores the codes it is given. A user whose role is not one of the six gets exactly the codes of its role record.
  • The account with the role store_customer is refused by the panel: it is a customer of a shop, not a member of the staff.

The audit log

  • Every action that matters writes an entry: the creation of a workspace, the change of its state or of its plan, a sale, the opening and the closing of a shift, an order of the shop, its state, a synchronisation, a setting and a profile.
  • An entry carries the workspace, the user, the action, the entity, its identifier, the detail and the address of the request.
  • The page of the audit log is behind the permission settings.manage and shows the last two hundred entries of the workspace.
  • An entry that cannot be written is written to the log of the server and does not stop the action that caused it.

The uploaded files

  • A request for a file under /uploads/ is reduced to its base name with filepath.Base, so a path can never climb out of the folder.
  • A path that contains .. is refused with 404 before anything is read.
  • The file is read from the folder UPLOAD_DIR, which is the only folder the application writes in.
  • The content type is set from the extension of the file and never from the content: an unknown extension is served as application/octet-stream.
  • The header X-Content-Type-Options: nosniff is sent on every response, so the browser never guesses a different type.

The headers of the responses

HeaderValueWhere it comes from
X-Frame-OptionsSAMEORIGIN by defaultThe variable FRAME_OPTIONS. The value none sends no header.
Content-Security-Policyframe-ancestors <value>The variable FRAME_ANCESTORS. It is sent only when the value is not empty.
X-Content-Type-OptionsnosniffAlways sent by the middleware.
Referrer-Policystrict-origin-when-cross-originAlways sent by the middleware.
Cache-Controlpublic, max-age=31536000, immutableOn the assets of the interface, which are embedded in the binary.

The framing headers are the two that matter for a panel: they stop another site from putting the panel of a business in a frame and from stealing the clicks of a cashier.

The details that count

  • The address of the connection is compared with a constant time comparison everywhere a secret is compared.
  • The form of the forgotten password answers the same sentence whether the address exists or not, so it does not tell a visitor which addresses are registered.
  • The form of the sign in answers the same sentence whether the address or the password is wrong.
  • The parameter next of the sign in is only followed when it starts with a slash, so it cannot be used to send a visitor to another site after the sign in.
  • The server accepts a body of 64 MB at most, closes a request that takes more than sixty seconds to read and more than a hundred and twenty seconds to write, and recovers from a panic without taking the process down.
  • The SQL statements are prepared by the driver of the database; the handlers bind the values and never build a query by concatenating a value of the request.
  • Every timestamp is stored in UTC, so the change of the time zone of a server never moves a document of the past.

What the operator has to do

The application does what it can with the configuration, but a few things belong to the server and to the proxy:

  • TLS. The server speaks plain HTTP. Terminate TLS at the proxy and set COOKIE_SECURE=true only when the cookies really travel over HTTPS.
  • Strict transport security. The header Strict-Transport-Security is set at the proxy, not by the application.
  • Firewall. The port of the database must not be reachable from the internet, and the port of the application should only be reachable from the proxy when it is on the same host.
  • The default password. Change the password of the platform administrator before the installation is reachable.
  • The secrets. The .env file holds the password of the database. Keep it readable by the service account only.
  • The backups. A dump of the database and a copy of UPLOAD_DIR are the two things a restore needs.

The measures that are not in 1.0.0

They are listed here so that nobody assumes them. The code does not implement them yet.

  • No rate limit on the sign in. There is no lockout and no delay after a failed attempt. Put a limit in front of the form at the proxy, or restrict the path /login to your own network.
  • No two factor authentication. The table of the codes and the fields of the two factor exist in the schema, but the flow of the forgotten password only records a six digit code that expires after fifteen minutes. Nothing asks for it yet.
  • No content security policy for the scripts. Only the directive frame-ancestors is sent.
  • No manager PIN in the register. The table of the approvals exists, but the register does not ask for a PIN yet.
  • No encryption of the data at rest. The protection of the disk and of the backups is the job of the operator.