Skip to content

feat: prompt protect, close #850 wip#1109

Merged
looplj merged 1 commit intorelease/v0.9.xfrom
dev-tmp
Mar 20, 2026
Merged

feat: prompt protect, close #850 wip#1109
looplj merged 1 commit intorelease/v0.9.xfrom
dev-tmp

Conversation

@looplj
Copy link
Copy Markdown
Owner

@looplj looplj commented Mar 20, 2026

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 delivers a crucial security enhancement by introducing a 'Prompt Protection' system. It empowers users to define and manage rules that automatically detect and handle sensitive information in prompts, either by masking it or rejecting the prompt entirely. This ensures that confidential data is not inadvertently exposed to language models, thereby improving data privacy and compliance across all LLM interactions.

Highlights

  • New Feature: Prompt Protection: Introduced a comprehensive 'Prompt Protection' feature allowing users to define regex-based rules to automatically mask or reject sensitive content within prompts before they are processed by LLMs.
  • Frontend Management UI: Developed a new frontend page and associated components for managing prompt protection rules, including creation, editing, deletion, bulk actions (enable/disable/delete), and status changes, integrated into the sidebar navigation.
  • Backend API and Business Logic: Implemented a new PromptProtectionRule entity within the Ent framework, complete with GraphQL API endpoints for CRUD operations, bulk actions, and status updates. Added business logic for rule validation and listing enabled rules.
  • LLM Request Orchestration Integration: Integrated the prompt protection logic into the LLM request orchestration pipeline. Prompts are now processed against active rules, with matching content either masked or the request rejected based on the rule's configuration and message role scopes.
  • Internationalization Support: Added new localization strings for the Prompt Protection feature in both English and Simplified Chinese to support a global user base.
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.

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.

Copy link
Copy Markdown
Contributor

@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 introduces a new prompt protection feature, allowing regex-based rules to mask or reject content in prompts. The changes span both the frontend and backend, including new UI components, API endpoints, database schema, and business logic.

My review has identified several areas for improvement, primarily on the backend. There are a few unimplemented resolvers that would cause panics at runtime, which I've flagged as high severity. I've also noted a performance concern regarding regular expression compilation in the request path and suggested a caching strategy. I've provided code suggestions for the unimplemented parts to help complete the feature.

Comment on lines +212 to +214
func (r *promptProtectionRuleResolver) ID(ctx context.Context, obj *ent.PromptProtectionRule) (*objects.GUID, error) {
panic(fmt.Errorf("not implemented: ID - id"))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This resolver is not implemented and will cause a panic at runtime. It should be implemented to correctly encode the integer ID into a GraphQL GUID object.

func (r *promptProtectionRuleResolver) ID(ctx context.Context, obj *ent.PromptProtectionRule) (*objects.GUID, error) {
	return &objects.GUID{
		Prefix: "ppr",
		ID:     obj.ID,
	}, nil
}

Comment on lines +351 to +353
func (r *queryResolver) PromptProtectionRules(ctx context.Context, after *entgql.Cursor[int], first *int, before *entgql.Cursor[int], last *int, orderBy *ent.PromptProtectionRuleOrder, where *ent.PromptProtectionRuleWhereInput) (*ent.PromptProtectionRuleConnection, error) {
panic(fmt.Errorf("not implemented: PromptProtectionRules - promptProtectionRules"))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This query resolver is not implemented and will cause a panic at runtime. It needs to be implemented to fetch and paginate PromptProtectionRule entities.

func (r *queryResolver) PromptProtectionRules(ctx context.Context, after *entgql.Cursor[int], first *int, before *entgql.Cursor[int], last *int, orderBy *ent.PromptProtectionRuleOrder, where *ent.PromptProtectionRuleWhereInput) (*ent.PromptProtectionRuleConnection, error) {
	return r.client.PromptProtectionRule.Query().Paginate(ctx, after, first, before, last,
		ent.WithPromptProtectionRuleOrder(orderBy),
		ent.WithPromptProtectionRuleFilter(where.Filter),
	)
}

Comment on lines +73 to +90
func (r *promptProtectionSettingsResolver) Action(ctx context.Context, obj *objects.PromptProtectionSettings) (string, error) {
panic(fmt.Errorf("not implemented: Action - action"))
}

// Scopes is the resolver for the scopes field.
func (r *promptProtectionSettingsResolver) Scopes(ctx context.Context, obj *objects.PromptProtectionSettings) ([]string, error) {
panic(fmt.Errorf("not implemented: Scopes - scopes"))
}

// Action is the resolver for the action field.
func (r *promptProtectionSettingsInputResolver) Action(ctx context.Context, obj *objects.PromptProtectionSettings, data string) error {
panic(fmt.Errorf("not implemented: Action - action"))
}

// Scopes is the resolver for the scopes field.
func (r *promptProtectionSettingsInputResolver) Scopes(ctx context.Context, obj *objects.PromptProtectionSettings, data []string) error {
panic(fmt.Errorf("not implemented: Scopes - scopes"))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

These resolvers for PromptProtectionSettings and PromptProtectionSettingsInput are not implemented and will cause panics at runtime. They should be implemented to handle the type conversions between the Go objects and the GraphQL schema.

func (r *promptProtectionSettingsResolver) Action(ctx context.Context, obj *objects.PromptProtectionSettings) (string, error) {
	return string(obj.Action), nil
}

// Scopes is the resolver for the scopes field.
func (r *promptProtectionSettingsResolver) Scopes(ctx context.Context, obj *objects.PromptProtectionSettings) ([]string, error) {
	return lo.Map(obj.Scopes, func(s objects.PromptProtectionScope, _ int) string {
		return string(s)
	}), nil
}

// Action is the resolver for the action field.
func (r *promptProtectionSettingsInputResolver) Action(ctx context.Context, obj *objects.PromptProtectionSettings, data string) error {
	obj.Action = objects.PromptProtectionAction(data)
	return nil
}

// Scopes is the resolver for the scopes field.
func (r *promptProtectionSettingsInputResolver) Scopes(ctx context.Context, obj *objects.PromptProtectionSettings, data []string) error {
	obj.Scopes = lo.Map(data, func(s string, _ int) objects.PromptProtectionScope {
		return objects.PromptProtectionScope(s)
	})
	return nil
}

Comment on lines +68 to +75
func MatchPromptProtectionRule(pattern string, content string) bool {
re, err := regexp.Compile(pattern)
if err != nil {
return false
}

return re.MatchString(content)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Compiling a regular expression on every call to MatchPromptProtectionRule is inefficient and can become a performance bottleneck, as this function is in the request path. Additionally, the error from regexp.Compile is currently being swallowed, which could hide issues with invalid patterns stored in the database.

I recommend caching the compiled regular expressions to improve performance. You could use a sync.Map for a thread-safe, package-level cache. Also, any compilation error should be logged to aid in debugging.

A similar issue exists in ReplacePromptProtectionRule on line 77.

@looplj looplj force-pushed the dev-tmp branch 2 times, most recently from e62607f to c2fffea Compare March 20, 2026 14:33
@looplj looplj merged commit 9c4bcf4 into release/v0.9.x Mar 20, 2026
2 checks passed
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.

1 participant