Skip to content

cbs-consulting/cds-guard

v0.1.1Apache-2.0

Sets up @cbs-consulting/cds-guard in CAP and UI5 projects and keeps coding agents from changing protected CDS entities, annotations, code blocks and files by accident.

cbs CDS Guard

Nobody changes a protected CDS entity, annotation or code block by accident, on any Git platform.

npm License Node.js SAP CAP Platforms


cds-guard protects the parts of a CAP or UI5 project that must not change without the right people noticing: a CDS entity or a single element, a Fiori annotation, a block of code, a file or a value in manifest.json. You list them in .guard/protected.yml, together with who may approve a change.

cds-guard lock stores a fingerprint of each protected item in a lock file. cds-guard check runs in the pull request pipeline and fails when a protected item no longer matches. A deliberate change updates the lock file, and the lock file can only be merged with approval from the rule's approvers, through your Git platform's own code owners. No API tokens, no platform-specific code: the same setup works on GitHub, Azure DevOps, Bitbucket and GitLab.

$ npx cds-guard check
cds-guard: protected items changed.

✗ rule "pricing-model" (Prices and order amounts)
    changed       cds:my.app.Orders.netAmount  content changed
    changed       annotation:my.app.Orders:@UI.LineItem  content changed
  If this change is intended: run "npx cds-guard lock --rule pricing-model", commit the lock file and get approval from @my-org/team-pricing.

✨ What works where

What can be protected

