by nickjj · 4y ago · 0 comments
Yeah I don't think this gets talked about enough.
If you're talking about private repos in an organization then CI often runs on any pull request. That means a developer is able to make CI run in an unreviewed PR. Of course for it to make its way into a protected branch (main, etc.) it'll likely need a code review but nothing is stopping that developer who opened the unreviewed PR to modify the CI yaml file in a commit to make that PR's pipeline do something different.
Requiring a team lead or someone to allow every individual PR's pipeline to run (what GitHub does by default in public repos) would add too much friction and not all major git hosts support the idea of locking down the pipelines file by decoupling it from the code repo.
Edit: Depending on which CI provider you use, this situation is mostly preventable -- "mostly" in the sense that you can control how much damage can be done. Check out this comment later in this thread: https://news.ycombinator.com/item?id=29967077
For private repositories, that means access to credentials. Probably read-only credentials, but it requires network access.
Would you be suggesting that everyone should commit all dependencies?
I'm a proponent of lock-file approaches, which gain 99% of the benefits with far less pain. It requires network access, though.
1. stand up a private package mirror that you control, that uses a whitelist for what packages it is willing to mirror;
2. configure your project's dependency-fetching logic to fetch from said mirror;
3. configure CI to only allow outbound network access to your package mirror's IP.
The disadvantage — but also the point — of this, is that it is then a release manager's responsibility, not a developer's responsibility, to give the final say-so for adding a dependency to the project (because only the release manager has the permissions to add packages to the mirror.)
I guess that's beside the point if your goal is only to reduce risk of compromised CI/CD.
I agree that there are tokens and variables that are dangerous to expose via CI, but throwing the baby out with the bathwater confused me.
Every CI system I've ever seem has pulled dependencies in from the network.
git clone --recursive --branch "$commitid" "$repourl" "$repodir"
img="$(docker build --network=none -f "$dockerfile" "$repodir")"
docker run --rm -ti --network=none "$img"
Sure, CI pulls in from the network... but execution occurs without network.Luxury! When I were a youngun our devnet was airgapped and we had to sneakernet dependencies to it. Using vhs tape on a 9track spool. Because real tape was too expensive.
More seriously I've seen jenkins and gitlab pipelines with only access to sneakernet maintained mirrors.
However, it'd probably be better if you could have the CI framework collect and inject that information into the build using some hard-coded deterministic logic, rather than giving the build itself (developer-driven Arbitrary Code Execution) access to that capability.
Same idea as e.g. injecting Kubernetes Secrets into Pods as env-vars at the controller level, rather than giving the Pod itself the permission to query Secrets out of the controller through its API.
If a developer changes the CI pipeline file to make their PR's code run in `deployment: "production"` instead of `deployment: "test"` doesn't that bypass this?
Edit:
I'll leave my original question here because I think it's an important one but I answered this myself. It depends on which CI provider you're using but some of them do let you restrict specific deployments from being run only on specific branches or by specific folks (such as repo admins).
In the above case if the production deployment was only allowed to run on the main branch and the only way code makes its way into the main branch is after at least 1 person reviewed + merged it (or whatever policy your company wants) then a rogue developer can't edit the pipeline in an unreviewed PR to make something run in production.
Also with deployment specific environment variables then a rogue developer is also not able to edit a pipeline file to try and run commands that may affect production such as doing a `terraform apply` or pushing an unreviewed Docker image to your production registry.
As a concrete example, GitLab has the concept of protected branches and code owners, both of which allow you to restrict access to the corresponding environments’ credentials to a smaller group of people who have permission to touch the sensitive branches. That allows you to say things like “anyone can run in development but only our release engineers can merge to staging/production” or “changes to the CI configuration must be approved by the DevOps team”, respectively.
That does, of course, not prevent someone from running a Bitcoin miner in whatever environment you use to run untrusted merge requests but that’s better than access to your production data.
CI should never ever have access to anything related to production; not just for security but also to prevent potentially bad code being run in tests from trashing production data.