5 tips to keep your code secure

5 tips to keep your code secure

As the ownership of application security shifts left into developers’ hands, building security into your CI/CD pipeline is increasingly critical…

This post was written by Sarah Conway from Snyk, our marketplace partner. Find and fix security vulnerabilities faster with the Bitbucket-Snyk integration.

As the ownership of application security shifts left into developers’ hands, building security into your CI/CD pipeline is increasingly critical.  When security is automated and adopted early, you’re able to sustain fast development and rapid software delivery. As you look to embed security early and throughout your CI/CD pipeline, these tips will help you improve the security of your Bitbucket deployments, with no workflow interruptions.

1. Never store credentials as code/config

Passwords in your publicly available code can easily get into the wrong hands, which is why it’s best to avoid putting credentials into your repository in the first place. If some do sneak in though, fortunately there are a bunch of great tools available:

2. Remove sensitive data from your files and Bitbucket history

It’s best to avoid putting sensitive data in your Bitbucket repository so others aren’t able to see it. But if this does happen, you need to do a number of things to recover. First, invalidate the tokens and passwords that were exposed. Once a secret is public on the internet, you should assume it’s in the hands of attackers and react accordingly.

Next, remove the sensitive data from your repository, but don’t forget that Bitbucket keeps a full history of all your commits. This includes changelogs that list your sensitive information. So it’s important you clear your Bitbucket history after you remove sensitive data from a repo. For more info, see purging files from your repository’s history. Note, while this is a GitHub link, it’s good generic advice for all Git repositories, so you can use these tips in Bitbucket too. 

3. Tightly control access

We often focus on complex attack vectors, but fail miserably against some of the simplest ones when securing our applications. For instance, it only takes one developer to leave their password on a sticky note, hanging off their monitor for an attacker to gain access. It’s important to ensure our basic settings and practices are adhered to, both on the Bitbucket platform as well as in general. Mandate the following basic practices for your contributors:

  1. Require 2-factor-authentication on every contributor’s Bitbucket account.
  2. Never let Bitbucket users share accounts/passwords.
  3. Any laptops/devices with access to your source code must be properly secured.
  4. Repository administrators should manage team access to data. Only give contributors access to the data they need to do their work.
  5. Bitbucket accounts may be personal accounts, and do not naturally disappear when users leave the company. Make sure you diligently revoke access from Bitbucket users who are no longer working with you.
  6. Use Bitbucket’s Branch Permissions model to control who can push commits to which branches.

4. Add a SECURITY.md file

It’s natural for most project owners and maintainers to add a README.md for their repository. Likewise, you should include a SECURITY.md file that highlights security related information for your project. This file should include the following: 

5. Quickly find vulnerabilities within your Bitbucket workflow 

Snyk provides a Bitbucket integration that runs a scan against all opened pull requests to ensure they aren’t introducing new open source vulnerabilities. 

If a new vulnerability is found, Snyk alerts you about the newly found vulnerability and opens a fix pull request, including suggested upgrades or Snyk patches to fix the vulnerability.

In Bitbucket’s pull request interface, changes are scanned and results are shown as detailed in-line annotations next to the changes that introduce new issues.  These annotations make it easier to understand the results of Snyk’s scan and support informed decisions, as shown below.

Start securing your code

Snyk provides native testing and fixing of open source dependencies that developers can use to automate their workflow through Bitbucket Pipelines. Learn more about the Snyk-Bitbucket integration and sign up for a free starter Snyk plan.

Exit mobile version