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

fix: allow access to '..' in mapfs #7575

Merged
merged 2 commits into from
Sep 27, 2024
Merged

Conversation

nikpivkin
Copy link
Contributor

@nikpivkin nikpivkin commented Sep 23, 2024

Description

The path “../” becomes “..” after cleaning, causing the MapFS check that the path lies outside FS to fail.

Before

trivy conf test/main.tf
2024-09-23T16:44:35+06:00       INFO    [misconfig] Misconfiguration scanning is enabled
2024-09-23T16:44:37+06:00       INFO    [terraform scanner] Scanning root module        file_path="."
2024-09-23T16:44:37+06:00       ERROR   [terraform evaluator] Failed to load module. Maybe try 'terraform init'?        err="file does not exist"
2024-09-23T16:44:37+06:00       INFO    Detected config files   num=1

After

./trivy conf test/main.tf
2024-09-23T17:23:36+06:00       INFO    [misconfig] Misconfiguration scanning is enabled
2024-09-23T17:23:37+06:00       INFO    [terraform scanner] Scanning root module        file_path="."
2024-09-23T17:23:37+06:00       INFO    Detected config files   num=2

../main.tf (terraform)

Tests: 10 (SUCCESSES: 1, FAILURES: 9, EXCEPTIONS: 0)
Failures: 9 (UNKNOWN: 0, LOW: 2, MEDIUM: 1, HIGH: 6, CRITICAL: 0)

HIGH: No public access block so not blocking public acls
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════

S3 buckets should block public ACLs on buckets and any objects they contain. By blocking, PUTs with fail if the object has any public ACL a.


See https://avd.aquasec.com/misconfig/avd-aws-0086
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 ../main.tf:1-3
   via main.tf:1-4 (module.test)
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 ┌ resource "aws_s3_bucket" "name" {
   2 │   bucket = "root"
   3 └ }
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
....

Related issues

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).

Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
@nikpivkin nikpivkin changed the title fix: allow access to '..' fix: allow access to '..' in mapfs Sep 25, 2024
@nikpivkin nikpivkin marked this pull request as ready for review September 25, 2024 06:36
@nikpivkin nikpivkin requested a review from knqyf263 as a code owner September 25, 2024 06:36
pkg/mapfs/fs.go Outdated
@@ -245,3 +245,7 @@ func cleanPath(path string) string {
path = strings.TrimLeft(path, "/") // Remove the leading slash
return path
}

func (m *FS) isPathAboveRoot(name string) bool {
return strings.HasPrefix(name, "..") && m.underlyingRoot != ""
Copy link
Collaborator

@knqyf263 knqyf263 Sep 25, 2024

Choose a reason for hiding this comment

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

Suggested change
return strings.HasPrefix(name, "..") && m.underlyingRoot != ""
return (name == ".." || strings.HasPrefix(name, "../")) && m.underlyingRoot != ""

A regular file can have .. prefix.

$ touch ..foo
$ ls -al
total 0
drwxr-xr-x  3 teppei   96  9 25 11:06 ./
drwxrwxrwt 37 root   1184  9 25 11:06 ../
-rw-r--r--  1 teppei    0  9 25 11:06 ..foo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed a002320

Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
@@ -478,3 +478,26 @@ func TestFS_RemoveAll(t *testing.T) {
require.ErrorIs(t, err, fs.ErrNotExist)
})
}

func TestFS_WithUnderlyingRoot(t *testing.T) {
root := "testdata/subdir"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any reason why testdata is used? Can we use filepath.Join(t.TempDir(), "subdir"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The testdata/subdir contains some files to which access is tested.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, thanks.

@@ -478,3 +478,26 @@ func TestFS_RemoveAll(t *testing.T) {
require.ErrorIs(t, err, fs.ErrNotExist)
})
}

func TestFS_WithUnderlyingRoot(t *testing.T) {
root := "testdata/subdir"
Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, thanks.

@knqyf263 knqyf263 added this pull request to the merge queue Sep 25, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 25, 2024
@simar7 simar7 added this pull request to the merge queue Sep 27, 2024
Merged via the queue into aquasecurity:main with commit a8fbe46 Sep 27, 2024
12 checks passed
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.

bug(terraform): Trivy does not load a module from the parent directory
3 participants