Preventing Source Map Leaks and Understanding AI Agent Architecture

    Source Map files can expose complete source code through their `sourcesContent` field, turning a build misconfiguration into a serious security incident. This article explains how Source Maps leak source code, outlines environment-specific Source Map strategies for Vite and Webpack, provides a pre-publish audit script to detect hidden Source Map traces, and walks through CI/CD checkpoints and incident response steps. It also examines how a recent AI coding tool source code leak offers a real-world look at AI agent code organization.

    In a recent incident, the complete source code of a well-known AI coding tool appeared on a public package registry. This was not an intentional open-source release by the vendor—it was a build misconfiguration. Source Map files were accidentally included in the published package. While this was an engineering mistake, it also gives practitioners a rare opportunity to examine how a real AI coding agent structures its code.

    Why Source Maps Leak Source Code

    A Source Map is a file that maps compiled or minified code back to its original source locations. Browser debugging tools and error monitoring platforms rely on them. The risk lies in the sourcesContent field, which stores the full content of every original source file as strings. As long as a .map file is published alongside the JavaScript artifacts, anyone can read this field and reconstruct the source code, usually with just a few lines of scripting.

    Leaks typically occur in two scenarios:

    1. Config reuse: Development or staging environments generate full Source Maps, and the team reuses the same configuration for production builds without adjusting it.
    2. Missing artifact auditing: The release process checks source code and version numbers but never inspects the final build output directory. Small .map files are easy to overlook.

    This kind of issue recurs across different teams. It indicates that a one-time fix is not enough—the check needs to be built into the release pipeline itself.

    Source Map Strategy by Environment

    Environment Recommended Strategy Notes
    Local development Generate full Source Maps For local debugging only; never enters publish artifacts
    Staging/Pre-production Generate Source Maps, upload to private error monitoring platform Debuggable; files do not enter public repositories
    Production (public registry) Disable Source Maps No .map files allowed in the publish directory

    Many developers mistakenly believe that hidden mode does not generate .map files. This is incorrect. build.sourcemap: 'hidden' in Vite and hidden-source-map in Webpack both generate .map files exactly as normal—they just omit the sourceMappingURL comment at the end of the JavaScript file. The files remain in the directory, and anyone who obtains them can still read sourcesContent. Therefore, hidden is only appropriate for scenarios where files are uploaded to a private monitoring platform. Before publishing to a public registry, you must either delete the .map files or disable Source Map generation entirely.

    For Vite production builds:

    1. // vite.config.js — for production builds published to a public registry
    2. export default {
    3. build: {
    4. sourcemap: false,
    5. },
    6. };

    For Webpack production builds, set devtool: false. The exact configuration key varies by tool version, so always re-verify the output after upgrading.

    Pre-Publish Artifact Audit

    Checking for the .map extension alone is insufficient. There are three common cases that slip through:

    • Residual sourceMappingURL comments: The comment may point to a deleted file, breaking the debugging chain, or it may point to a file that still exists, continuing to expose source locations.
    • Inline Source Maps: Mapping data is embedded directly into the JavaScript file as data:application/json;base64, with no standalone .map file in the directory.
    • sourcesContent appearing elsewhere: For example, in manually copied mapping data or other JSON files.

    The following script scans the entire build directory and returns a non-zero exit code if any trace is found:

    1. #!/usr/bin/env bash
    2. # Pre-publish check of the build directory; exits with non-zero status if any Source Map trace is found
    3. set -euo pipefail
    4. DIST_DIR="${1:-./dist}"
    5. found=0
    6. check() {
    7. local label="$1"
    8. local result="$2"
    9. if [ -n "$result" ]; then
    10. echo "Error: ${label}"
    11. echo "$result"
    12. found=1
    13. fi
    14. }
    15. check "Found .map files" "$(find "$DIST_DIR" -type f -name '*.map')"
    16. check "Found sourceMappingURL references" "$(grep -R -l 'sourceMappingURL=' "$DIST_DIR" || true)"
    17. check "Found inline Source Map" "$(grep -R -l 'data:application/json' "$DIST_DIR" || true)"
    18. check "Found sourcesContent field" "$(grep -R -l 'sourcesContent' "$DIST_DIR" || true)"
    19. if [ "$found" -ne 0 ]; then
    20. echo "Publish blocked. Fix the build configuration and retry."
    21. exit 1
    22. fi
    23. echo "Artifact check passed."

    Beyond directory checks, you must also inspect what will actually be published. npm uses the files field or .npmignore to determine package contents, so check the package tarball rather than the source tree:

    1. # List files to be published; block if any .map files are included
    2. if npm pack --dry-run --json | grep -q '\.map"'; then
    3. echo "Error: The package contains .map files"
    4. exit 1
    5. fi

    It is recommended to use a files whitelist in package.json, listing only the directories that need to be published, for example "files": ["dist/index.js"]. A whitelist is more reliable than a blacklist because newly added debug files will not be published by default.

    Checkpoints in the CI/CD Pipeline

    • Make the artifact check a mandatory step after the build and before any publish task. Publishing only runs when the check passes.
    • Issue publish tokens with minimal permissions, and restrict both the people and the pipelines that can publish.
    • After a release, periodically download the latest published package and run the check again. This catches any publishing that bypassed the pipeline.

    Incident Response After a Leak

    1. Immediately deprecate or unpublish the affected version. Each platform has different rules and time limits for unpublishing; read the documentation in advance and verify the steps in a rehearsal.
    2. Publish a fixed version and confirm the new artifacts pass the check.
    3. Notify teams that depend on the package and help them roll back to a safe version.
    4. Conduct a post-mortem, add the check to the pipeline, and document the responsible parties.

    Risk Note: Unpublishing Does Not Mean Retrieval

    Unpublishing only prevents future downloads. Copies, mirrors, and caches that have already been distributed remain in the wild and cannot be removed through platform operations. If the source code contains API keys, internal domain names, access tokens, or other credentials, treat them as compromised and rotate them immediately, regardless of whether the package has been taken down.