Skip to content

Sequence diagram for npm publish workflow artifact validation #374

@Dargon789

Description

@Dargon789

Reviewer's Guide

Strengthens npm publish GitHub Actions workflow against artifact poisoning by enforcing artifact path containment within the isolated artifact directory and cross-checking artifact package.json metadata with the source package before publishing.

Sequence diagram for npm publish workflow artifact validation

sequenceDiagram
  actor Developer
  participant GitHubRunner
  participant WorkflowJob
  participant ArtifactStore
  participant ArtifactDir
  participant NpmRegistry

  Developer->>GitHubRunner: Push tag / trigger release
  GitHubRunner->>WorkflowJob: Start npm publish job
  WorkflowJob->>ArtifactStore: Download build artifact
  ArtifactStore-->>ArtifactDir: Extract to isolated temp directory ARTIFACT_DIR

  WorkflowJob->>ArtifactDir: Compute EXPECTED_ARTIFACT_PATH
  WorkflowJob->>ArtifactDir: realpath EXPECTED_ARTIFACT_PATH -> ABS_ARTIFACT_PATH
  WorkflowJob->>ArtifactDir: realpath ARTIFACT_DIR -> ABS_ARTIFACT_ROOT
  WorkflowJob->>WorkflowJob: Check ABS_ARTIFACT_PATH under ABS_ARTIFACT_ROOT
  alt Artifact path escapes root
    WorkflowJob-->>Developer: Fail job (artifact path outside isolated root)
  else Artifact contained
    WorkflowJob->>WorkflowJob: Verify PACKAGE_DIR and EXPECTED_ARTIFACT_PATH exist
    WorkflowJob->>ArtifactDir: Check for EXPECTED_ARTIFACT_PATH/package.json
    alt package.json present in artifact
      WorkflowJob->>WorkflowJob: Extract SRC_NAME, SRC_VERSION from PACKAGE_DIR/package.json
      WorkflowJob->>WorkflowJob: Extract ART_NAME, ART_VERSION from EXPECTED_ARTIFACT_PATH/package.json
      WorkflowJob->>WorkflowJob: Compare names and versions
      alt Name or version mismatch
        WorkflowJob-->>Developer: Fail job (refuse to publish)
      else Metadata matches
        WorkflowJob->>NpmRegistry: Run publish.mjs with PACKAGE_DIR
        NpmRegistry-->>WorkflowJob: Publish result
      end
    else No artifact package.json
      WorkflowJob->>NpmRegistry: Run publish.mjs with PACKAGE_DIR
      NpmRegistry-->>WorkflowJob: Publish result
    end
  end

  WorkflowJob-->>Developer: Report publish success or failure
Loading

Flow diagram for artifact path and package.json validation before npm publish

flowchart TD
  start([Start publish step])
  compute_paths[Compute EXPECTED_ARTIFACT_PATH and ARTIFACT_DIR]
  realpath_paths[Resolve ABS_ARTIFACT_PATH and ABS_ARTIFACT_ROOT with realpath]
  check_containment{Is ABS_ARTIFACT_PATH under ABS_ARTIFACT_ROOT?}
  fail_escape[[Fail: artifact path outside isolated artifact root]]
  check_package_dir{Does PACKAGE_DIR exist and contain package.json?}
  fail_package_dir[[Fail: invalid PACKAGE_DIR or missing package.json]]
  check_artifact_dir{Does EXPECTED_ARTIFACT_PATH exist and is directory?}
  fail_artifact_dir[[Fail: artifact directory missing]]
  has_artifact_pkg{Does EXPECTED_ARTIFACT_PATH/package.json exist?}
  use_jq{Is jq available?}
  parse_with_jq[Parse name and version from both package.json files using jq]
  parse_with_grep[Parse name and version from both package.json files using grep/sed]
  compare_names{SRC_NAME and ART_NAME both set and differ?}
  fail_name[[Fail: artifact package name does not match source]]
  compare_versions{SRC_VERSION and ART_VERSION both set and differ?}
  fail_version[[Fail: artifact package version does not match source]]
  publish[Run bun ./scripts/publish.mjs PACKAGE_DIR]
  end_step([End step])

  start --> compute_paths --> realpath_paths --> check_containment
  check_containment -- No --> fail_escape --> end_step
  check_containment -- Yes --> check_package_dir
  check_package_dir -- No --> fail_package_dir --> end_step
  check_package_dir -- Yes --> check_artifact_dir
  check_artifact_dir -- No --> fail_artifact_dir --> end_step
  check_artifact_dir -- Yes --> has_artifact_pkg

  has_artifact_pkg -- No --> publish --> end_step
  has_artifact_pkg -- Yes --> use_jq
  use_jq -- Yes --> parse_with_jq --> compare_names
  use_jq -- No --> parse_with_grep --> compare_names

  compare_names -- Yes --> fail_name --> end_step
  compare_names -- No --> compare_versions
  compare_versions -- Yes --> fail_version --> end_step
  compare_versions -- No --> publish --> end_step
Loading

File-Level Changes

Change Details Files
Add realpath-based containment check to ensure the resolved artifact directory cannot escape the isolated artifacts root before publishing.
  • Compute absolute paths for the expected artifact directory and the artifact root using realpath.
  • Use a shell case pattern to verify the artifact directory path is a child of the artifact root.
  • Abort the workflow with an error message if the artifact directory is outside the expected root.
.github/workflows/npm.yml
Validate artifact package.json name and version against the source package.json prior to running the publish script.
  • Conditionally perform validation when package.json exists inside the expected artifact path.
  • Extract name and version from both source and artifact package.jsons using jq when available, with a grep/sed fallback when jq is missing.
  • Compare extracted name and version fields and fail the workflow with clear error messages if either field mismatches between source and artifact.
.github/workflows/npm.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Originally posted by @sourcery-ai[bot] in #373 (comment)

Metadata

Metadata

Assignees

Labels

dependenciesPull requests that update a dependency filedocumentationImprovements or additions to documentationduplicateThis issue or pull request already existsenhancementNew feature or requestgithub_actionsPull requests that update GitHub Actions codegood first issueGood for newcomersrustPull requests that update rust code

Projects

Status

Backlog

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions