Skip to content

Commit 0be2267

Browse files
authored
Merge pull request #56 from tstromberg/main
Shorten terminal rendering width
2 parents 0a34bf6 + 0770c5a commit 0be2267

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

bincapz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestDiff(t *testing.T) {
185185

186186
got := out.String()
187187
if diff := cmp.Diff(got, want); diff != "" {
188-
t.Errorf("unexpected diff: %s\ngot: %s", diff, got)
188+
t.Errorf("unexpected diff: %s", diff)
189189
}
190190

191191
})

pkg/render/terminal.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,22 @@ func renderTable(fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
133133
tWidth := terminalWidth()
134134
keyWidth := 36
135135
riskWidth := 7
136-
padding := 7
137-
descWidth := tWidth - keyWidth - riskWidth - padding
136+
padding := 4
137+
maxKeyLen := 0
138+
139+
for _, k := range kbs {
140+
key := forceWrap(k.Key, keyWidth)
141+
if len(key) > maxKeyLen {
142+
maxKeyLen = len(key)
143+
}
144+
}
145+
146+
descWidth := tWidth - maxKeyLen - riskWidth - padding
138147
if descWidth > 120 {
139148
descWidth = 120
140149
}
141150

142151
klog.Infof("terminal width: %d - desc width: %d", tWidth, descWidth)
143-
maxKeyLen := 0
144152

145153
for _, k := range kbs {
146154
desc := k.Behavior.Description
@@ -155,6 +163,8 @@ func renderTable(fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
155163
desc = fmt.Sprintf("by %s", k.Behavior.RuleAuthor)
156164
}
157165
}
166+
167+
key := forceWrap(k.Key, keyWidth)
158168
words, _ := tablewriter.WrapString(desc, descWidth)
159169

160170
// klog.Infof("%s / %s - %s", k.Key, desc, k.Behavior.)
@@ -171,11 +181,6 @@ func renderTable(fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
171181
desc = fmt.Sprintf("%s:%s%s%s", desc, before, forceWrap(strings.Join(k.Behavior.Values, "\n"), descWidth), after)
172182
}
173183

174-
key := forceWrap(k.Key, keyWidth)
175-
if len(key) > maxKeyLen {
176-
maxKeyLen = len(key)
177-
}
178-
179184
// lowercase first character for consistency
180185
desc = strings.ToLower(string(desc[0])) + desc[1:]
181186
risk := fmt.Sprintf("%d/%s", k.Behavior.RiskScore, k.Behavior.RiskLevel)
@@ -191,14 +196,31 @@ func renderTable(fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
191196

192197
if title != "" {
193198
fmt.Fprintf(w, "%s\n", title)
194-
fmt.Fprintf(w, "%s\n", strings.Repeat("-", 70))
195199
}
196200

197201
table := tablewriter.NewWriter(os.Stdout)
198202
table.SetAutoWrapText(false)
199203
// table.SetHeader([]string{"Risk", "Key", "Description"})
200204
table.SetBorder(false)
201-
// ttable.SetBorders(tablewriter.Border{Left: false, Top: true, Right: false, Bottom: false})
205+
table.SetCenterSeparator("")
206+
// SetBorders miscalculates length with NoWhiteSpace
207+
//table.SetBorders(tablewriter.Border{Left: false, Top: true, Right: false, Bottom: false})
208+
maxDescWidth := 0
209+
maxRiskWidth := 0
210+
for _, d := range data {
211+
if len(d[0]) > maxRiskWidth {
212+
maxRiskWidth = len(d[0])
213+
}
214+
for _, l := range strings.Split(d[2], "\n") {
215+
if len(l) > maxDescWidth {
216+
maxDescWidth = len(l)
217+
}
218+
}
219+
220+
}
221+
tableWidth := maxKeyLen + maxDescWidth + padding + maxRiskWidth
222+
klog.Infof("table width: %d", tableWidth)
223+
fmt.Fprintf(w, "%s\n", strings.Repeat("-", tableWidth))
202224
table.SetNoWhiteSpace(true)
203225
table.SetTablePadding(" ")
204226
descColor := tablewriter.Normal

rules/fs/directory-traverse.yara

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
rule fts {
22
meta:
3-
description = "traverse filesystem hierarchies"
3+
description = "traverse filesystem hierarchy"
44
syscall = "openat,getdents"
55
pledge = "rpath"
66
strings:
@@ -15,7 +15,7 @@ rule fts {
1515

1616
rule py_walk : notable {
1717
meta:
18-
description = "traverse filesystem hierarchies"
18+
description = "traverse filesystem hierarchy"
1919
strings:
2020
$walk = "os.walk"
2121
condition:

rules/fs/permission-get.yara

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
rule getmode {
22
meta:
3-
description = "Uses libc functions to access filesystem information"
3+
description = "looks up file permissions"
44
pledge = "rpath"
55
strings:
66
$_chmod = "_getmode"

rules/fs/permission-modify.yara

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
rule chmod : notable {
22
meta:
3-
description = "modifies file permissions using chmod"
3+
description = "modifies file permissions"
44
pledge = "fattr"
55
syscall = "chmod"
66
strings:
@@ -14,7 +14,7 @@ rule chmod : notable {
1414

1515
rule fchmod : notamble {
1616
meta:
17-
description = "modifies file permissions using fchmod"
17+
description = "modifies file permissions"
1818
pledge = "fattr"
1919
syscall = "fchmodat"
2020
strings:

testdata/macOS/libffmpeg.dirty.mdiff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| +1/LOW | **random/insecure** | generate random numbers insecurely |
1212
| +1/LOW | **sync/semaphore/user** | uses semaphores to synchronize data between processes or threads |
1313
| +2/MED | **exec/pipe** | uses popen to launch a program and pipe output to/from it |
14-
| +2/MED | **fs/permission/modify** | modifies file permissions using chmod |
14+
| +2/MED | **fs/permission/modify** | modifies file permissions |
1515
| +2/MED | **net/http/cookies** | able to access HTTP resources using cookies |
1616
| +2/MED | **net/url/request** | requests resources via URL |
1717
| +2/MED | **ref/path/hidden** | references a hidden file that can be generated dynamically: "%s/.main_storage" |

0 commit comments

Comments
 (0)