feat: prompt protect, close #850 wip#1109
Conversation
Summary of ChangesHello, 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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| func (r *promptProtectionRuleResolver) ID(ctx context.Context, obj *ent.PromptProtectionRule) (*objects.GUID, error) { | ||
| panic(fmt.Errorf("not implemented: ID - id")) | ||
| } |
There was a problem hiding this comment.
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
}| 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")) | ||
| } |
There was a problem hiding this comment.
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),
)
}| 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")) | ||
| } |
There was a problem hiding this comment.
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
}| func MatchPromptProtectionRule(pattern string, content string) bool { | ||
| re, err := regexp.Compile(pattern) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return re.MatchString(content) | ||
| } |
There was a problem hiding this comment.
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.
e62607f to
c2fffea
Compare
No description provided.