Skip to content

Commit 014d19e

Browse files
committed
fix: address security warning
1 parent 01397e1 commit 014d19e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

backend/companion/companionRouter.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,20 @@ async function handleChatMessage(req, res) {
8888
const conversationId = sessionId;
8989

9090
try {
91-
const url = `https://companion.cp.dev.kyma.cloud.sap/api/conversations/${conversationId}/messages`;
91+
if (!conversationId || typeof conversationId !== 'string') {
92+
return res.status(400).json({ error: 'Invalid conversation ID' });
93+
}
94+
95+
const baseUrl =
96+
'https://companion.cp.dev.kyma.cloud.sap/api/conversations/';
97+
let targetUrl;
98+
try {
99+
targetUrl = new URL(`${conversationId}/messages`, baseUrl);
100+
} catch (urlError) {
101+
console.error('Invalid URL construction:', urlError);
102+
return res.status(400).json({ error: 'Invalid conversation ID' });
103+
}
104+
92105
const payload = {
93106
query,
94107
resource_kind: resourceType,
@@ -123,14 +136,12 @@ async function handleChatMessage(req, res) {
123136
throw new Error('Missing authentication credentials');
124137
}
125138

126-
const response = await fetch(url, {
139+
const response = await fetch(targetUrl, {
127140
method: 'POST',
128141
headers,
129142
body: JSON.stringify(payload),
130143
});
131144

132-
console.log(response);
133-
134145
if (!response.ok) {
135146
throw new Error(`HTTP error! status: ${response.status}`);
136147
}

0 commit comments

Comments
 (0)