Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): use full path for nested jars #3992

Merged
merged 10 commits into from
Apr 24, 2023

Conversation

DmitriyLewen
Copy link
Contributor

@DmitriyLewen DmitriyLewen commented Apr 5, 2023

Description

Use full path for nested jars.
The table format still only uses name of root jar.
The json format uses full path.

Before:

test_jars/test.jar

After:

test_jars/test.jar/test/log4j-1.2.12.jar

Related issues

Related PRs

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Apr 6, 2023
@@ -105,11 +106,15 @@ func toApplication(fileType, filePath, libFilePath string, r dio.ReadSeekerAt, l
}
locs = append(locs, l)
}
libPath := libFilePath
if lib.FilePath != "" {
libPath = filepath.ToSlash(lib.FilePath)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use / in PkgPath when scanning in Windows.
That's why I added ToSlash here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we should have used slashes here. path.Join must be used rather than filepath.Join. The inner separator in ZIP files is always slashed even though it is created on Windows.

https://github.com/DmitriyLewen/go-dep-parser/blob/4888ccee8867aa2d59ad2966faf06944e808e8e3/pkg/java/jar/parse.go#L196

4.4.17 file name: (Variable)

  4.4.17.1 The name of the file, with optional relative path.
  The path stored MUST NOT contain a drive or
  device letter, or a leading slash.  All slashes
  MUST be forward slashes '/' as opposed to
  backwards slashes '\' for compatibility with Amiga
  and UNIX file systems etc.  If input came from standard
  input, there is no file name field.  

https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT

Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 564b68b

@@ -105,11 +105,15 @@ func toApplication(fileType, filePath, libFilePath string, r dio.ReadSeekerAt, l
}
locs = append(locs, l)
}
libPath := libFilePath
if lib.FilePath != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment?

Suggested change
if lib.FilePath != "" {
// This file path is populated for virtual file paths within archives, such as nested JAR files.
if lib.FilePath != "" {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 98da085

Comment on lines 283 to 290
firstExtIndex := len(path)
for _, ext := range jarExtensions {
i := strings.Index(path, ext)
if i != -1 && i < firstExtIndex {
firstExtIndex = i + len(ext)
}
}
return path[:firstExtIndex]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid of the case where the path /path/to/test.warning/app.jar. It is rare, though.

Suggested change
firstExtIndex := len(path)
for _, ext := range jarExtensions {
i := strings.Index(path, ext)
if i != -1 && i < firstExtIndex {
firstExtIndex = i + len(ext)
}
}
return path[:firstExtIndex]
// File paths are always forward-slashed in Trivy
paths := strings.Split(path, "/")
for i, p := range paths {
if slices.Contains(jarExtensions, filepath.Ext(p)) {
return strings.Join(paths[:i+1], "/")
}
}
return path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 98da085

@knqyf263 knqyf263 merged commit 8559841 into aquasecurity:main Apr 24, 2023
8 checks passed
@DmitriyLewen DmitriyLewen deleted the feat/jar-nested-path branch April 24, 2023 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show JAR nested path of vulnerable package
2 participants