-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(cli): prevent file paths from being treated as slash commands #3743
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
Changes from all commits
c3504db
776a2ab
7cf3eae
a13d5ba
e0cb5d6
abc8965
579274b
08d9043
4ee65bb
36ac8c4
9a279ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,10 @@ import { FileCommandLoader } from '../../services/FileCommandLoader.js'; | |
| import { McpPromptLoader } from '../../services/McpPromptLoader.js'; | ||
| import { SkillCommandLoader } from '../../services/SkillCommandLoader.js'; | ||
| import { parseSlashCommand } from '../../utils/commands.js'; | ||
| import { isBtwCommand } from '../utils/commandUtils.js'; | ||
| import { | ||
| hasSlashCommandPathSeparator, | ||
| isBtwCommand, | ||
| } from '../utils/commandUtils.js'; | ||
| import { clearScreen } from '../../utils/stdioHelpers.js'; | ||
| import { useKeypress } from './useKeypress.js'; | ||
| import { | ||
|
|
@@ -78,7 +81,7 @@ const SLASH_COMMANDS_SKIP_RECORDING = new Set([ | |
| 'btw', | ||
| ]); | ||
|
|
||
| interface SlashCommandProcessorActions { | ||
| export interface SlashCommandProcessorActions { | ||
| openAuthDialog: () => void; | ||
| openArenaDialog?: (type: Exclude<ArenaDialogType, null>) => void; | ||
| openThemeDialog: () => void; | ||
|
|
@@ -448,6 +451,9 @@ export const useSlashCommandProcessor = ( | |
| if (!trimmed.startsWith('/') && !trimmed.startsWith('?')) { | ||
| return false; | ||
| } | ||
| if (trimmed.startsWith('/') && hasSlashCommandPathSeparator(trimmed)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch is unreachable in practice. Every caller of Two options:
Without a comment, a future maintainer will likely "DRY it up" and remove it, possibly along with the safety net it was meant to provide. |
||
| return false; | ||
| } | ||
|
|
||
| const recordedItems: Array<Omit<HistoryItem, 'id'>> = []; | ||
| const recordItem = (item: Omit<HistoryItem, 'id'>) => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,9 +38,18 @@ export const isAtCommand = (query: string): boolean => | |
| // Check if starts with @ OR has a space, then @ | ||
| query.startsWith('@') || /\s@/.test(query); | ||
|
|
||
| const SLASH_PATH_SEPARATOR_RE = /[/\\]/; | ||
|
|
||
| const getSlashCommandFirstToken = (query: string): string => | ||
| query.slice(1).trimStart().split(/\s+/)[0] ?? ''; | ||
|
yiliang114 marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| export const hasSlashCommandPathSeparator = (query: string): boolean => | ||
| SLASH_PATH_SEPARATOR_RE.test(getSlashCommandFirstToken(query)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unstated precondition: Two cheap mitigations:
|
||
|
|
||
| /** | ||
| * Checks if a query string potentially represents an '/' command. | ||
| * It triggers if the query starts with '/' but excludes code comments like '//' and '/*'. | ||
| * It triggers if the query starts with '/' but excludes code comments like '//' | ||
| * and '/*', and file paths where the first token contains a path separator. | ||
| * | ||
| * @param query The input query string. | ||
| * @returns True if the query looks like an '/' command, false otherwise. | ||
|
|
@@ -60,6 +69,10 @@ export const isSlashCommand = (query: string): boolean => { | |
| return false; | ||
| } | ||
|
|
||
| if (hasSlashCommandPathSeparator(query)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.