Skip to content

Commit a68377d

Browse files
Merge pull request #24 from acezxn/js-audit-1
Multiple bug fixes
2 parents f9c7bf4 + fb52296 commit a68377d

5 files changed

Lines changed: 62 additions & 38 deletions

File tree

requirements.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
black
2+
tree-sitter>=0.20.0,<0.22.0
3+
transformers
4+
torch
5+
tiktoken
6+
replicate
7+
openai
8+
google-generativeai
9+
tqdm
10+
networkx
11+
streamlit
12+
botocore
13+
boto3
14+
black
15+
anthropic
16+
mypy
17+
types-networkx
18+
types-tqdm
19+
boto3-stubs[essential]

src/llmtool/dfbscan/intra_dataflow_analyzer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from os import path
22
import json
33
import time
4-
from typing import List, Set, Optional, Dict
4+
from typing import List, Set, Optional, Dict, Union
55
from llmtool.LLM_utils import *
66
from llmtool.LLM_tool import *
77
from memory.syntactic.function import *
@@ -149,7 +149,7 @@ def _parse_response(
149149
r"Line:\s*([^;]+);"
150150
)
151151

152-
current_path = None
152+
current_path: Optional[Dict[str, Union[str, list]]] = None
153153
for line in response.splitlines():
154154
line = line.strip().lstrip("-").strip()
155155
if not line:
@@ -176,7 +176,8 @@ def _parse_response(
176176
"index": detail_match.group(4).strip(),
177177
"line": detail_match.group(5).strip(),
178178
}
179-
current_path["propagation_details"].append(detail)
179+
if isinstance(current_path["propagation_details"], list):
180+
current_path["propagation_details"].append(detail)
180181

181182
elif current_path is not None:
182183
paths.append(current_path)

src/prompt/Javascript/dfbscan/intra_dataflow_analyzer.json

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
" 2. Function Invocations: Call sites where SRC is passed as an argument.",
1919
" 3. Return Statements: Points where the function returns, possibly propagating SRC.",
2020
" 4. Parameter Assignments: Assignments where SRC is assigned to a parameter or an object field that is accessible outside the function.",
21-
" 5. Non local variable assignment: Assignments where SRC is assigned to a predefined non local variable.",
21+
" 5. Non local variable assignment: Assignments where SRC is assigned to a predefined non local variable that is accessible outside the function.",
2222
"- Step 2: Identify all execution paths relevant to the key points found in Step 1. For each path:",
2323
" - Identify every potential execution path;",
2424
" - Verify whether the key points are executed along each path;",
@@ -69,39 +69,39 @@
6969
"- Type: Argument; Name: a; Function: updateValue; Index: 0; Line: 8; Dependency: SRC (p) is assigned to 'a', then passed as an argument to updateValue;",
7070
"- Type: Return; Name: a; Function: None; Index: 0; Line: 10; Dependency: SRC (p) is assigned to 'a', then returned.",
7171
"",
72-
"Example 2: Propagation via Parameter Passing and Return",
72+
"Example 2: Propagation via Nonlocal Variable assignment",
7373
"User:",
7474
"Now I will give you a target function with the source point 'src' at line 1:",
7575
"```javascript",
76-
"function modifyValue(src, flag) {",
77-
" if (flag > 0) {",
78-
" flag = src; // source value: src",
79-
" } else if (flag == 0) {",
80-
" return src; // return statement",
76+
"function update(src, count) {",
77+
" if (count > 0) {",
78+
" item = src;",
8179
" }",
82-
" return -1; // Default return value ",
80+
" return item;",
8381
"}",
8482
"```",
83+
"",
84+
"Consider the following as non local variables:",
85+
"- item at line 3",
86+
"",
8587
"Where does the source variable 'src' at line 1 propagate within this function?",
8688
"System:",
8789
"Explanation:",
8890
"Step 1: Identify SRC and its alias;",
8991
"SRC: The variable src is defined at line 1;",
9092
"Step 2: Identify key points and execution paths:",
91-
"Path 1 (flag > 0): src is assigned to flag at line 3, making it accessible outside the function if flag is referenced after the call;",
92-
"Path 2 (flag == 0): src is returned at line 5, propagating to the caller;",
93-
"Path 3 (flag < 0): Function returns -1, so SRC does not propagate in this path;",
93+
"Path 1 (count > 0): src is used to modify the value of item at line 3, which is later returned at line 5;",
94+
"Path 2 (count <= 0): the value of item is directly returned at line 5 without using src;",
9495
"Step 3: Simulate the execution paths:",
95-
"Path 1: When flag > 0, src is assigned to flag, allowing potential propagation outside the function through the parameter reference;",
96-
"Path 2: When flag == 0, src is returned to the caller;",
97-
"Path 3: When flag < 0, src does not propagate, as the function returns -1;",
96+
"In Path 1, propagation is from src -> Nonlocal variable assignment at item = src -> return item;",
97+
"In Path 2, propagation is from src -> return item;",
9898
"Answer:",
99-
"Path 1: Lines 1 -> 3;",
100-
"- Type: Parameter; Name: flag; Function: None; Index: 1; Line: 3; Dependency: SRC (src) is assigned to parameter 'flag', which may be referenced by the caller;",
99+
"Path 1: Lines 1 -> 3 -> 5;",
100+
"- Type: Nonlocal; Name: item; Function: None; Index: None; Line: 3; Dependency: SRC (src) is used to update the value of item, allowing potential propagation outside the function;",
101+
"- Type: Return; Name: item; Function: None; Index: 0; Line: 5; Dependency: the item nonlocal variable, which is updated to the value of src, is returned to the caller;",
101102
"Path 2: Lines 1 -> 5;",
102-
"- Type: Return; Name: src; Function: None; Index: 0; Line: 5; Dependency: SRC (src) is returned to the caller;",
103-
"Path 3: Lines 1 -> 6;",
104-
"- No propagation; Dependency: Default return value -1 is unrelated to SRC."
103+
"- No propagation; Dependency: The value of the item nonlocal variable is directly returned to the caller;",
104+
""
105105
],
106106
"question_template": "- Where does the source <SRC_NAME> at line <SRC_LINE> in this function propagate?",
107107
"answer_format_cot": [
@@ -114,6 +114,7 @@
114114
" - For parameter propagation: 'Type: Parameter; Name: {parameter name}; Function: None; Index: {parameter index}; Line: {assignment line number}; Dependency: {summary of dependency from SRC to parameter}';",
115115
" - For sink propagation: 'Type: Sink; Name: {sink name}; Function: None; Index: None; Line: {sink statement line number}; Dependency: {summary of dependency from SRC to sink}';",
116116
" - For non local variable assignment: 'Type: Nonlocal; Name: {non local name}; Function: None; Index: None; Line: {assignment statement line number}; Dependency: {summary of dependency from SRC to assignment}';",
117+
" Note: Each bulletpoint under the path bulletpoint represent an identified key point along the execution path. Each key point information must be in a single line, and do not provide any key point information that does not adhere to one of the five types listed above.",
117118
"(4) If there is no propagation along a path, provide a brief explanation of why SRC does not propagate in that path as follows:",
118119
"- Path <Path Number>: <Execution Path>;",
119120
" - No propagation; Dependency: {reason for no propagation};",
@@ -128,8 +129,10 @@
128129
"Here are the Function call sites and return statements within the function, which can be used in Step 1;\n",
129130
"<CALL_STATEMENTS>\n",
130131
"<RETURN_VALUES>\n",
131-
"<NONLOCAL_VALUES>",
132+
"<NONLOCAL_VALUES>\n",
132133
"Now, please answer the following question:\n<QUESTION>\n",
133134
"Your response should strictly follow the format:\n<ANSWER>\n"
134135
]
135136
}
137+
138+

