forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVM-439 Custom Lint rule to check file permissions (0xPolygon#1271)
* New lint rule * Remove test file * change uts * Add unit tests for custom lint rules * Comments fix
- Loading branch information
1 parent
87936ea
commit cd3b34d
Showing
9 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package gorules | ||
|
||
import ( | ||
"github.com/quasilyte/go-ruleguard/dsl" | ||
) | ||
|
||
func OsFilePermissionRule(m dsl.Matcher) { | ||
m.Match(`os.$name($file, $number, 0777)`).Report("os.$name called with file mode 0777") | ||
m.Match(`os.$name($file, 0777)`).Report("os.$name called with file mode 0777") | ||
m.Match(`os.$name(0777)`).Report("os.$name called with file mode 0777") | ||
|
||
m.Match(`os.$name($file, $number, os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") | ||
m.Match(`os.$name($file, os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") | ||
m.Match(`os.$name(os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package gorules | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/quasilyte/go-ruleguard/analyzer" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestRules(t *testing.T) { | ||
t.Parallel() | ||
|
||
testdata := analysistest.TestData() | ||
|
||
rules := "rules.go" // if we are to separate rules in different files, here we would write them (e.g., rule1.go,rule2.go,rule3.go) | ||
if err := analyzer.Analyzer.Flags.Set("rules", rules); err != nil { | ||
t.Fatalf("set rules flag: %v", err) | ||
} | ||
|
||
analysistest.Run(t, testdata, analyzer.Analyzer, "./...") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package target | ||
|
||
import "os" | ||
|
||
func testLintError() { | ||
os.Mkdir("test", 0777) // want `OsFilePermissionRule: os.Mkdir called with file mode` | ||
os.Mkdir("test", os.ModePerm) // want `OsFilePermissionRule: os.Mkdir called with file mode` | ||
os.MkdirAll("test", 0777) // want `OsFilePermissionRule: os.MkdirAll called with file mode` | ||
os.MkdirAll("test", os.ModePerm) // want `OsFilePermissionRule: os.MkdirAll called with file mode` | ||
os.Chmod("test", 0777) // want `OsFilePermissionRule: os.Chmod called with file mode` | ||
os.Chmod("test", os.ModePerm) // want `OsFilePermissionRule: os.Chmod called with file mode` | ||
os.OpenFile("test", 0, 0777) // want `OsFilePermissionRule: os.OpenFile called with file mode` | ||
os.OpenFile("test", 0, os.ModePerm) // want `OsFilePermissionRule: os.OpenFile called with file mode` | ||
} | ||
|
||
func testNoLintError() { | ||
os.Mkdir("test", 67108864) | ||
os.Mkdir("test", os.ModeDevice) | ||
os.MkdirAll("test", 67108864) | ||
os.MkdirAll("test", os.ModeDevice) | ||
os.Chmod("test", 67108864) | ||
os.Chmod("test", os.ModeDevice) | ||
os.OpenFile("test", 0, 67108864) | ||
os.OpenFile("test", 0, os.ModeDevice) | ||
} |