WhatExample in protected.ymlCounts as a changeDoesn't count
CDS entity, type, service, aspectcds: [ my.app.Prices ]elements, types, keys, associations, annotations on it (from any file)comments, formatting, file moves
CDS elementcds: [ my.app.Orders.netAmount ]its type, length, default, annotationsother elements of the entity
CDS structure onlyignoreAnnotations: truetypes and elementsannotations and doc comments
CDS annotationannotations: [ "my.app.Orders:@UI.LineItem" ]the annotation, its qualified variants (#Compact) and sub-terms, also when added in another .cds fileeverything else on the entity
Code blockmarkers: [ tax-calc ]anything between @protected-begin tax-calc and @protected-end, a removed or extra blocktrailing spaces, line endings, code around the block
Filepaths: [ "app/*/webapp/annotations/*.xml" ]content, a new file matching the pattern, a deleted fileline endings, trailing spaces
JSON valuejson: [{ file: …/manifest.json, pointers: [ /sap.app/id ] }]the value at the pointerformatting, other values

CDS items are compared on the compiled model (CSN) that @sap/cds builds, so an annotate in another file is caught and a reformat is not. Markers work in any language that has comments: //, #, /* */, <!-- -->.

Platforms

PlatformPipelineApproval of lock filesGenerated by cds-guard codeowners
GitHubaction.ymlCODEOWNERS + "Require review from Code Owners".github/CODEOWNERS block
Azure DevOpsci/azure-pipelines/cds-guard.ymlBranch policy "Automatically included reviewers", required, with path filteraz repos policy required-reviewer script
Bitbucketci/bitbucket/bitbucket-pipelines.ymlCODEOWNERS (enforced with a merge check; check your plan).bitbucket/CODEOWNERS block
GitLabci/gitlab/cds-guard.gitlab-ci.ymlCODEOWNERS + "Code owner approval" (Premium)CODEOWNERS block with a [cds-guard] section
Anything else (Jenkins, …)npx @cbs-consulting/cds-guard checkwhatever path-based reviewers the platform has–

🚀 Quick start

npm install --save-dev @cbs-consulting/cds-guard # your project already has @sap/cds
npx cds-guard init                               # creates .guard/protected.yml
# add rules to .guard/protected.yml
npx cds-guard lock                               # writes .guard/locks/*.lock
npx cds-guard codeowners --write                 # or --platform azure|gitlab|bitbucket
npx cds-guard skill --write                      # optional: teaches AI coding agents the guard
git add .guard .github/CODEOWNERS && git commit -m "Protect the pricing model"

Then add the check to your pipeline and make it required (see Set up your platform).

📝 Configuration

.guard/protected.yml:

version: 1

cds:
  root: .                    # where the CAP project is (default: the repository root)
  # model: [ db, srv, app ]  # load these folders or files instead of the CAP defaults

# Who must approve changes to the guard itself: .guard/protected.yml and the lock files of
# rules without approvers.
guardOwners: [ "@my-org/leads" ]

# Further files only the guard owners may change, such as the pipeline that runs cds-guard.
guardPaths: [ .github/workflows/cds-guard.yml ]

# Extra glob patterns to skip when looking for files and markers.
ignore: [ "test/**" ]

rules:
  - id: pricing-model
    description: Prices and order amounts
    cds: [ my.app.Prices, my.app.Orders.netAmount ]
    annotations: [ "my.app.Orders:@UI.LineItem", "my.app.Orders:@UI.HeaderInfo" ]
    approvers: [ "@my-org/team-pricing", alice ]

  - id: tax-formula
    markers: [ tax-calc ]
    markerFiles: [ "srv/**/*.js" ]   # where to look (default: all text files)
    approvers: [ bob@example.com ]

  - id: frozen-ui
    paths: [ "app/orders/webapp/annotations/*.xml" ]
    json:
      - file: app/orders/webapp/manifest.json
        pointers: [ /sap.app/id, /sap.ui5/routing ]
KeyMeaning
idName of the rule and its lock file .guard/locks/<id>.lock. Letters, digits, ., _, -.
cdsFully qualified definitions (ns.Entity) or elements (ns.Entity.element, also nested ns.Entity.struct.field).
ignoreAnnotationsCompare cds items without their annotations and doc comments.
annotationstarget:@Term, where target is a definition or element. @UI.HeaderInfo covers @UI.HeaderInfo.TypeName and @UI.HeaderInfo#Q.
markers, markerFilesMarker ids and the glob patterns of files to search.
pathsGlob patterns of whole files.
jsonFiles and JSON pointers inside them.
approversWho approves changes, in the format of your platform: @user, @org/team, e-mail address, or for Azure DevOps e-mail addresses and group names. Without approvers, guardOwners approve.

examples/ has a complete configuration for each platform, with different kinds of rules and the ways each platform names users, groups and teams.

Changing which items a rule protects needs a new lock (cds-guard check reports scope-changed). Changing only approvers or description does not; run cds-guard codeowners --write instead.

Marker example:

module.exports = function tax (net) {
  // @protected-begin tax-calc
  const rate = 0.19
  return Math.round(net * rate * 100) / 100
  // @protected-end
}

🔧 Commands

CommandWhat it does
cds-guard initCreates .guard/protected.yml with examples.
cds-guard lock [--rule ID]...Writes the lock files of all rules, or of the given ones, and lists the items that changed. Refuses items that can't be found.
cds-guard check [--format F] [--junit FILE] [--codequality FILE] [--base REF]Compares with the lock files. Exit code 1 when something changed, or when a generated CODEOWNERS block or Azure script no longer matches protected.yml. See Check output.
cds-guard codeowners [--platform P] [--write [FILE]] [--branch B] [--check]Prints or writes the approver entries. Only the block between # BEGIN cds-guard and # END cds-guard is replaced, the rest of CODEOWNERS stays. --check only reports whether the written blocks are up to date.
cds-guard listShows every protected item per rule, and which ones can't be found.
cds-guard skill [--write [DIR]]Prints or writes the agent skill to DIR/cds-guard/SKILL.md (default .claude/skills).

All commands take --root DIR. Exit codes: 0 ok, 1 protected items changed, 2 usage or configuration error.

Check output

OptionWhat it adds
--base REFFor each changed item, a short diff of how it looked at the git ref REF (a branch, tag or commit). A shallow clone fetches the commit from origin first. If git can't find it, check warns and carries on.
--format githubGitHub Actions annotations: each finding is marked on its file and line in the pull request.
--format azureThe same for Azure Pipelines (logging commands).
--format json, --format junitThe whole result as JSON or as a JUnit report on standard output.
--junit FILE, --codequality FILEAlso write a JUnit report, or a GitLab Code Quality report for the merge request widget, to FILE.

The GitHub Action and the CI templates in ci/ already pass the target branch as --base and use the matching format.

✗ rule "tax-formula"
    changed       marker:tax-calc@srv/tax.js  block starting at line 2 changed
        -   const rate = 0.19
        +   const rate = 0.2
            return Math.round(net * rate * 100) / 100

🏗️ Set up your platform

Three things make the guard binding. Without them it is only a warning.

  1. Main only accepts pull requests. Nobody pushes directly.
  2. The cds-guard job is a required check for merging.
  3. The lock files need approval from the rule's approvers, through cds-guard codeowners.

GitHub

# .github/workflows/cds-guard.yml
name: cds-guard
on: [pull_request, merge_group]
jobs:
  cds-guard:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22 }
      - uses: cbs-consulting/cds-guard@v0

