Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

BUG: KeywordSelectionStrategy accepts a negative context, so SelectiveTextConverter duplicates or truncates the prompt

Đã đóng
#3,444 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@ShivamSharma43 đang làm issue này rồi.

Từ ngày 21/10/2026.

  • #3446 của @ShivamSharma43 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Start in pyrit/converter/text_selection_strategy.py and compare KeywordSelectionStrategy with the other selection strategies that validate their bounds. Check the negative context and empty keyword cases from the issue’s reproducer, then verify they raise ValueError without changing valid selections.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug

KeywordSelectionStrategy in pyrit/converter/text_selection_strategy.py stores keyword, context_before and context_after without checking them, while the other character-level strategies (PositionSelectionStrategy, ProportionSelectionStrategy, RangeSelectionStrategy) and the word-level ones validate their bounds.

select_range then computes start = max(0, index - context_before) and end = min(len(text), index + len(keyword) + context_after), so a negative context produces a range that does not cover the keyword, or one whose end is before its start. SelectiveTextConverter uses that range as prompt[:start] + convert(prompt[start:end]) + prompt[end:], which:

  • with context_after=-10 on "the secret plan" returns the range (4, 0), and the converter sends "the the secret plan": the text between end and start is duplicated and nothing is converted.
  • with context_before=-3 returns (7, 10), so only "ret" is converted and the rest of the keyword is sent in plaintext ("the seccmV0 plan" with Base64Converter).

An empty keyword is also accepted; it matches at index 0, so the strategy selects the first context_after characters of every prompt instead of a keyword.

Steps/Code to Reproduce
import asyncio
from pyrit.converter import Base64Converter, SelectiveTextConverter
from pyrit.converter.text_selection_strategy import KeywordSelectionStrategy

prompt = "the secret plan"
for strategy in (
    KeywordSelectionStrategy(keyword="secret", context_after=-10),
    KeywordSelectionStrategy(keyword="secret", context_before=-3),
):
    converter = SelectiveTextConverter(sub_converter=Base64Converter(), selection_strategy=strategy)
    print(strategy.select_range(text=prompt), repr(asyncio.run(converter.convert_async(prompt=prompt)).output_text))
Expected Results

KeywordSelectionStrategy raises a ValueError for a negative or non-integer context_before/context_after and for an empty keyword, like the other selection strategies do for out-of-range values.

Actual Results
(4, 0) 'the the secret plan'
(7, 10) 'the seccmV0 plan'
Screenshots

N/A

Versions
  • OS: Windows 11
  • Python version: 3.11.9
  • PyRIT version: installed from main (898b3bdc) in editable mode
Ngôn ngữ chính
Python
Star
4.6k
Fork
962
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
417

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/PyRIT

Tất cả issue của microsoft/PyRIT

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.