Skip to content

Commit bdb4b54

Browse files
committed
add dedicated step
1 parent 2ff6879 commit bdb4b54

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

patchwork/step.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ def __init__(self, inputs: DataPoint):
4545
"""
4646

4747
# check if the inputs have the required keys
48-
if self.__input_class is not None:
49-
missing_keys = self.__input_class.__required_keys__.difference(inputs.keys())
50-
if len(missing_keys) > 0:
51-
raise ValueError(f"Missing required data: {list(missing_keys)}")
48+
self.test_inputs(inputs)
5249

5350
# store the inputs
5451
self.inputs = inputs
@@ -77,6 +74,12 @@ def __init_subclass__(cls, **kwargs):
7774
cls.__output_class = output_class
7875
else:
7976
cls.__output_class = None
77+
@classmethod
78+
def test_inputs(cls, inputs: DataPoint):
79+
if cls.__input_class is not None:
80+
missing_keys = cls.__input_class.__required_keys__.difference(inputs.keys())
81+
if len(missing_keys) > 0:
82+
raise ValueError(f"Missing required data: {list(missing_keys)}")
8083

8184
def __managed_run(self, *args, **kwargs) -> Any:
8285
self.debug(self.inputs)

patchwork/steps/CallCommand/CallCommand.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import json
1+
from __future__ import annotations
2+
23
import shlex
34
import subprocess
45
from pathlib import Path
@@ -17,12 +18,12 @@ def __init__(self, inputs: dict):
1718
self.env = self.__parse_env_text(inputs.get("env", ""))
1819

1920
@staticmethod
20-
def __parse_env_text(env_text):
21+
def __parse_env_text(env_text: str) -> dict[str, str]:
2122
env_spliter = shlex.shlex(env_text, posix=True)
2223
env_spliter.whitespace_split = True
2324
env_spliter.whitespace += ";"
2425

25-
env = dict()
26+
env: dict[str, str] = dict()
2627
for env_assign in env_spliter:
2728
env_assign_spliter = shlex.shlex(env_assign, posix=True)
2829
env_assign_spliter.whitespace_split = True
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from __future__ import annotations
2+
3+
import subprocess
4+
5+
from patchwork.logger import logger
6+
from patchwork.steps import CallCommand
7+
from patchwork.steps.ScanPSFuzz.typed import ScanPSFuzzInputs, ScanPSFuzzOutputs
8+
9+
10+
class ScanPSFuzz(CallCommand, input_class=ScanPSFuzzInputs, output_class=ScanPSFuzzOutputs):
11+
def __init__(self, inputs: dict):
12+
if not self.__is_ps_fuzz_installed():
13+
raise ValueError("""\
14+
`prompt-security-fuzzer` is not installed. Please install with the following instructions:
15+
1. Install pipx: https://github.com/pypa/pipx
16+
2. pipx install prompt-security-fuzzer
17+
3. pipx inject prompt-security-fuzzer setuptools
18+
""")
19+
self.test_inputs(inputs)
20+
wrapped_input = dict(
21+
command="prompt-security-fuzzer",
22+
command_args=f'-b {inputs["prompt_file_path"]}',
23+
env=f'OPENAI_API_KEY={inputs["openai_api_key"]}'
24+
)
25+
26+
working_dir = inputs.get("working_dir"),
27+
if working_dir is not None:
28+
wrapped_input["working_dir"] = working_dir
29+
30+
super().__init__(wrapped_input)
31+
32+
def __is_ps_fuzz_installed(self):
33+
try:
34+
subprocess.run(["prompt-security-fuzzer", "-h"], check=True)
35+
return True
36+
except subprocess.CalledProcessError as e:
37+
err = e
38+
except FileNotFoundError as e:
39+
err = e
40+
# If the command fails, prompt-security-fuzzer is not installed
41+
logger.info(f"prompt-security-fuzzer is not installed: {err}")
42+
return False
43+

patchwork/steps/ScanPSFuzz/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing_extensions import Annotated, TypedDict
2+
3+
from patchwork.common.utils.step_typing import StepTypeConfig
4+
5+
6+
class __RequiredScanPSFuzzInputs(TypedDict):
7+
prompt_file_path: Annotated[str, StepTypeConfig(is_path=True)]
8+
openai_api_key: Annotated[str, StepTypeConfig(is_config=True)]
9+
10+
class ScanPSFuzzInputs(__RequiredScanPSFuzzInputs, total=False):
11+
working_dir: Annotated[str, StepTypeConfig(is_path=True)]
12+
13+
14+
class ScanPSFuzzOutputs(TypedDict):
15+
stdout_output: str

patchwork/steps/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from patchwork.steps.ReadPRDiffs.ReadPRDiffs import ReadPRDiffs
4343
from patchwork.steps.ReadPRs.ReadPRs import ReadPRs
4444
from patchwork.steps.ScanDepscan.ScanDepscan import ScanDepscan
45+
from patchwork.steps.ScanPSFuzz.ScanPSFuzz import ScanPSFuzz
4546
from patchwork.steps.ScanSemgrep.ScanSemgrep import ScanSemgrep
4647
from patchwork.steps.SimplifiedLLM.SimplifiedLLM import SimplifiedLLM
4748
from patchwork.steps.SimplifiedLLMOnce.SimplifiedLLMOnce import SimplifiedLLMOnce
@@ -90,6 +91,7 @@
9091
"ReadPRDiffsPB",
9192
"ReadPRs",
9293
"ScanDepscan",
94+
"ScanPSFuzz"
9395
"ScanSemgrep",
9496
"SimplifiedLLM",
9597
"SimplifiedLLMOnce",

0 commit comments

Comments
 (0)