src/tstool/analyzer/Javascript_TS_analyzer.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,43 @@ def extract_scope_info(self, tree: tree_sitter.Tree) -> None:
2222
:param tree: Parsed syntax tree
2323
"""
2424
scope_stack: List[int] = []
25-
scope_id: int = 0
2625

2726
def search(root: Node) -> None:
28-
nonlocal scope_id
29-
3027
for child in root.children:
3128
if child.type == "statement_block":
3229
if len(scope_stack) > 0:
33-
self.scope_env[scope_stack[-1]][1].add(scope_id)
30+
self.scope_env[scope_stack[-1]][1].add(self.current_scope_id)
3431

35-
self.scope_env[scope_id] = (child, set())
36-
self.scope_root_to_scope_id[child] = scope_id
37-
scope_stack.append(scope_id)
32+
self.scope_env[self.current_scope_id] = (child, set())
33+
self.scope_root_to_scope_id[child] = self.current_scope_id
34+
scope_stack.append(self.current_scope_id)
3835

3936
if child.parent:
4037
if child.parent.type == "function_declaration":
41-
self.function_root_to_scope_id[child.parent] = scope_id
38+
self.function_root_to_scope_id[child.parent] = (
39+
self.current_scope_id
40+
)
4241
elif (
4342
child.parent.type == "arrow_function"
4443
or child.parent.type == "function_expression"
4544
):
4645
if child.parent.parent:
4746
self.function_root_to_scope_id[child.parent.parent] = (
48-
scope_id
47+
self.current_scope_id
4948
)
5049

51-
scope_id += 1
50+
self.current_scope_id += 1
5251
search(child)
5352
scope_stack.pop()
5453
else:
5554
search(child)
5655

5756
return
5857

59-
self.scope_env[scope_id] = (tree.root_node, set())
60-
self.scope_root_to_scope_id[tree.root_node] = scope_id
61-
scope_stack.append(scope_id)
62-
scope_id += 1
58+
self.scope_env[self.current_scope_id] = (tree.root_node, set())
59+
self.scope_root_to_scope_id[tree.root_node] = self.current_scope_id
60+
scope_stack.append(self.current_scope_id)
61+
self.current_scope_id += 1
6362
search(tree.root_node)
6463
return
6564

@@ -147,7 +146,7 @@ def extract_nonlocal_info(self) -> None:
147146
)
148147

149148
for candidate_node in identifiers_per_scope[child_scope_id]:
150-
if candidate_node:
149+
if not candidate_node:
151150
continue
152151

153152
# Name mismatch

src/tstool/analyzer/TS_analyzer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def __init__(
178178
self.scope_env: Dict[int, Tuple[Node, Set[int]]] = {}
179179
self.api_env: Dict[int, API] = {}
180180

181+
self.current_scope_id: int = 0
182+
181183
# Dictionary storing mapping from the root node of the scope to its scope id
182184
self.scope_root_to_scope_id: Dict[Node, int] = {}
183185

0 commit comments

Comments
 (0)