Skip to content

Commit

Permalink
Code rules: Do not allow console.log in the source code (#235)
Browse files Browse the repository at this point in the history
* Add console.log semgrep rule

* make sure the error is a warning
  • Loading branch information
academo authored Jul 22, 2024
1 parent ac28776 commit 188d54f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
40 changes: 39 additions & 1 deletion pkg/analysis/passes/coderules/coderules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,43 @@ func TestUseSyscall(t *testing.T) {
_, err := Analyzer.Run(pass)
require.NoError(t, err)
require.Len(t, interceptor.Diagnostics, 1)
require.Equal(t, "It is not permitted to use the syscall module. Using syscall.Getcwd is not permitted", interceptor.Diagnostics[0].Title)
require.Equal(
t,
"It is not permitted to use the syscall module. Using syscall.Getcwd is not permitted",
interceptor.Diagnostics[0].Title,
)
}

func TestJSConsoleLog(t *testing.T) {
if !isSemgrepInstalled() {
t.Skip("semgrep not installed, skipping test")
return
}
var interceptor testpassinterceptor.TestPassInterceptor
pass := &analysis.Pass{
RootDir: filepath.Join("./"),
ResultOf: map[*analysis.Analyzer]interface{}{
sourcecode.Analyzer: filepath.Join("testdata", "console-log"),
},
Report: interceptor.ReportInterceptor(),
}

_, err := Analyzer.Run(pass)
require.NoError(t, err)
require.Len(t, interceptor.Diagnostics, 1)
require.Equal(
t,
"Console logging detected. Plugins should not log to the console.",
interceptor.Diagnostics[0].Title,
)
require.Equal(
t,
interceptor.Diagnostics[0].Detail,
"Code rule violation found in testdata/console-log/index.ts at line 2",
)
require.Equal(
t,
interceptor.Diagnostics[0].Severity,
analysis.Warning,
)
}
10 changes: 10 additions & 0 deletions pkg/analysis/passes/coderules/semgrep-rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ rules:
message: It is not permitted to use the syscall module. Using syscall.$F is not permitted
languages: [go]
severity: ERROR

- id: detect-console-logs
pattern-either:
- pattern: console.log(...)
- pattern: console.info(...)
- pattern: console.table(...)
- pattern: console.error(...)
message: "Console logging detected. Plugins should not log to the console."
languages: [javascript, typescript]
severity: WARNING
3 changes: 3 additions & 0 deletions pkg/analysis/passes/coderules/testdata/console-log/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function test() {
console.log("test");
}

0 comments on commit 188d54f

Please sign in to comment.