In a branch ruleset (or branch protection) for main: "Require a pull request before merging", "Require review from Code Owners", and "Require status checks to pass" with cds-guard. Then npx @cbs-consulting/cds-guard codeowners --write. List the workflow file in guardPaths so nobody can switch the check off unnoticed.

The action marks each finding on its line in the pull request and shows how changed items looked on the base branch. Set diff: false to leave the diffs out.

Azure DevOps

Copy ci/azure-pipelines/cds-guard.yml into the repository and reference it from a pipeline (the comment at the top shows how). In the branch policies of main: add the pipeline as a required Build validation. Then create the reviewer policies once:

npx cds-guard codeowners --platform azure --write   # writes .guard/azure-policies.sh
REPO_ID=$(az repos show --repository my-repo --query id -o tsv) sh .guard/azure-policies.sh

Run both commands again after approvers change. The script creates the new policies and then removes the ones an earlier run created. check fails while .guard/azure-policies.sh doesn't match protected.yml.

The pipeline fetches the target branch to show what changed. If your checkout step doesn't keep its credentials, add persistCredentials: true or fetchDepth: 0 to it; without either, check still works and only leaves out the diffs.

Bitbucket

Merge ci/bitbucket/bitbucket-pipelines.yml into your pipeline. In the branch restrictions of main: prevent changes without a pull request, and add the merge checks for successful builds and code owner approval (enforcing merge checks needs Bitbucket Cloud Premium, or Data Center). Then npx @cbs-consulting/cds-guard codeowners --platform bitbucket --write, which writes .bitbucket/CODEOWNERS.

GitLab

Include ci/gitlab/cds-guard.gitlab-ci.yml. Turn on "Pipelines must succeed", protect main with "Allowed to push and merge: No one" and "Code owner approval" (Premium), then npx cds-guard codeowners --platform gitlab --write.

Other CI systems

Run npx @cbs-consulting/cds-guard check after npm ci, so the project's own @sap/cds compiles the model. On an agent without Node.js, run both in a node:22 container:

docker run --rm -v "$PWD:/repo" -w /repo node:22 sh -c "npm ci && npx --yes @cbs-consulting/cds-guard check"

🤖 AI coding agents

The package ships an agent skill that teaches coding agents to work with the guard: set it up when asked, check whether a file is protected before editing it, run check afterwards, and lock only changes the user asked for, never to silence a failing check.

npx @cbs-consulting/cds-guard skill --write                 # .claude/skills/cds-guard/SKILL.md
npx @cbs-consulting/cds-guard skill --write .agents/skills  # another agent's skills folder

The repository is also a plugin in the cbs Agentic Marketplace, so you can install the skill once for all your projects:

/plugin marketplace add cbs-consulting/agentic-marketplace     # Claude Code
/plugin install cds-guard@cbs-consulting
copilot plugin marketplace add cbs-consulting/agentic-marketplace  # GitHub Copilot CLI
copilot plugin install cds-guard@cbs-consulting
codex plugin marketplace add cbs-consulting/agentic-marketplace    # Codex, then /plugins

The plugin follows the default branch of this repository. skill --write gives you the copy that matches your installed version instead.

Claude Code picks it up from .claude/skills/. Other agents that read the Agent Skills format take the folder they look in. Commit the file so the whole team gets it. The skill belongs to the installed version: run the command again after an upgrade, which replaces the file. To use the skill without installing the package, copy skills/cds-guard/SKILL.md.

❓ How it works and its limits

  • Fingerprints, not diffs. Each item is hashed: CDS items as canonical JSON of the compiled model without $location and other $ metadata, text as normalised lines. check compares the working tree with the lock files, so it needs no base branch and works with shallow clones.
  • The project's own @sap/cds compiles the model, so the hashes match what the app builds. The lock file records the @sap/cds version; after an upgrade, check warns if the version differs. If the upgrade changes how the model compiles, run cds-guard lock and approve it like any other change.
  • It is a merge gate, not access control. Anyone can edit protected code locally. What stops the merge is the required check plus the required reviewers on the lock files. People who may bypass branch rules can still merge.
  • Approver files stay in sync. check compares the generated CODEOWNERS block (in any place a platform reads it from) and .guard/azure-policies.sh with protected.yml, and fails when they differ. The block also makes the guard owners the owners of the CODEOWNERS file itself, so removing it needs their approval.
  • Renames look like deletions. A renamed entity is reported as missing; update the rule and lock again.

🤝 Contributing

See CONTRIBUTING.md. Security problems go through the Security Policy, not public issues.

📄 License

Apache License 2.0, © 2026 cbs Corporate Business Solutions Unternehmensberatung GmbH.