Onam

What is code security?

In short

Code security is the practice of finding and fixing security defects in everything that builds a system — application source code, open-source dependencies, infrastructure-as-code templates, container images and pipeline configuration — before it is deployed. It combines static analysis (SAST), dependency analysis (SCA), IaC scanning and secret detection, run automatically on every change.

Code security at a glance

QuestionAnswer
What does it cover?Your source code, third-party dependencies, IaC templates, container images, CI/CD configuration
Main techniquesSAST, SCA, IaC scanning, secret detection, plus DAST against the running app
When does it run?IDE and pre-commit, pull request, build, and before deploy
What does it not do?Prove a finding is exploitable in production — that needs runtime and cloud context
Related practiceSecurity as code — writing security policy itself as versioned, testable code
Where does it fit?The "code" layer of a CNAPP, feeding CSPM, KSPM and CWPP

What code security covers

Most of what runs in production was not written by your team. A typical service is your code, a large tree of open-source packages, a base image, Terraform or CloudFormation that creates the cloud resources, Helm charts or manifests that deploy it, and a pipeline that holds the credentials to do all of that. Each layer can carry a defect, and each needs a different technique to find it.

SAST vs SCA vs IaC scanning

SASTSCAIaC scanning
ScansYour own source codeThird-party and open-source dependenciesTerraform, CloudFormation, Bicep, Helm, Kubernetes manifests
FindsInjection, unsafe deserialisation, path traversal, weak cryptoPackages with known CVEs, licence problems, outdated versionsPublic storage, open security groups, missing encryption, privileged pods
Runs code?NoNoNo
Main weaknessFalse positives where it cannot prove a path is unreachableReports a vulnerable package whether or not the vulnerable function is calledOnly sees what is in the template, not changes made in the console
Typical open-source toolsSemgrep, CodeQLOWASP Dependency-Check, Trivy, GrypeCheckov, Trivy (formerly tfsec), KICS

DAST — dynamic application security testing — is the fourth technique. It exercises the running application from outside, so it finds what is genuinely reachable, including configuration and deployment problems no static tool sees, but only on paths it manages to reach.

Secret detection cuts across all of them: credentials in source, in commit history, in image layers, in pipeline variables and in committed state files. Tools such as Gitleaks and TruffleHog run as pre-commit hooks and in CI. See What is cloud secrets management?.

SBOM — a software bill of materials, in SPDX or CycloneDX format — is the inventory SCA produces. Keep one per build, so "which running services contain this package?" is a query rather than a project.

What is IaC scanning?

IaC scanning evaluates infrastructure-as-code against security policy before anything is created. It is the earliest point to catch a cloud misconfiguration: the template that will create a public bucket, rather than the public bucket.

Scan in two places. The template in the pull request catches the mistake when it is cheap to fix. The plan (terraform plan output) catches what the template resolves to once variables and modules are filled in — a module default can open a port that the template never mentions.

What is security as code?

Security as code means writing security policies, controls and tests as versioned code that runs automatically, instead of as documents and manual reviews. Examples: an OPA or Kyverno policy that rejects privileged pods; a Checkov custom rule that blocks unencrypted databases; a pipeline step that fails on a leaked credential.

The difference from code security is direction. Code security scans code for defects. Security as code turns security rules themselves into code — reviewed in pull requests, tested, and enforced by the pipeline. Mature teams do both: the scanners are the checks, and security as code is how the policy those checks enforce is written down and versioned.

Why fixing it in the console makes it come back

A posture tool reports a storage bucket with public access. An engineer opens the cloud console, unchecks the box and marks the finding resolved. The next terraform apply recreates the bucket exactly as the template describes it — public — because the template was never changed.

The finding returns, gets re-triaged, gets fixed in the console again. Everyone is working; nothing is improving.

The fix has to land where the resource is defined. That means knowing which template, repository and line produced a given running resource — connecting the code side to the runtime side rather than running two programmes that never meet.

Code security examples: three findings and their fixes

1. IaC — a bucket created without a public-access block. The scanner flags the template in the pull request. Fix in the template, not the console:

