Skip to content

Scrub secret values to avoid leaks #4413

Closed

Description

@shykes

Problem

Dagger cannot scrub secret values from its various logs and output streams. This is a regression from Dagger 0.2 and the CUE SDK. This can result in secrets leaking in the case of a crash, for example.

Below is the known status of this problem, broken down by source of the sensitive value:

Source of the secret value Dagger 0.2 scrubbed the value Dagger 0.3 scrubs the value Dagger 0.3 could scrub the value without a new API
Raw contents of a file Yes ✅ No ❌ Yes ✅
Raw contents of an environment variable Yes ✅ No ❌ Yes ✅
Decoded from a structured file or stream (eg. JSON value, line split) Yes ✅ No ❌ No, requires new API 🏗️

Solution

Give developers a way to mark sensitive values, by 1) leveraging existing APIs as much as possible, and 2) adding new APIs if needed.

See also

Activity

  1. changed the title [-]Mark secret values as sensitive[/-] [+]Scrub secret values to avoid leaks[/+] on Jan 19, 2023
  2. d3rp3tt3 commented on Jan 25, 2023

    @d3rp3tt3
    Contributor

    I think this should be the top priority for the team to address since it's been noted as a blocker and critical security concern for users trying out Dagger in production environments.

  3. aluzzardi commented on Jan 27, 2023

    @aluzzardi
    Contributor

    Straw-man design:

    • All secrets available in Dagger are centrally stored in secret.Store
    • The "root leak" is in buildkit events (e.g. stdout is printed from buildkit events)
    • Since a recent change, all events are managed in a single place (handleSolveEvents) and re-dispatched to different "listeners"

    Therefore I propose:

    • In handleSolveEvents, we strip all secrets from the raw buildkit events (whether it's logs, vertex name ... anything)
    • To know what the secrets are, secret.Store provides a way to list secrets

    This is similar to the 0.2.x design: https://github.com/dagger/dagger/blob/v0.2.36/client/client.go#L303-L314

    /cc @sipsma @dolanor

  4. shykes commented on Jan 27, 2023

    @shykes
    ContributorAuthor

    @aluzzardi if we go for a helper-based approach to dynamic secrets (a-la Earthly, #4193 ), could this approach still work? The big difference I see is that a secret.Store would no longer be needed, since secrets would be resolved just-in-time by external helpers. But I guess we could still keep the secret.Store as a "pull-through cache", and use it specifically for the purpose of scrubbing? Does this makes sense to you?

  5. aluzzardi commented on Jan 27, 2023

    @aluzzardi
    Contributor

    Does this makes sense to you?

    Yes. secret.Store would be renamed to something else, but would still perform the same job: keep track of "seen" secrets so the scrubber can do its job.

    This holds true no matter how secrets are implemented: Host().Env(), a container, helper, API call, ...

    The one shortcoming is we can only scrub once the secret exists. If secrets are leaked during creation, or fail to be created and leak in errors, they won't get scrubbed.

  6. sipsma commented on Jan 27, 2023

    @sipsma
    Contributor

    Another factor here is that if you print a secret to stdout/stderr it will be persisted to the buildkit cache as part of the shim stdio redirection. So if we only scrub secrets later when displaying the output to users, it will still be in the cache. We also would need to scrub the secrets from the values returned by the Stdout and Stderr APIs, which aren't buildkit events. Also in the exec failure error message (which now includes stdout+stderr).

    This makes me wonder if we should just scrub the secrets as part of the shim stdio redirection itself. That way they never leak to the cache and we don't have to think about scrubbing return values all over the place. Then I think @aluzzardi's suggestion would probably handle the rest of the cases?

  7. shykes commented on Jan 27, 2023

    @shykes
    ContributorAuthor

    This makes me wonder if we should just scrub the secrets as part of the shim stdio redirection itself.

    That would be amazing 🥇

  8. self-assigned this
    on Jan 27, 2023
  9. aluzzardi commented on Jan 27, 2023

    @aluzzardi
    Contributor

    @sipsma You're absolutely right -- I forgot about that part.

    This makes me wonder if we should just scrub the secrets as part of the shim stdio redirection itself.

    Yeah, that sounds right.

    We'd have to somehow let the shim know that such and such file or env is a secret. Maybe by injecting a metadata file? Or a special env? Does it matter if it's part of the cache key (I guess not)?

    Sounds doable.

    That way they never leak to the cache and we don't have to think about scrubbing return values all over the place. Then I think @aluzzardi's suggestion would probably handle the rest of the cases?

    Wondering if implementing only at the shim level is enough. It won't catch secrets in vertex name (e.g. echo <plaintext secret>), but I think that's alright? It's a much simpler design.

    edit: Updated straw-man

    • Engine passes secrets either by ENV or File (already the case -- container.Exec in core/container.go)
    • Engine injects a new variable, e.g. _DAGGER_SCRUB_SECRETS={files: ["/path"], envs: ["ENV1"]} containing the location of secrets
      • Alternative: it's injected as a file somewhere in /.dagger_meta_mount
      • In either of those cases, the path and/or env name is part of the cache key: that's ok since it's already the case anyway
      • In either of those cases, the value of the secret is not part of the cache (nor cache key)
    • Shim parses _DAGGER_SCRUB_SECRETS, loads all secret values from env and files, and scrubs stdout/stderr in real-time
  10. linked a pull request that will close this issueScrub secrets #4518on Feb 2, 2023
  11. dolanor commented on Feb 3, 2023

    @dolanor
    Contributor

    Regarding the caching. I don't know how buildkit would know if it needs to restart a step or not.
    If the step needs a secret (MY_SECRET=mysecretvalue) and it gets scrubbed into ***.
    But if we decide to change the secret to myothersecretvalue, how can buildkit know that it needs to restart the step? I guess the cache key would contain a hash of MY_SECRET secret name, but not the value. Therefore, it might think it doesn't need to restart the step?
    Or does it restart any step that has secrets?

  12. aluzzardi commented on Feb 3, 2023

    @aluzzardi
    Contributor

    We use hashes of secret values as Secret IDs. So changing a value changes the secret ID, which invalidates the cache of everything referring to that secret id

  13. aluzzardi commented on Feb 3, 2023

    @aluzzardi
    Contributor

    To clarify: the shim has nothing to do with buildkit. The shim runs in the container alongside user code (well, it wraps user code).

    Whatever the shim does, it will not affect the cache status (the shim invocation itself will be cached or not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions