Skip to content

Commit

Permalink
feat: sanitize alt text to prevent invalid html code (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sm1lEE authored Aug 13, 2024
1 parent 552e563 commit a236d0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ After updating the addon, make sure to inspect the `config/aida.php` file to lea
*/
$altText = SomeApi::get($asset, $locale);

/**
* You might want to sanitize the altText with `htmlspecialchars($altText, ENT_QUOTES, 'UTF-8')` to prevent invalid HTML code.
* Alternatively you can use the `sanitize` modifier in your view files.
*/

return $altText;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Generator/OpenAIGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function generate($asset, $locale = 'en')
// TODO: The response could be incomplete, e. g. when the 'finishReason' is not 'stop', probably due to a token limitation or usage limit. These cases need to be handled.
$result = trim($response->choices[0]->message->content);

return $result;
// Sanitize the result to convert special characters into HTML entities, preventing invalid HTML code.
$sanitizedResult = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');

return $sanitizedResult;
}
}

0 comments on commit a236d0b

Please sign in to comment.