Elffuss BraidHub

kikocis/llama-stack public

cd38843c2a4c · 1.2 KiBRawPermalink
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the terms described in the LICENSE file in
5# the root directory of this source tree.
6
7from termcolor import cprint
8
9from .base import ShieldResponse, TextShield
10from llama_stack.apis.safety import * # noqa: F403
11
12
13class CodeScannerShield(TextShield):
14 def get_shield_type(self) -> ShieldType:
15 return BuiltinShield.code_scanner_guard
16
17 async def run_impl(self, text: str) -> ShieldResponse:
18 from codeshield.cs import CodeShield
19
20 cprint(f"Running CodeScannerShield on {text[50:]}", color="magenta")
21 result = await CodeShield.scan_code(text)
22 if result.is_insecure:
23 return ShieldResponse(
24 shield_type=BuiltinShield.code_scanner_guard,
25 is_violation=True,
26 violation_type=",".join(
27 [issue.pattern_id for issue in result.issues_found]
28 ),
29 violation_return_message="Sorry, I found security concerns in the code.",
30 )
31 else:
32 return ShieldResponse(
33 shield_type=BuiltinShield.code_scanner_guard, is_violation=False
34 )