Skip to content

Commit e7d41d0

Browse files
committed
profile: support directives in blocks
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent c6ec268 commit e7d41d0

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

docs/syntax.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ require (
2121
example.com/mod201 v1.2.3 // indirect
2222
)
2323
24-
// policy cannot be specified here because the parser ignores
25-
// the comment lines here
24+
//gomodjail:unconfined
2625
require (
26+
example.com/mod300 v1.2.3
27+
example.com/mod301 v1.2.3 // gomodjail:confined
28+
example.com/mod302 v1.2.3
2729
)
2830
```
2931

30-
This makes the following modules confined: `mod100`, `mod102`, and `mod201`.
32+
This makes the following modules confined: `mod100`, `mod102`, `mod201`, and `mod301`.
3133

3234
The version numbers are ignored.

pkg/profile/fromgomod/fromgomod.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,63 @@ func FromGoMod(mod *modfile.File, prof *profile.Profile) error {
3737
}
3838
}
3939

40-
for _, f := range mod.Require {
41-
if syn := f.Syntax; syn != nil {
42-
pol := currentDefaultPolicy
43-
for _, c := range append(syn.Before, syn.Suffix...) {
40+
for _, stmt := range mod.Syntax.Stmt {
41+
lineBlock, ok := stmt.(*modfile.LineBlock)
42+
if !ok {
43+
continue
44+
}
45+
currentBlockDefaultPolicy := currentDefaultPolicy
46+
for _, c := range append(lineBlock.Before, lineBlock.Suffix...) {
47+
if tok := c.Token; tok != "" {
48+
pol, err := policyFromComment(tok)
49+
if err != nil {
50+
err = fmt.Errorf("failed to parse comment %+v: %w", c, err)
51+
return err
52+
}
53+
if lineBlock.Token[0] != "require" {
54+
return fmt.Errorf("policy %q is specified in a non-require block", pol)
55+
}
56+
currentBlockDefaultPolicy = pol
57+
}
58+
}
59+
for _, line := range lineBlock.Line {
60+
if line == nil {
61+
continue
62+
}
63+
pol := currentBlockDefaultPolicy
64+
for _, c := range append(line.Before, line.Suffix...) {
4465
if tok := c.Token; tok != "" {
4566
polFromComment, err := policyFromComment(tok)
4667
if err != nil {
4768
err = fmt.Errorf("failed to parse comment %+v: %w", c, err)
4869
return err
4970
}
71+
if lineBlock.Token[0] != "require" {
72+
return fmt.Errorf("policy %q is specified in a non-require block", polFromComment)
73+
}
5074
if polFromComment != "" {
5175
pol = polFromComment
5276
}
5377
}
5478
}
5579
if pol == "" {
56-
pol = currentDefaultPolicy
80+
pol = currentBlockDefaultPolicy
5781
}
5882
if pol == profile.PolicyUnconfined {
5983
pol = "" // reduce map size
6084
}
61-
if existPol, ok := prof.Modules[f.Mod.Path]; ok && existPol != pol {
62-
slog.Warn("Overwriting an existing policy", "module", f.Mod.Path, "old", existPol, "new", pol)
85+
modPath := line.Token[0]
86+
if existPol, ok := prof.Modules[modPath]; ok && existPol != pol {
87+
slog.Warn("Overwriting an existing policy", "module", modPath, "old", existPol, "new", pol)
6388
}
6489
if pol == "" {
65-
delete(prof.Modules, f.Mod.Path)
90+
delete(prof.Modules, modPath)
6691
} else {
67-
prof.Modules[f.Mod.Path] = pol
92+
prof.Modules[modPath] = pol
6893
}
6994
}
7095
}
96+
7197
return nil
7298
}
7399

pkg/profile/fromgomod/fromgomod_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,28 @@ require (
7474
example.com/mod202 v1.2.3 // indirect
7575
)
7676
77-
// policy cannot be specified here because the parser ignores
78-
// the comment lines here
77+
//gomodjail:unconfined
7978
require (
79+
example.com/mod300 v1.2.3
80+
example.com/mod301 v1.2.3 // gomodjail:confined
81+
example.com/mod302 v1.2.3
82+
)
83+
84+
// gomodjail:confined
85+
require (
86+
example.com/mod400 v1.2.3 // indirect
87+
example.com/mod401 v1.2.3 // indirect // gomodjail:unconfined
88+
example.com/mod402 v1.2.3 // indirect
8089
)
8190
`,
8291
expected: map[string]string{
8392
"example.com/mod100": "confined",
8493
"example.com/mod102": "confined",
8594
"example.com/mod201": "confined",
8695
"example.com/mod202": "confined",
96+
"example.com/mod301": "confined",
97+
"example.com/mod400": "confined",
98+
"example.com/mod402": "confined",
8799
},
88100
},
89101
}

0 commit comments

Comments
 (0)