Skip to content

Commit

Permalink
Merge pull request #228 from luotianqi777/mvn_comment
Browse files Browse the repository at this point in the history
docs: mvn comment
  • Loading branch information
luotianqi777 authored Dec 23, 2023
2 parents 44ff5c3 + 876c39e commit 2511e79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
21 changes: 8 additions & 13 deletions opensca/sca/java/mvn.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,26 @@ func inheritModules(poms []*Pom) {
// 从每个根pom开始遍历
v.ForEachPath(func(p, n *model.DepGraph) bool {

// 判断parent是否有expand来判断是否已经继承过属性
expand := false
// Expand为空代表当前节点已传递属性
if n.Expand == nil {
return true
}

// 判断parent.Expand是否为空来判断parent是否已经传递过属性
for _, p := range n.Parents {
if p.Expand != nil {
expand = true
// 至少一个parent尚未传递属性则不处理当前节点
return true
}
}

// 至少一个parent尚未继承属性则暂不处理当前节点
if expand {
return true
}

if n.Expand == nil {
return true
}

// 获取当前pom
pom, ok := n.Expand.(*Pom)
if !ok {
return true
}

// 删除expand标识已继承属性
// 置空Expand标记该节点已传递属性
n.Expand = nil

// 将属性传递给需要继承的pom
Expand Down
40 changes: 31 additions & 9 deletions opensca/sca/java/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/java/xml"
)

// Pom pom信息
type Pom struct {
PomDependency
Parent PomDependency `xml:"parent"`
Expand All @@ -22,9 +23,11 @@ type Pom struct {
Mirrors []string `xml:"mirrors>mirror>url"`
Licenses []string `xml:"licenses>license>name"`
Profiles []Pom `xml:"profiles>profile"`
File *model.File `xml:"-" json:"-"`
// 当前pom对应的文件信息
File *model.File `xml:"-" json:"-"`
}

// PomDependency pom依赖
type PomDependency struct {
ArtifactId string `xml:"artifactId"`
GroupId string `xml:"groupId"`
Expand All @@ -35,20 +38,31 @@ type PomDependency struct {
RelativePath string `xml:"relativePath"`
Optional bool `xml:"optional"`
Exclusions []*PomDependency `xml:"exclusions>exclusion"`
Define *Pom `xml:"-"`
RefProperty *Property `xml:"-"`
Start int `xml:",start"`
End int `xml:",end"`
// 定义当前依赖的pom
Define *Pom `xml:"-"`
// 当前依赖引用的属性
RefProperty *Property `xml:"-"`
// 定义当前依赖标签位置起始行号
Start int `xml:",start"`
// 定义当前依赖标签位置结束行号
End int `xml:",end"`
}

// Property pom属性
type Property struct {
Key string
Value string
// 属性名
Key string
// 属性值
Value string
// 定义当前属性的pom
Define *Pom
Start int `xml:",start"`
End int `xml:",end"`
// 定义当前属性标签位置起始行号
Start int
// 定义当前属性标签位置结束行号
End int
}

// PomProperties pom属性集合
type PomProperties map[string]*Property

func (pp *PomProperties) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error {
Expand Down Expand Up @@ -76,6 +90,7 @@ func (pp *PomProperties) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error
return nil
}

// NeedExclusion 判断是否是当前依赖需要排除的子依赖
func (pd PomDependency) NeedExclusion(dep PomDependency) bool {
check := func(s1, s2 string) bool {
return s1 == "" || s1 == "*" || s1 == s2
Expand Down Expand Up @@ -113,6 +128,7 @@ func (pd PomDependency) Index4() string {
return fmt.Sprintf("%s:%s", pd.Index3(), pd.Scope)
}

// ReadPom 读取pom信息
func ReadPom(reader io.Reader) *Pom {

data, err := io.ReadAll(reader)
Expand Down Expand Up @@ -207,6 +223,7 @@ func ReadPom(reader io.Reader) *Pom {
return p
}

// Update 使用pom信息更新当前依赖中使用的属性
func (p *Pom) Update(dep *PomDependency) {
var ref *Property
dep.GroupId, ref = p.update(dep.GroupId)
Expand All @@ -221,6 +238,9 @@ func (p *Pom) Update(dep *PomDependency) {

var propertyReg = regexp.MustCompile(`\$\{[^{}]*\}`)

// update 使用pom信息更新字符串中使用的属性
// val: 更新后的字符串
// ref: 更新后最终应用的属性信息
func (p *Pom) update(value string) (val string, ref *Property) {
val = propertyReg.ReplaceAllStringFunc(value,
func(s string) string {
Expand Down Expand Up @@ -248,6 +268,7 @@ func (p *Pom) update(value string) (val string, ref *Property) {

var reg = regexp.MustCompile(`\s`)

// trimSpace 清除空白字符
func trimSpace(p *PomDependency) {
if p == nil {
return
Expand All @@ -263,6 +284,7 @@ func trimSpace(p *PomDependency) {
p.Type = trim(p.Type)
}

// Check 检查是否是合法maven依赖
func (dep PomDependency) Check() bool {
return !(dep.ArtifactId == "" || dep.GroupId == "" || dep.Version == "" || strings.Contains(dep.GAV(), "$"))
}
Expand Down

0 comments on commit 2511e79

Please sign in to comment.