Skip to content

fix(node): escape prompt blocks, harden prompt isolation and improve error typing#142

Merged
calchiwo merged 1 commit intomainfrom
hardening/node-prompt-escape
Feb 12, 2026
Merged

fix(node): escape prompt blocks, harden prompt isolation and improve error typing#142
calchiwo merged 1 commit intomainfrom
hardening/node-prompt-escape

Conversation

@calchiwo
Copy link
Owner

@calchiwo calchiwo commented Feb 12, 2026

This PR hardens prompt construction by escaping repository-derived content
to prevent delimiter breakout in XML-style isolation blocks.

Improves type safety by replacing any with unknown in catch blocks
and normalizing error extraction.

closes #141

@vercel
Copy link

vercel bot commented Feb 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
explainthisrepo Ready Ready Preview, Comment Feb 12, 2026 7:30pm

@gemini-code-assist
Copy link

Summary of Changes

Hello @calchiwo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the robustness and security of the application by addressing potential prompt injection vulnerabilities and improving error handling practices. It introduces a mechanism to sanitize user-controlled input before it's incorporated into AI prompts, and refines error reporting by adopting stricter TypeScript types for caught exceptions, leading to more predictable and safer code execution.

Highlights

  • Prompt Hardening: Implemented escaping for repository-derived content within prompt blocks to prevent delimiter breakout, enhancing the security and reliability of prompt construction.
  • Improved Error Typing: Replaced any with unknown in catch blocks across the codebase, improving type safety and standardizing error extraction for more robust error handling.
  • Version Update: Updated the package version from 0.4.2 to 0.4.3 in package.json and package-lock.json.
Changelog
  • node_version/cli.ts
    • Updated error handling in catch blocks to use the unknown type.
    • Standardized error message extraction for consistent logging.
  • node_version/package-lock.json
    • Updated the package version to 0.4.3.
  • node_version/package.json
    • Updated the package version to 0.4.3.
  • node_version/prompt.ts
    • Added escapeForPromptBlock function to sanitize input.
    • Applied escaping to various prompt inputs (repoName, description, readme, treeText, filesText, readmeSnippet, readmeContent, treeContent) within buildPrompt, buildQuickPrompt, and buildSimplePrompt functions.
  • package-lock.json
    • Removed the file.
  • package.json
    • Removed the file.
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@calchiwo calchiwo merged commit ef0e378 into main Feb 12, 2026
6 checks passed
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request makes some great improvements. The switch from any to unknown in catch blocks significantly improves type safety, and the standardized error message extraction is a good step. Additionally, escaping user-provided content before including it in prompts is a crucial security hardening measure to prevent prompt injection.

I've left a couple of suggestions for further improvement: one related to reducing code duplication in error handling, and another to make the prompt content escaping even more robust. Overall, this is a solid contribution to improving the codebase's safety and maintainability.

} catch (e: any) {
console.warn(`Warning: Could not read repo files: ${e?.message || e}`);
} catch (e: unknown) {
const message = e instanceof Error ? e.message : String(e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for extracting an error message is repeated in multiple catch blocks throughout this file. To improve maintainability and reduce code duplication, consider creating a utility function. For example:

function getErrorMessage(error: unknown): string {
  if (error instanceof Error) {
    return error.message;
  }
  return String(error);
}

This function could then be called in each catch block, making the code cleaner and easier to maintain.

@@ -1,3 +1,7 @@
function escapeForPromptBlock(input: string): string {
return input.replace(/</g, "&lt;").replace(/>/g, "&gt;");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While escaping < and > is crucial to prevent breaking out of the XML-style blocks, it's also a good practice to escape ampersands (&) to avoid potential misinterpretation by the LLM, as & is used to start character entities (e.g., &lt;). To make this more robust, consider escaping ampersands as well. The order is important: & should be escaped first.

Suggested change
return input.replace(/</g, "&lt;").replace(/>/g, "&gt;");
return input.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d32c16a38a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -1 +0,0 @@
{}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retarget Dependabot before removing root npm manifest

Removing the root package.json leaves no npm manifest at /, but the existing npm Dependabot config still points to that directory (.github/dependabot.yml directory: "/"). In this state the npm update job has nothing to parse and dependency update automation for Node can stop/error, so this cleanup should be paired with either moving Dependabot to /node_version or keeping a minimal root manifest.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt delimiter isolation can be broken by unescaped repository content

1 participant