Skip to content

Commit 2e7a3f3

Browse files
fix: sarif output
1 parent 6c9c7f2 commit 2e7a3f3

File tree

6 files changed

+229
-15
lines changed

6 files changed

+229
-15
lines changed

.codacy/codacy.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
runtimes:
2-
32
43
54
65
76
tools:
8-
9-
10-
7+
118
12-
- pmd@6.55.0
13-
9+
- pmd@7.11.0
10+
1411
1512
1613

cmd/analyze.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,6 @@ func validatePaths(paths []string) error {
437437
return nil
438438
}
439439

440-
func validateCloudMode(cliLocalMode bool) error {
441-
if cliLocalMode {
442-
fmt.Println("Warning: cannot run in cloud mode")
443-
}
444-
return nil
445-
}
446-
447440
var analyzeCmd = &cobra.Command{
448441
Use: "analyze",
449442
Short: "Analyze code using configured tools",
@@ -465,8 +458,6 @@ Supports API token, provider, and repository flags to automatically fetch tool c
465458

466459
cliLocalMode := len(initFlags.ApiToken) == 0
467460

468-
validateCloudMode(cliLocalMode)
469-
470461
var toolsToRun map[string]*plugins.ToolInfo
471462

472463
if toolsToAnalyzeParam != "" {

example_1.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// vulneravel.ts
2+
3+
import * as http from 'http';
4+
import * as url from 'url';
5+
import * as fs from 'fs';
6+
import * as mysql from 'mysql';
7+
8+
// 1. Exposição de credenciais
9+
const db = mysql.createConnection({
10+
host: 'localhost',
11+
user: 'root',
12+
password: 'rootpassword', // Credenciais expostas
13+
database: 'test'
14+
});
15+
16+
http.createServer((req, res) => {
17+
const parsedUrl = url.parse(req.url || '', true);
18+
const query = parsedUrl.query;
19+
20+
// 2. Injeção SQL - FIXED: Use parameterized query
21+
const username = query.username;
22+
const sql = `SELECT * FROM users WHERE username = ?`;
23+
db.query(sql, [username], (err, result) => {
24+
if (err) throw err;
25+
26+
// 3. Exposição de dados sensíveis - FIXED: Filter sensitive fields
27+
const safeResult = result.map((user: any) => {
28+
const { password, ...safeUser } = user;
29+
return safeUser;
30+
});
31+
res.writeHead(200, { 'Content-Type': 'application/json' });
32+
res.end(JSON.stringify(safeResult)); // devolve dados sem campos sensíveis
33+
});
34+
35+
// 4. Leitura insegura de ficheiros
36+
const file = query.file as string;
37+
fs.readFile('./uploads/' + file, 'utf8', (err, data) => {
38+
if (!err) {
39+
res.write('\n\n' + data); // pode ser usado para leitura arbitrária de ficheiros
40+
}
41+
});
42+
43+
// 5. Execução de código arbitrário
44+
if (query.runCode) {
45+
eval(query.runCode as string); // MUITO perigoso
46+
}
47+
48+
}).listen(8080);
49+
50+
// 6. Dependência desatualizada (suponha que mysql está vulnerável)
51+
52+
// 7. Falta de HTTPS (http em vez de https)
53+
54+
// 8. Nenhuma validação de entrada em nenhuma parte
55+
56+
// 9. Stack traces revelados com throw err
57+
58+
// 10. Não existe autenticação nem controlo de acessos
59+
60+
console.log('Servidor inseguro a correr em http://localhost:8080');

out.txt

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
Warning: cannot run in cloud mode
2+
{
3+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
4+
"runs": [
5+
{
6+
"invocations": [
7+
{
8+
"executionSuccessful": true,
9+
"toolExecutionNotifications": []
10+
}
11+
],
12+
"results": [],
13+
"tool": {
14+
"driver": {
15+
"name": "Semgrep OSS",
16+
"rules": null,
17+
"semanticVersion": "1.78.0"
18+
}
19+
}
20+
},
21+
{
22+
"columnKind": "utf16CodeUnits",
23+
"originalUriBaseIds": {
24+
"ROOTPATH": {
25+
"uri": "file:///Users/czak/GIT/codacy/codacy-cli-v2/example_1.ts/"
26+
}
27+
},
28+
"results": [],
29+
"tool": {
30+
"driver": {
31+
"fullName": "Trivy Vulnerability Scanner",
32+
"informationUri": "https://github.com/aquasecurity/trivy",
33+
"name": "Trivy",
34+
"rules": null,
35+
"version": "0.59.1"
36+
}
37+
}
38+
},
39+
{
40+
"artifacts": [
41+
{
42+
"location": {
43+
"uri": "file:///Users/czak/GIT/codacy/codacy-cli-v2/example_1.ts"
44+
}
45+
}
46+
],
47+
"invocations": [
48+
{
49+
"executionSuccessful": true,
50+
"toolConfigurationNotifications": [
51+
{
52+
"descriptor": {
53+
"id": "ESL0999"
54+
},
55+
"level": "warning",
56+
"locations": [
57+
{
58+
"physicalLocation": {
59+
"artifactLocation": {
60+
"index": 0,
61+
"uri": "file:///Users/czak/GIT/codacy/codacy-cli-v2/example_1.ts"
62+
}
63+
}
64+
}
65+
],
66+
"message": {
67+
"text": "File ignored because no matching configuration was supplied."
68+
}
69+
}
70+
]
71+
}
72+
],
73+
"results": [],
74+
"tool": {
75+
"driver": {
76+
"informationUri": "https://eslint.org",
77+
"name": "ESLint",
78+
"rules": null,
79+
"version": "9.26.0"
80+
}
81+
}
82+
},
83+
{
84+
"results": [
85+
{
86+
"level": "error",
87+
"locations": [
88+
{
89+
"physicalLocation": {
90+
"artifactLocation": {
91+
"uri": "example_1.ts"
92+
},
93+
"region": {
94+
"startColumn": 1,
95+
"startLine": 1
96+
}
97+
}
98+
}
99+
],
100+
"message": {
101+
"text": "Parsing failed: 'invalid syntax (example_1, line 1)'"
102+
},
103+
"ruleId": "syntax-error"
104+
}
105+
],
106+
"tool": {
107+
"driver": {
108+
"informationUri": "https://pylint.org",
109+
"name": "Pylint",
110+
"rules": null,
111+
"version": "3.3.6"
112+
}
113+
}
114+
},
115+
{
116+
"results": [
117+
{
118+
"locations": [
119+
{
120+
"physicalLocation": {
121+
"artifactLocation": {
122+
"uri": "example_1.ts"
123+
},
124+
"region": {
125+
"startColumn": 1,
126+
"startLine": 3
127+
}
128+
}
129+
}
130+
],
131+
"message": {
132+
"text": "invalid file example_1.ts: example_1.ts:3:1: expected 'package', found 'import' (and 4 more errors)"
133+
}
134+
}
135+
],
136+
"tool": {
137+
"driver": {
138+
"informationUri": "https://github.com/mgechev/revive",
139+
"name": "revive",
140+
"rules": null
141+
}
142+
}
143+
}
144+
],
145+
"version": "2.1.0"
146+
}

req.txt

Whitespace-only changes.

vul.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
// 5. Execução de código arbitrário
4+
if (query.runCode) {
5+
eval(query.runCode as string); // MUITO perigoso
6+
}
7+
8+
}).listen(8080);
9+
10+
// 6. Dependência desatualizada (suponha que mysql está vulnerável)
11+
12+
// 7. Falta de HTTPS (http em vez de https)
13+
14+
// 8. Nenhuma validação de entrada em nenhuma parte
15+
16+
// 9. Stack traces revelados com throw err
17+
18+
// 10. Não existe autenticação nem controlo de acessos
19+
20+
console.log('Servidor inseguro a correr em http://localhost:8080');

0 commit comments

Comments
 (0)