-
Notifications
You must be signed in to change notification settings - Fork 59
Add issue assistant workflow for automated triage #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc7e21c
Add issue assistant workflow for automated triage
DimaBir 1877928
Enhance issue assistant workflow with better logging
DimaBir e05c59d
Add workflow to refresh wiki cache daily
DimaBir 8757e58
Add security validation module for issue assistant
DimaBir a794890
Enhance issue assistant workflow with protections and fixes
DimaBir 0c5ac42
Refactor security.js to enhance regex safety and reduce comments
DimaBir 789fbc7
Refactor issue assistant workflow for clarity and safety
DimaBir dbe9ab8
Update wiki cache workflow for date in commit message
DimaBir 38b4371
Update issue-assistant.yml
DimaBir efefaf8
Document security validation module design and patterns
DimaBir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| const DEFAULT_INJECTION_PATTERNS = [ | ||
| /ignore\s+(all\s+)?(previous|prior)/i, | ||
| /disregard\s+(your\s+)?instructions/i, | ||
| /you\s+are\s+now/i, | ||
| /pretend\s+(to\s+be|you)/i, | ||
| /system\s*prompt/i, | ||
| /jailbreak/i, | ||
| /<\|.*\|>/i, | ||
| /\[\[.*\]\]/i, | ||
| ]; | ||
|
|
||
| const DEFAULT_SUSPICIOUS_PATTERNS = [ | ||
| /\@(dependabot|github-actions)/i, | ||
| /merge\s+this/i, | ||
| /webhook/i, | ||
| ]; | ||
|
|
||
| function compilePatterns(secretPatterns, defaultPatterns) { | ||
| if (secretPatterns && Array.isArray(secretPatterns)) { | ||
| return secretPatterns.map(p => { | ||
| if (typeof p === 'string') { | ||
| const match = p.match(/^\/(.*)\/([gimsuy]*)$/); | ||
| if (match) { | ||
| const safeFlags = match[2].replace(/[gy]/g, ''); | ||
| return new RegExp(match[1], safeFlags); | ||
| } | ||
| return new RegExp(p, 'i'); | ||
| } | ||
| if (p instanceof RegExp) { | ||
| const safeFlags = p.flags.replace(/[gy]/g, ''); | ||
| return new RegExp(p.source, safeFlags); | ||
| } | ||
| return p; | ||
| }); | ||
| } | ||
| return defaultPatterns; | ||
| } | ||
|
|
||
| function detectPromptInjection(content, customPatterns) { | ||
| const patterns = compilePatterns(customPatterns, DEFAULT_INJECTION_PATTERNS); | ||
| const normalizedContent = content | ||
| .replace(/\s+/g, ' ') | ||
| .replace(/[^\x20-\x7E\s]/g, ' '); | ||
|
|
||
| const detected = []; | ||
| for (const pattern of patterns) { | ||
| if (pattern.test(normalizedContent)) { | ||
| detected.push('pattern_match'); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| detected: detected.length > 0, | ||
| count: detected.length | ||
| }; | ||
| } | ||
|
|
||
| function detectSuspiciousContent(content, customPatterns) { | ||
| const patterns = compilePatterns(customPatterns, DEFAULT_SUSPICIOUS_PATTERNS); | ||
| const detected = []; | ||
|
|
||
| for (const pattern of patterns) { | ||
| if (pattern.test(content)) { | ||
| detected.push('suspicious_match'); | ||
| } | ||
| } | ||
|
|
||
| const words = content.toLowerCase().split(/\s+/); | ||
| const wordCounts = {}; | ||
| for (const word of words) { | ||
| wordCounts[word] = (wordCounts[word] || 0) + 1; | ||
| } | ||
| const maxRepetition = Math.max(...Object.values(wordCounts), 0); | ||
| if (maxRepetition > 50) { | ||
| detected.push('excessive_repetition'); | ||
| } | ||
|
|
||
| return { | ||
| detected: detected.length > 0, | ||
| count: detected.length | ||
| }; | ||
| } | ||
|
|
||
| async function checkRateLimit(github, context, userId, limitPerHour) { | ||
| const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000).toISOString(); | ||
|
|
||
| try { | ||
| let responseCount = 0; | ||
| let page = 1; | ||
| const perPage = 100; | ||
|
|
||
| while (true) { | ||
| const { data: comments } = await github.rest.issues.listCommentsForRepo({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| since: oneHourAgo, | ||
| per_page: perPage, | ||
| page: page | ||
| }); | ||
|
|
||
| if (comments.length === 0) break; | ||
|
|
||
| for (const comment of comments) { | ||
| if (comment.body && comment.body.includes('<!-- msdo-issue-assistant -->')) { | ||
| try { | ||
| const { data: issue } = await github.rest.issues.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: comment.issue_url.split('/').pop() | ||
| }); | ||
|
|
||
| if (issue.user && issue.user.id === userId) { | ||
| responseCount++; | ||
| } | ||
| } catch (e) { | ||
| responseCount++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (comments.length < perPage) break; | ||
| page++; | ||
|
|
||
| if (page > 10) break; | ||
| } | ||
|
|
||
| return { | ||
| allowed: responseCount < limitPerHour, | ||
| currentCount: responseCount | ||
| }; | ||
| } catch (error) { | ||
| console.error('Rate limit check failed:', error.message); | ||
| return { allowed: false, error: error.message }; | ||
| } | ||
| } | ||
|
|
||
| function sanitizeInput(content, maxLength) { | ||
| if (!content) return ''; | ||
|
|
||
| let sanitized = content | ||
| .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '') | ||
| .replace(/[^\S\r\n]+/g, ' ') | ||
| .replace(/\n{3,}/g, '\n\n') | ||
| .trim(); | ||
|
|
||
| if (sanitized.length > maxLength) { | ||
| sanitized = sanitized.substring(0, maxLength) + '... [truncated]'; | ||
| } | ||
|
|
||
| return sanitized; | ||
| } | ||
|
|
||
| function detectIssueType(title, body) { | ||
| const content = (title + ' ' + body).toLowerCase(); | ||
|
|
||
| const bugScore = ['bug', 'error', 'fail', 'crash', 'broken', 'not working'].filter(w => content.includes(w)).length; | ||
| const featureScore = ['feature', 'request', 'enhancement', 'suggestion', 'add support'].filter(w => content.includes(w)).length; | ||
| const questionScore = ['how to', 'how do', 'question', 'help', 'possible'].filter(w => content.includes(w)).length; | ||
|
|
||
| if (bugScore === 0 && featureScore === 0 && questionScore === 0) return 'unknown'; | ||
| if (bugScore >= featureScore && bugScore >= questionScore) return 'bug'; | ||
| if (featureScore >= questionScore) return 'feature'; | ||
| return 'question'; | ||
|
DimaBir marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async function validateRequest({ | ||
| github, | ||
| context, | ||
| maxInputLength, | ||
| rateLimitPerHour, | ||
| customInjectionPatterns, | ||
| customSuspiciousPatterns | ||
| }) { | ||
| const errors = []; | ||
| const issue = context.payload.issue; | ||
| const comment = context.payload.comment; | ||
|
|
||
| const content = comment ? comment.body : issue.body; | ||
| const title = issue.title || ''; | ||
| const userId = comment ? comment.user.login : issue.user.login; | ||
| const userIdNum = comment ? comment.user.id : issue.user.id; | ||
| const userType = comment ? comment.user.type : issue.user.type; | ||
|
|
||
| if (userType === 'Bot') { | ||
| errors.push('Bot users not processed'); | ||
| return { shouldRespond: false, errors }; | ||
| } | ||
|
|
||
| if (!content || content.length === 0) { | ||
| errors.push('Empty content'); | ||
| return { shouldRespond: false, errors }; | ||
| } | ||
|
|
||
| if (content.length > maxInputLength) { | ||
| errors.push('Content exceeds maximum length'); | ||
| } | ||
|
|
||
| const injectionCheck = detectPromptInjection(content, customInjectionPatterns); | ||
| if (injectionCheck.detected) { | ||
| errors.push('Potential prompt injection detected'); | ||
| console.log('Injection attempt from ' + userId + ': ' + injectionCheck.count + ' patterns matched'); | ||
| } | ||
|
|
||
| const suspiciousCheck = detectSuspiciousContent(content, customSuspiciousPatterns); | ||
| if (suspiciousCheck.detected) { | ||
| errors.push('Suspicious content detected'); | ||
| } | ||
|
|
||
| const rateLimit = await checkRateLimit(github, context, userIdNum, rateLimitPerHour); | ||
| if (!rateLimit.allowed) { | ||
| errors.push('Rate limit exceeded'); | ||
| } | ||
|
|
||
| if (comment) { | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number | ||
| }); | ||
|
|
||
| const botComments = comments.filter(c => | ||
| c.body && c.body.includes('<!-- msdo-issue-assistant -->') | ||
| ); | ||
|
|
||
| if (botComments.length >= 3) { | ||
| errors.push('Maximum bot responses reached'); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| shouldRespond: errors.length === 0, | ||
| errors, | ||
| sanitizedContent: sanitizeInput(content, maxInputLength), | ||
| issueType: detectIssueType(title, content) | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| validateRequest, | ||
| detectPromptInjection, | ||
| detectSuspiciousContent, | ||
| sanitizeInput, | ||
| detectIssueType, | ||
| checkRateLimit | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.