# Sensitive attributes

> Seal or hash a value at rest, keep it out of every read and every search, and let only a root administrator change the values that matter most.

Some of what you keep about a user should never come back out: a national ID, a recovery secret, a password. Mark the
attribute, and the realm seals or hashes it at rest, leaves it out of every read and the audit trail, and refuses
to filter or sort on it, so a search can't be used to guess it.

## Before you begin

- **Privilege:** `RealmOwner`, or a privilege of your own over type definitions, to mark attributes. Only a root
  administrator can add, change or remove `x-root-protected`. See
  [Who may do what](../identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).
- **Mark before you store.** Add the mark before the first value is written.

## How it works

Each mark goes on the attribute's definition in the type:

| Mark | What the realm does |
|---|---|
| `"scope": "private"` | Stores it as written, never returns it, and leaves it out of the audit trail. A write that leaves it out keeps the stored value |
| `"x-encrypt": { "key": "store/encryption" }` | Seals it at rest with the realm's own storage key. Goes with `"scope": "private"`, on a text attribute |
| `"x-hash": { "algorithm": "bcrypt" }` | Stores a one-way hash, as the realm does for passwords. `pbkdf2` is the other choice |

Values marked this way can't be filtered, sorted or searched.

**`x-encrypt` is always written the same way:** `{ "key": "store/encryption" }`, the realm's storage key. Write it
exactly so. `"x-encrypt": true` isn't understood and leaves the value unsealed, with no error; a different `key` is
accepted when you save the type but isn't the realm's storage key, so don't use one.

**Sealed values follow key rotation.** When the realm's storage key is rotated, sealed values are sealed again under
its latest version. See [Encryption of stored data](../../../platform/data/encryption/).

**Root-protected values.** `"x-root-protected": ["write"]` lets everyone read a value but only a root administrator
change it; `["read", "write"]` also hides it from everyone else. Only a root administrator can add, change or remove
this mark.

## Set it up

Read the type, add the attribute with its mark, and save it back, as
[Attributes and validation](../attributes-and-validation/#set-it-up) describes. A national ID, sealed and never
returned:

```json
"nationalId": { "type": "string", "scope": "private",
                "x-encrypt": { "key": "store/encryption" } }
```

Then write it like any other attribute:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "nationalId": "<value>" }'
```

A read of Maya never shows it.

## Limits and defaults

| What | Default or limit |
|---|---|
| `x-encrypt` | Only on a text attribute that is also `"scope": "private"`; key always `store/encryption` |
| `x-hash` algorithms | `bcrypt` (the default for passwords) or `pbkdf2` |
| Searching a marked value | Never: left out of filters, sort and free text |

## When a save fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `INVALID_REQUEST` | The type's marks don't fit: `x-encrypt` on an attribute that isn't text or isn't private, or a `key` that doesn't read | Put `x-encrypt` on a private text attribute, with `{ "key": "store/encryption" }` |
| `403` | `FORBIDDEN` | A change to `x-root-protected` by anyone but a root administrator | Ask a root administrator |

## Audit record

The save of the type is recorded with its definition. Writes of a marked value are recorded like any change, with the
value left out: the history shows that the attribute changed, never what it holds.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A value you meant to seal reads back | The attribute isn't `"scope": "private"`, or `x-encrypt` was written as `true` | Fix the definition as above, then write the value again |
| A search on the attribute is refused | Marked values can't be searched, by design | Search on another attribute |

## Good to know

- **A replace keeps what it can't see.** A `PUT` that leaves out a private value keeps the stored one, since the
  writer was never shown it.
- **Hashing is one-way.** A hashed value can never be read back, by anyone, administrators included.
- **Passwords are already protected.** The shipped `passwordHash` is private and bcrypt-hashed.

## Related

- [Attributes and validation](../attributes-and-validation/): add attributes and their rules.
- [Encryption of stored data](../../../platform/data/encryption/): the realm's keys.
- [Search and paging](../../users/search/): what can and can't be searched.
