Skip to content

Commit ad1566e

Browse files
committed
content fixed code/repo
1 parent 3889c83 commit ad1566e

File tree

4 files changed

+79
-70
lines changed

4 files changed

+79
-70
lines changed

frontend/src/components/VulnerabilityScanner/CodeVulnerabilityScanner.jsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import React, { useState } from "react";
1+
import React from "react";
22
import { toast } from "react-toastify";
33
import { ShieldCheckIcon, ArrowPathIcon, CodeBracketSquareIcon } from "@heroicons/react/24/outline";
44

5-
const CodeVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId }) => {
6-
const [code, setCode] = useState("");
7-
5+
const CodeVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId, code, setCode }) => {
86
const scanCode = async () => {
97
if (!code.trim()) {
108
toast.warn("Please enter code to scan for vulnerabilities!", {
@@ -17,14 +15,17 @@ const CodeVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId }
1715

1816
setIsLoading(true);
1917
try {
20-
const response = await fetch("http://127.0.0.1:5000/scan-code", {
18+
const formData = new FormData();
19+
formData.append('input_type', 'content');
20+
formData.append('content', code);
21+
formData.append('extension', 'py');
22+
23+
const response = await fetch("http://127.0.0.1:5000/semgrep", {
2124
method: "POST",
2225
headers: {
23-
"Content-Type": "application/json",
2426
Authorization: `Bearer ${localStorage.getItem("token")}`,
25-
"X-User-ID": userId,
2627
},
27-
body: JSON.stringify({ code: code }),
28+
body: formData,
2829
});
2930

3031
if (!response.ok) {
@@ -63,8 +64,8 @@ const CodeVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId }
6364
<textarea
6465
id="code-input"
6566
placeholder="Paste your code here..."
66-
value={code}
67-
onChange={(e) => setCode(e.target.value)}
67+
value={code} // use lifted state
68+
onChange={(e) => setCode(e.target.value)} // update lifted state
6869
rows={10}
6970
className="w-full p-4 bg-gray-700 text-gray-100 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-red-500 focus:border-transparent transition duration-200 placeholder-gray-400 font-mono text-sm"
7071
/>
@@ -92,4 +93,4 @@ const CodeVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId }
9293
);
9394
};
9495

95-
export default CodeVulnerabilityScanner;
96+
export default CodeVulnerabilityScanner;

frontend/src/components/VulnerabilityScanner/GitHubVulnerabilityScanner.jsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@ import React, { useState } from "react";
22
import { toast } from "react-toastify";
33
import { ShieldCheckIcon, ArrowPathIcon } from "@heroicons/react/24/outline";
44

5-
const GitHubVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId }) => {
6-
const [url, setUrl] = useState("");
5+
const GitHubVulnerabilityScanner = ({ setResult, isLoading, url,setIsLoading, setUrl,userId }) => {
6+
// const [url, setUrl] = useState("");
77

88
const scanVulnerabilities = async () => {
99
if (!url || !url.includes("github.com")) {
10-
toast.warn("Please enter a valid GitHub repository URL (e.g. https://github.com/user/repo)!", {
11-
position: "top-right",
12-
autoClose: 3000,
13-
theme: "light",
14-
});
10+
toast.warn(
11+
"Please enter a valid GitHub repository URL (e.g. https://github.com/user/repo)!",
12+
{ position: "top-right", autoClose: 3000, theme: "light" }
13+
);
1514
return;
1615
}
1716

1817
setIsLoading(true);
1918
try {
19+
const formData = new FormData();
20+
formData.append('input_type', 'repo');
21+
formData.append('repo_url', url);
22+
2023
const response = await fetch("http://127.0.0.1:5000/semgrep", {
2124
method: "POST",
2225
headers: {
23-
"Content-Type": "application/json",
2426
Authorization: `Bearer ${localStorage.getItem("token")}`,
25-
"X-User-ID": userId,
2627
},
27-
body: JSON.stringify({ repo_url: url, input_type: "repo" }),
28+
body: formData,
2829
});
2930

3031
if (!response.ok) {
@@ -64,15 +65,10 @@ const GitHubVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId
6465
id="github-url"
6566
type="text"
6667
placeholder="GitHub URL (e.g. https://github.com/user/repo)"
67-
value={url}
68-
onChange={(e) => setUrl(e.target.value)}
68+
value={url} // <-- controlled input
69+
onChange={(e) => setUrl(e.target.value)} // <-- update state
6970
className="w-full pl-10 pr-4 py-3 bg-gray-700 text-gray-100 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-red-500 focus:border-transparent transition duration-200 placeholder-gray-400"
7071
/>
71-
<span className="absolute left-3 top-12 transform -translate-y-1/2 text-gray-400">
72-
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
73-
<path d="M12 0C5.37 0 0 5.37 0 12c0 5.3 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61-.546-1.385-1.333-1.756-1.333-1.756-1.09-.745.083-.73.083-.73 1.205.135 1.838 1.24 1.838 1.24 1.065 1.885 2.876 1.34 3.578 1.025.112-.795.434-1.34.792-1.645-2.776-.315-5.686-1.385-5.686-6.165 0-1.365.487-2.48 1.287-3.355-.13-.315-.558-1.585.123-3.305 0 0 1.05-.335 3.44 1.285A12.01 12.01 0 0112 4.8c1.065.005 2.135.145 3.14.43 2.39-1.62 3.435-1.285 3.435-1.285.685 1.72.255 2.99.125 3.305.805.875 1.285 1.99 1.285 3.355 0 4.795-2.915 5.845-5.695 6.155.445.385.84 1.145.84 2.31 0 1.665-.015 3.015-.015 3.42 0 .32.215.695.825.575C20.565 21.8 24 17.3 24 12c0-6.63-5.37-12-12-12z" />
74-
</svg>
75-
</span>
7672
</div>
7773
<button
7874
onClick={scanVulnerabilities}
@@ -97,4 +93,4 @@ const GitHubVulnerabilityScanner = ({ setResult, isLoading, setIsLoading, userId
9793
);
9894
};
9995

100-
export default GitHubVulnerabilityScanner;
96+
export default GitHubVulnerabilityScanner;

frontend/src/hooks/useScaner.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ export const useScaner = () => {
88
const [isLoading, setIsLoading] = useState(false);
99
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
1010

11+
// NEW: lifted code state
12+
const [code, setCode] = useState("");
13+
const [url, setUrl] = useState(""); // <-- define state
14+
15+
1116
const location = useLocation();
1217
const selectedFiles = location.state?.selectedFiles || [];
1318

14-
// Get user data
1519
const user = JSON.parse(localStorage.getItem('user') || '{}');
1620
const userId = user.id;
1721

18-
// Handle body scroll when loading
1922
useEffect(() => {
2023
document.body.style.overflow = isLoading ? 'hidden' : 'auto';
2124
return () => {
@@ -27,8 +30,10 @@ export const useScaner = () => {
2730
setSourceType(null);
2831
setResult(null);
2932
setIsLoading(false);
33+
// Keep code if you want persistence across reset,
34+
// OR uncomment next line to clear when going back:
35+
// setCode("");
3036

31-
// Add smooth transition effect
3237
const scannerContent = document.querySelector('.scanner-content');
3338
if (scannerContent) {
3439
scannerContent.classList.add('opacity-0');
@@ -38,17 +43,9 @@ export const useScaner = () => {
3843
}
3944
};
4045

41-
const clearResult = () => {
42-
setResult(null);
43-
};
44-
45-
const openHistory = () => {
46-
setIsHistoryOpen(true);
47-
};
48-
49-
const closeHistory = () => {
50-
setIsHistoryOpen(false);
51-
};
46+
const clearResult = () => setResult(null);
47+
const openHistory = () => setIsHistoryOpen(true);
48+
const closeHistory = () => setIsHistoryOpen(false);
5249

5350
const handleCopy = async (content) => {
5451
try {
@@ -67,20 +64,20 @@ export const useScaner = () => {
6764
}
6865
};
6966

70-
const selectSourceType = (type) => {
71-
setSourceType(type);
72-
};
67+
const selectSourceType = (type) => setSourceType(type);
7368

7469
return {
75-
// State
7670
sourceType,
7771
result,
7872
isLoading,
7973
isHistoryOpen,
8074
selectedFiles,
8175
userId,
82-
83-
// Actions
76+
code,
77+
url,
78+
setUrl,
79+
setIsLoading, // NEW
80+
setCode, // NEW
8481
setResult,
8582
setIsLoading,
8683
resetScanner,
@@ -90,4 +87,4 @@ export const useScaner = () => {
9087
handleCopy,
9188
selectSourceType,
9289
};
93-
};
90+
};

frontend/src/pages/VulnerabilityScanner/VulnerabilityScanner.jsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const VulnerabilityScannerPage = () => {
3030
isHistoryOpen,
3131
selectedFiles,
3232
userId,
33+
code,
34+
url,
35+
setCode,
36+
setUrl,
3337
setResult,
3438
setIsLoading,
3539
resetScanner,
@@ -93,28 +97,39 @@ const VulnerabilityScannerPage = () => {
9397
};
9498

9599
// Vulnerability Scanner Component
96-
const VulnerabilityScannerComponent = ({ sourceType }) => {
97-
const scannerProps = {
98-
setResult,
99-
isLoading,
100-
setIsLoading,
101-
userId
102-
};
103-
104-
switch (sourceType) {
105-
case 'selected_repos':
106-
return <SelectedReposVulnerabilityScanner {...scannerProps} />;
107-
case 'github':
108-
return <GitHubVulnerabilityScanner {...scannerProps} />;
109-
case 'code':
110-
return <CodeVulnerabilityScanner {...scannerProps} preSelectedFiles={selectedFiles} />;
111-
case 'file':
112-
return <FileVulnerabilityScanner {...scannerProps} />;
113-
default:
114-
return null;
115-
}
100+
// inside VulnerabilityScannerComponent
101+
const VulnerabilityScannerComponent = ({ sourceType }) => {
102+
const scannerProps = {
103+
setResult,
104+
isLoading,
105+
setIsLoading,
106+
userId
116107
};
117108

109+
switch (sourceType) {
110+
case 'selected_repos':
111+
return <SelectedReposVulnerabilityScanner {...scannerProps} />;
112+
case 'github':
113+
return <GitHubVulnerabilityScanner {...scannerProps}
114+
url={url}
115+
setUrl={setUrl} />;
116+
case 'code':
117+
return (
118+
<CodeVulnerabilityScanner
119+
{...scannerProps}
120+
code={code}
121+
setCode={setCode}
122+
preSelectedFiles={selectedFiles}
123+
/>
124+
);
125+
case 'file':
126+
return <FileVulnerabilityScanner {...scannerProps} />;
127+
default:
128+
return null;
129+
}
130+
};
131+
132+
118133
// Results Section Component
119134
const ResultsSection = () => (
120135
<div className="results-section">

0 commit comments

Comments
 (0)