AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Prevent Sourcegraph Cody from Indexing Sensitive Files

羅馥伊 · 365Finds Editorial
Indexed 88,650 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

If you came here looking for .cody/ignore, we have bad news and a reality check: Sourcegraph sunsetted that experimental file format. Dropping a .cody/ignore into your repository root today does nothing. Sourcegraph Cody simply ignores the file, while you operate under a dangerous illusion of security.

At 365Finds, we look at code privacy with zero tolerance for placebo controls. When developers connect an AI assistant like Sourcegraph Cody to a proprietary codebase, the fear is tangible: API keys, database credentials, internal authentication routines, or trade secrets slipping into third-party Large Language Model (LLM) context windows.

Securing your codebase against Cody indexing requires understanding how Cody actually pulls context—from enterprise site filters down to local IDE buffers.

1. Enterprise Repository Isolation: cody.contextFilters

For organizations using Cody Enterprise (version 5.4.0 or newer), the primary administrative boundary is Context Filters. This setting lives in the Sourcegraph site configuration, not in individual repositories.

Context filters enforce access rules before code ever reaches an LLM provider (such as Anthropic Claude or OpenAI GPT-4). When an excluded repository is queried via chat, autocomplete, or inline commands, Cody halts execution and explicitly returns: file is ignored (due to cody.contextFilters Enterprise configuration setting).

How to Configure Context Filters

Administrators configure cody.contextFilters inside the Sourcegraph Management Console (Site Admin > Site Configuration) using RE2 regular expressions:

{
  "cody.contextFilters": {
    "include": [
      {
        "repoNamePattern": "^github\\.com\\/your-org\\/.*"
      }
    ],
    "exclude": [
      {
        "repoNamePattern": ".*-secrets.*"
      },
      {
        "repoNamePattern": "^github\\.com\\/your-org\\/(payment-service|auth-vault)$"
      }
    ]
  }
}

The Behavior Rules

Configuration StateCody Context Access Behavior
Not specifiedCody accesses all indexed repositories across the instance
Only include specifiedCody restricts context strictly to repositories matching the inclusion pattern
Only exclude specifiedCody accesses all repositories except those matching the exclusion pattern
Both include and excludeCody includes matching repositories minus any repository that matches the exclusion list

Here is the operational nuance 365Finds insists you know: cody.contextFilters operates at the repository level, not the individual file level. You cannot use site configuration regex to exclude config/production.json across twenty repositories while keeping the rest of those repositories indexed.

2. Codebase Index Exclusion: .sourcegraph/ignore

Cody gathers deep codebase context by querying Sourcegraph's backend code search engine and embeddings index. If a file is omitted from Sourcegraph's search index, Cody cannot retrieve it as remote context.

To block sensitive paths, directories, and file patterns from the backend index, add a .sourcegraph/ignore file to the root of your repository:

// Sensitive credentials and cryptographic keys
secrets/
*.pem
*.key
*.p12

// Local environment variables and production configs
.env*
config/production.json
config/credentials.yml

// Internal compliance and audit directories
compliance/restricted/

Key Technical Mechanics

3. Local Workspace Protection in the IDE

Server-side rules handle remote indexing, but Cody also runs locally in VS Code and JetBrains. Local context generation behaves differently.

Git Hygiene and .gitignore

Sourcegraph's code intelligence and Cody's workspace scanning rely directly on git ls-files. Files ignored by .gitignore that remain uncommitted are not tracked by Git and will not be indexed by Sourcegraph's remote engine. Ensure your root .gitignore explicitly excludes:

.env
.env.*
*.key
*.pem
*.cert
service-account-key.json

Editor Workspace Exclusions

In Visual Studio Code, Cody respects native workspace exclusion settings. In your team's .vscode/settings.json, configure:

{
  "files.exclude": {
    "**/secrets/**": true,
    "**/*.pem": true,
    "**/*.key": true
  },
  "search.exclude": {
    "**/secrets/**": true,
    "**/.env*": true
  }
}

This prevents sensitive files from appearing in local fuzzy searches and @-mention context pickers within the Cody chat interface.

The Active Tab Trap

365Finds flags this blind spot for engineering leads: open editor tabs override many exclusion boundaries.

When a developer opens a sensitive file—such as a production .env or database configuration file—directly in an editor tab, Cody treats that active document as immediate, visible workspace context. If an engineer triggers an inline chat or code autocomplete while focused on that tab, local LLM context builders can read the contents of the active buffer regardless of .sourcegraph/ignore.

If sensitive credentials must be opened for maintenance, close the active tab immediately upon finishing, or disable Cody completions for plaintext and markdown files via editor settings (cody.advanced.agent.allowedLanguages).

The 365Finds Assessment

Relying solely on AI ignore files to hide raw secrets is an architectural failure disguised as an AI policy. If your API keys, TLS certificates, or production database passwords sit in plain text inside your repository tree, you do not have a Cody indexing problem—you have a credential management breakdown.

365Finds recommends a layered security posture:

  1. Centralize secrets out of band: Extract credentials into secret managers (such as AWS Secrets Manager, HashiCorp Vault, or 1Password CLI) before Cody touches the codebase.
  2. Prevent secret commits: Enforce pre-commit hooks using gitleaks or detect-secrets to prevent accidental commits of secret patterns.
  3. Quarantine sensitive services: Use cody.contextFilters to exclude entire proprietary or payment-processing microservices at the organizational level.
  4. Deploy .sourcegraph/ignore: Commit explicit exclusion globs at the repository root to strip sensitive test fixtures and non-secret proprietary files from the backend search graph.

Sourcegraph Cody provides sufficient administrative levers to safeguard proprietary code, provided your team recognizes where repository boundaries end and secret management begins.

References