resource "aws_s3_bucket_public_access_block" "exports" {
  bucket                  = aws_s3_bucket.exports.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

2. SAST — SQL built from user input. The analyser traces a request parameter into a query string. Fix: use a parameterised query.

# Before: cursor.execute(f"SELECT * FROM orders WHERE id = {order_id}")
cursor.execute("SELECT * FROM orders WHERE id = %s", (order_id,))

3. Secret detection — a cloud access key in a config file. Deleting the line is not enough; the key is in commit history. Fix: revoke and rotate the key first, then remove it from history, then move the value to a secrets manager and block the pattern with a pre-commit hook.

Reachability is what makes the numbers usable

A dependency scan on a mature service can return hundreds of findings. Fixing them in CVSS order sounds reasonable and wastes most of the effort, because severity describes the vulnerability, not your exposure to it. Three questions change the order:

  1. Is the vulnerable function called? A critical CVE in a code path the application never executes is not a critical risk to that application.
  2. Is the workload reachable? The same container behind an internal service and behind an internet-facing load balancer carries very different risk.
  3. What can it reach next? A vulnerable workload with a read-only role is contained. The same workload with a role that can assume other roles is an entry point — see attack path analysis and CIEM.

None of those can be answered from the code alone, which is why code findings become useful when they are joined to runtime and cloud context.

Where code security belongs in the pipeline

StageWhat runsWhat it should block
IDE / pre-commitSecret detection, fast SASTAny credential, always
Pull requestSAST on the diff, SCA on changed dependencies, IaC policyNew Critical or High findings on changed code
BuildImage scan, SBOM generationBase images with known critical CVEs
Pre-deployIaC policy against the planPlans that would violate posture policy
RuntimePosture, reachability, driftNothing — it reports; blocking here breaks production

The rule that keeps this survivable: gate on the delta, not the backlog. A pipeline that fails on every pre-existing finding gets switched off. One that fails only on newly introduced findings holds the line while the backlog is worked separately.

Code security best practices

  1. Detect secrets before code leaves the laptop, not after it reaches the default branch.
  2. Prioritise SCA by reachability, not by CVSS alone.
  3. Scan IaC against the plan, so misconfigurations are prevented rather than reported.
  4. Fix at source. Every runtime finding should trace back to the template and repository that produced it.
  5. Write policy as code. Version it, review it and test it like any other code.
  6. Protect the pipeline itself. Branch protection, least-privilege CI tokens and short-lived cloud credentials through OIDC — the pipeline holds the keys to production.
  7. Keep one inventory across code and cloud, so a new CVE can be traced to running workloads in one query.

How Onam approaches code security

Onam's cleared figures in this area cover the platforms code lives on and the cloud it deploys to. GitHub and GitLab are two of its eight SaaS posture connectors, with 122 CIS rules for GitLab; in its cloud rule catalog, 502 rule definitions are tagged to secrets and key management and 241 to application and API. One honest limit: GitHub has posture checks but no CIS rule pack yet.

Frequently asked questions

What is code security?

Code security is finding and fixing security defects in everything that builds a system — source code, open-source dependencies, infrastructure-as-code, container images and pipeline configuration — before deployment, using SAST, SCA, IaC scanning and secret detection on every change.

What is the difference between SAST, SCA and IaC scanning?

SAST analyses your own source code for insecure patterns such as injection. SCA inventories third-party dependencies and matches them against known vulnerabilities. IaC scanning checks infrastructure templates such as Terraform and Kubernetes manifests for misconfigurations before they are deployed. Each finds a different class of problem.

What is the difference between SAST and DAST?

SAST reads source code without running it and can see paths that tests never execute. DAST tests the running application from outside and finds what is genuinely reachable, including deployment problems, but only on the paths it manages to exercise. They complement each other.

What is security as code?

Security as code is writing security policies, controls and tests as versioned code that runs automatically in the pipeline — for example an admission policy that rejects privileged pods, or a rule that blocks unencrypted databases — instead of relying on documents and manual review.

What is IaC scanning?

IaC scanning evaluates infrastructure-as-code — Terraform, CloudFormation, Bicep, Helm charts and Kubernetes manifests — against security policy before resources are created, so misconfigurations such as public storage or open security groups are caught in the pull request.

Why do cloud misconfigurations come back after being fixed?

Because they were fixed in the console while the infrastructure-as-code template that created them was left unchanged. The next apply recreates the original configuration. A fix only holds when it lands where the resource is defined.

Should a build fail on every security finding?

No. Gate on newly introduced findings and work the existing backlog separately. A pipeline that fails on every pre-existing issue tends to be switched off or bypassed, which is worse than no gate.

What is reachability analysis in dependency scanning?

Determining whether the vulnerable function in a dependency is actually invoked by your application, and whether the workload is exposed. It turns a long dependency report into a short list you can act on.

See it on your own cloud

Connect a read-only role for posture scanning. Your first findings arrive with the first scan — across seven clouds and your SaaS platforms.