Skip to content

fix(amazonq): add validation for create a saved prompt UX #1116

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 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion chat-client/src/client/mynahUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,26 @@ export const createMynahUi = (
autoFocus: true,
title: 'Prompt name',
placeholder: 'Enter prompt name',
validationPatterns: {
patterns: [
{
pattern: /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,99}$/,
errorMessage:
'Use only letters, numbers, hyphens, and underscores, starting with a letter or number. Maximum 100 characters.',
},
],
},
description: "Use this prompt by typing '@' followed by the prompt name.",
},
],
[
{ id: ContextPrompt.CancelButtonId, text: 'Cancel', status: 'clear' },
{ id: ContextPrompt.SubmitButtonId, text: 'Create', status: 'main' },
{
id: ContextPrompt.SubmitButtonId,
text: 'Create',
status: 'main',
waitMandatoryFormItems: true,
},
],
`Create a saved prompt`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as path from 'path'
import * as sinon from 'sinon'
import * as assert from 'assert'
import { expect } from 'chai'
import { getUserPromptsDirectory, getNewPromptFilePath, promptFileExtension } from './contextUtils'
import * as pathUtils from '@aws/lsp-core/out/util/path'
import { sanitizeFilename } from '@aws/lsp-core/out/util/text'

describe('contextUtils', () => {
let getUserHomeDirStub: sinon.SinonStub

beforeEach(() => {
getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir')

// Default behavior
getUserHomeDirStub.returns('/home/user')
})

afterEach(() => {
sinon.restore()
})

describe('getUserPromptsDirectory', () => {
it('should return the correct prompts directory path', () => {
const result = getUserPromptsDirectory()
assert.strictEqual(result, path.join('/home/user', '.aws', 'amazonq', 'prompts'))
})
})

describe('getNewPromptFilePath', () => {
it('should use default name when promptName is empty', () => {
const result = getNewPromptFilePath('')
assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `default${promptFileExtension}`)
)
})

it('should use default name when promptName is undefined', () => {
const result = getNewPromptFilePath(undefined as unknown as string)
assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `default${promptFileExtension}`)
)
})

it('should trim whitespace from promptName', () => {
const result = getNewPromptFilePath(' test-prompt ')
const expectedSanitized = sanitizeFilename('test-prompt')
assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `${expectedSanitized}${promptFileExtension}`)
)
})

it('should truncate promptName if longer than 100 characters', () => {
const longName = 'a'.repeat(150)
const truncatedName = 'a'.repeat(100)

const result = getNewPromptFilePath(longName)
const expectedSanitized = sanitizeFilename(truncatedName)

assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `${expectedSanitized}${promptFileExtension}`)
)
})

it('should sanitize the filename using sanitizeFilename', () => {
const unsafeName = 'unsafe/name?with:invalid*chars'
const expectedSanitized = sanitizeFilename(path.basename(unsafeName))

const result = getNewPromptFilePath(unsafeName)

assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `${expectedSanitized}${promptFileExtension}`)
)
})

it('should handle path traversal attempts', () => {
const traversalPath = '../../../etc/passwd'
const expectedSanitized = sanitizeFilename(path.basename(traversalPath))

const result = getNewPromptFilePath(traversalPath)

assert.strictEqual(
result,
path.join('/home/user', '.aws', 'amazonq', 'prompts', `${expectedSanitized}${promptFileExtension}`)
)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getUserHomeDir } from '@aws/lsp-core/out/util/path'
import * as path from 'path'

Check warning on line 2 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/contextUtils.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"

Check warning on line 2 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/contextUtils.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"
import { sanitizeFilename } from '@aws/lsp-core/out/util/text'

export const promptFileExtension = '.md'
export const additionalContentInnerContextLimit = 8192
Expand All @@ -9,10 +10,22 @@
return path.join(getUserHomeDir(), '.aws', 'amazonq', 'prompts')
}

/**
* Creates a secure file path for a new prompt file.
*
* @param promptName - The user-provided name for the prompt
* @returns A sanitized file path within the user prompts directory
*/
export const getNewPromptFilePath = (promptName: string): string => {
const userPromptsDirectory = getUserPromptsDirectory()
return path.join(
userPromptsDirectory,
promptName ? `${promptName}${promptFileExtension}` : `default${promptFileExtension}`
)

const trimmedName = promptName?.trim() || ''

const truncatedName = trimmedName.slice(0, 100)

const safePromptName = truncatedName ? sanitizeFilename(path.basename(truncatedName)) : 'default'

const finalPath = path.join(userPromptsDirectory, `${safePromptName}${promptFileExtension}`)

return finalPath
}
Loading