Skip to content

Commit 2b3d3f1

Browse files
committed
Misc small security fixes, mostly using more secure RNGs and adding timeouts to requests in scripts
1 parent d8cf263 commit 2b3d3f1

7 files changed

Lines changed: 37 additions & 11 deletions

File tree

frontend/src/components/ui/sidebar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ function SidebarMenuSkeleton({
608608
}) {
609609
// Random width between 50 to 90%.
610610
const width = React.useMemo(() => {
611-
return `${Math.floor(Math.random() * 40) + 50}%`
611+
const array = new Uint32Array(1)
612+
crypto.getRandomValues(array)
613+
return `${(array[0] % 40) + 50}%`
612614
}, [])
613615

614616
return (

frontend/src/services/agentCoreService.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ const parseStreamingChunk = (line, currentCompletion, updateCallback) => {
7373
// Generate a UUID-like string that meets AgentCore requirements (min 33 chars)
7474
const generateId = () => {
7575
const timestamp = Date.now().toString(36)
76-
const random1 = Math.random().toString(36).substring(2)
77-
const random2 = Math.random().toString(36).substring(2)
78-
const random3 = Math.random().toString(36).substring(2)
76+
77+
// Use cryptographically secure random number generation
78+
const getSecureRandom = () => {
79+
const array = new Uint32Array(1)
80+
crypto.getRandomValues(array)
81+
return array[0].toString(36)
82+
}
83+
84+
const random1 = getSecureRandom()
85+
const random2 = getSecureRandom()
86+
const random3 = getSecureRandom()
7987
return `${timestamp}-${random1}-${random2}-${random3}`
8088
}
8189

gateway/utils/gateway_access_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get_gateway_access_token() -> str:
7373
print(f"[AUTH] Scopes: {data['scope']}")
7474

7575
# Request access token
76-
response = requests.post(token_url, headers=headers, data=data)
76+
response = requests.post(token_url, headers=headers, data=data, timeout=30)
7777

7878
if response.status_code != 200:
7979
print(f"[AUTH ERROR] Token request failed: {response.status_code}")

scripts/post-deploy.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ def generate_aws_exports(stack_name):
3535
"--output", "json"
3636
]
3737

38-
result = subprocess.run(command, capture_output=True, text=True, check=True)
38+
result = subprocess.run(
39+
command,
40+
capture_output=True,
41+
text=True,
42+
check=True,
43+
shell=False, # Explicitly disable shell
44+
timeout=60 # Add timeout for security
45+
)
3946
stack_data = json.loads(result.stdout)
4047

4148
# Extract stack info

scripts/test-agent-invocation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ def start_local_agent(memory_id: str, region: str) -> subprocess.Popen:
8484
print_msg(f"Agent file not found: {agent_path}", "error")
8585
sys.exit(1)
8686

87+
# Security validation: ensure agent_path is within the patterns directory
88+
patterns_dir = Path(__file__).parent.parent / "patterns"
89+
try:
90+
agent_path.resolve().relative_to(patterns_dir.resolve())
91+
except ValueError:
92+
print_msg(f"Security error: Agent path outside patterns directory: {agent_path}", "error")
93+
sys.exit(1)
94+
8795
print(f"Starting local agent at {agent_path}...")
8896
print(f" Memory ID: {memory_id}")
8997
print(f" Region: {region}\n")

scripts/test-feedback-api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def make_api_request(
3737

3838
try:
3939
if method == "POST":
40-
response = requests.post(url, headers=headers, json=data)
40+
response = requests.post(url, headers=headers, json=data, timeout=30)
4141
elif method == "GET":
42-
response = requests.get(url, headers=headers)
42+
response = requests.get(url, headers=headers, timeout=30)
4343
else:
4444
raise ValueError(f"Unsupported method: {method}")
4545

scripts/test-gateway.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def fetch_access_token(client_id: str, client_secret: str, token_url: str) -> st
2929
response = requests.post(
3030
token_url,
3131
data=f"grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}",
32-
headers={'Content-Type': 'application/x-www-form-urlencoded'}
32+
headers={'Content-Type': 'application/x-www-form-urlencoded'},
33+
timeout=30
3334
)
3435

3536
if response.status_code != 200:
@@ -52,7 +53,7 @@ def list_tools(gateway_url: str, access_token: str) -> dict:
5253
"method": "tools/list"
5354
}
5455

55-
response = requests.post(gateway_url, headers=headers, json=payload)
56+
response = requests.post(gateway_url, headers=headers, json=payload, timeout=30)
5657

5758
if response.status_code != 200:
5859
print_msg(f"Gateway request failed: {response.status_code} - {response.text}", "error")
@@ -78,7 +79,7 @@ def call_tool(gateway_url: str, access_token: str, tool_name: str, arguments: di
7879
}
7980
}
8081

81-
response = requests.post(gateway_url, headers=headers, json=payload)
82+
response = requests.post(gateway_url, headers=headers, json=payload, timeout=30)
8283

8384
if response.status_code != 200:
8485
print_msg(f"Gateway request failed: {response.status_code} - {response.text}", "error")

0 commit comments

Comments
 (0)