Skip to content

Commit 381a5ee

Browse files
authored
Merge pull request #683 from 10up/enhacement/prompt-injection
Fixed the potential prompt injection in content resizing.
2 parents 6f3ca32 + 6c3a37c commit 381a5ee

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

includes/Classifai/Providers/OpenAI/ChatGPT.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) {
231231
'messages' => [
232232
[
233233
'role' => 'system',
234-
'content' => $prompt,
234+
'content' => 'You will be provided with content delimited by triple quotes. ' . $prompt,
235235
],
236236
[
237237
'role' => 'user',
238-
'content' => $this->get_content( $post_id, $excerpt_length, false, $args['content'] ) . '',
238+
'content' => '"""' . $this->get_content( $post_id, $excerpt_length, false, $args['content'] ) . '"""',
239239
],
240240
],
241241
'temperature' => 0.9,
@@ -330,11 +330,11 @@ public function generate_titles( int $post_id = 0, array $args = [] ) {
330330
'messages' => [
331331
[
332332
'role' => 'system',
333-
'content' => $prompt,
333+
'content' => 'You will be provided with content delimited by triple quotes. ' . $prompt,
334334
],
335335
[
336336
'role' => 'user',
337-
'content' => $this->get_content( $post_id, absint( $args['num'] ) * 15, false, $args['content'] ) . '',
337+
'content' => '"""' . $this->get_content( $post_id, absint( $args['num'] ) * 15, false, $args['content'] ) . '"""',
338338
],
339339
],
340340
'temperature' => 0.9,
@@ -435,11 +435,11 @@ public function resize_content( int $post_id, array $args = array() ) {
435435
'messages' => [
436436
[
437437
'role' => 'system',
438-
'content' => $prompt,
438+
'content' => 'You will be provided with content delimited by triple quotes. ' . $prompt,
439439
],
440440
[
441441
'role' => 'user',
442-
'content' => esc_html( $args['content'] ),
442+
'content' => '"""' . esc_html( $args['content'] ) . '"""',
443443
],
444444
],
445445
'temperature' => 0.9,

tests/test-plugin/e2e-test-plugin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) {
2525
$response = file_get_contents( __DIR__ . '/chatgpt.json' );
2626
} elseif ( strpos( $url, 'https://api.openai.com/v1/chat/completions' ) !== false ) {
2727
$response = file_get_contents( __DIR__ . '/chatgpt.json' );
28-
$body_json = isset( $parsed_args['body'] ) ? wp_unslash( $parsed_args['body'] ) : false;
28+
$body_json = $parsed_args['body'] ?? false;
2929

3030
if ( $body_json ) {
3131
$body = json_decode( $body_json, JSON_OBJECT_AS_ARRAY );
@@ -34,11 +34,11 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) {
3434

3535
if ( str_contains( $prompt, 'Increase the content' ) || str_contains( $prompt, 'Decrease the content' ) ) {
3636
$response = file_get_contents( __DIR__ . '/resize-content.json' );
37-
} else if ( 'This is a custom excerpt prompt' === $prompt ) {
37+
} else if ( str_contains( $prompt, 'This is a custom excerpt prompt' ) ) {
3838
$response = file_get_contents( __DIR__ . '/chatgpt-custom-excerpt-prompt.json' );
39-
} else if ( 'This is a custom title prompt' === $prompt ) {
39+
} else if ( str_contains( $prompt, 'This is a custom title prompt' ) ) {
4040
$response = file_get_contents( __DIR__ . '/chatgpt-custom-title-prompt.json' );
41-
} else if ( 'This is a custom shrink prompt' === $prompt || 'This is a custom grow prompt' === $prompt ) {
41+
} else if ( str_contains( $prompt, 'This is a custom shrink prompt' ) || str_contains( $prompt, 'This is a custom grow prompt' ) ) {
4242
$response = file_get_contents( __DIR__ . '/resize-content-custom-prompt.json' );
4343
}
4444
}

0 commit comments

Comments
 (0)