-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
151 lines (130 loc) · 4.74 KB
/
Copy pathutils.py
File metadata and controls
151 lines (130 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import tiktoken
from typing import Union, List, Dict
def sanitize_command(command: str) -> str:
"""
Sanitize a shell command by checking against a blacklist of dangerous patterns.
Args:
command: The shell command to sanitize
Returns:
str: The original command if it passes validation
Raises:
ValueError: If the command contains any dangerous patterns
"""
# Comprehensive list of dangerous shell commands and patterns that could harm the system
DANGEROUS_PATTERNS = [
'rm -rf /', # Delete root directory
'rm -rf *', # Delete all in current dir
'rm -rf ~', # Delete home directory
'mkfs', # Format filesystem
'dd if=/dev/zero',
'> /dev/sda', # Overwrite disk
':(){:|:&};:', # Fork bomb
'chmod -R 777 /', # Recursive permission change on root
'chmod -R 000 /',
'> /etc/passwd', # Overwrite critical system files
'> /etc/shadow',
'shutdown', # System control commands
'reboot',
'halt',
'poweroff',
'init 0',
'init 6',
'format',
'fdisk',
'> /etc/hosts',
'> /etc/resolv.conf',
'mv /* /dev/null',
'dd if=/dev/random',
'dd if=/dev/urandom',
':(){ :|:& };:', # Alternative fork bomb
'> /boot', # Delete critical directories
'rm -rf /boot',
'rm -rf /etc',
'rm -rf /usr',
'rm -rf /var',
'rm -rf /lib',
'rm -rf /bin',
'rm -rf /sbin',
'chown -R', # Recursive ownership change
'chmod -R'
]
# Preserve original command but check lowercase version
original_command = command
command_lower = command.lower().strip()
# Check command against blacklist
for pattern in DANGEROUS_PATTERNS:
if pattern in command_lower:
raise ValueError(
f"Command '{command}' contains dangerous pattern '{pattern}'"
)
return original_command
def detect_language(file_path: str, file_contents: str = None) -> str:
"""
Detect the programming language from file extension and/or content.
Args:
file_path: Path to the source file
file_contents: Optional file contents to analyze
Returns:
String identifier for the language (e.g., 'c', 'cpp', 'python', 'rust', 'go', 'java')
"""
import os
# Language mapping based on file extensions
extension_map = {
'.c': 'c',
'.cpp': 'cpp',
'.cxx': 'cpp',
'.cc': 'cpp',
'.c++': 'cpp',
'.h': 'c', # Default header files to C
'.hpp': 'cpp',
'.hxx': 'cpp',
'.py': 'python',
'.rs': 'rust',
'.go': 'go',
'.java': 'java',
'.js': 'javascript',
'.ts': 'typescript',
}
# Get file extension
_, ext = os.path.splitext(file_path.lower())
if ext in extension_map:
return extension_map[ext]
# If no extension match, try to detect from content
if file_contents:
# Check for common language signatures
content_lower = file_contents.lower()[:500] # Check first 500 chars
if 'package main' in content_lower or 'import (' in content_lower:
return 'go'
elif 'fn main' in content_lower or '#![allow' in content_lower:
return 'rust'
elif 'def ' in content_lower or 'import ' in content_lower:
return 'python'
elif 'public class' in content_lower or 'public static void main' in content_lower:
return 'java'
elif '#include' in content_lower and 'namespace' in content_lower:
return 'cpp'
elif '#include' in content_lower:
return 'c'
# Default to C/C++ for binary analysis context
return 'c'
def count_tokens(text: Union[str, List[Dict[str, str]]], model: str = "gpt-4o") -> int:
"""
Count the number of tokens in a text string using OpenAI's tokenizer.
Args:
text: Either a string or a list of message dictionaries containing 'content' keys
model: The model name to use for tokenization (default: gpt-4o)
Returns:
The total number of tokens in the text
Example:
>>> count_tokens("Hello world!")
2
>>> count_tokens([{"content": "Hello"}, {"content": "world!"}])
2
"""
# Convert list of messages to single string if needed
if isinstance(text, list):
text = " ".join(str(item.get("content", "")) for item in text)
# Get tokenizer for specified model
encoder = tiktoken.encoding_for_model("gpt-4o")
tokens = encoder.encode(text)
return len(tokens)