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

The plugin cannot handle relative paths in container tar files in some cases #218

Closed
mladkau opened this issue May 29, 2020 · 4 comments · Fixed by #244
Closed

The plugin cannot handle relative paths in container tar files in some cases #218

mladkau opened this issue May 29, 2020 · 4 comments · Fixed by #244
Assignees
Labels
bug Something isn't working

Comments

@mladkau
Copy link
Contributor

mladkau commented May 29, 2020

  • node -v: v12.17.0
  • npm -v: 6.14.4
  • snyk -v: 1.320.1

Expected behaviour

The plugin can handle relative paths in different variants in a container tar file or produces an appropriate error message if input is unsupported. Please add documentation if the accepted input needs to meet special requirements which are not stated explicitly in the official spec(s): https://github.com/moby/moby/blob/master/image/spec/v1.md

Actual behaviour

Some form of relative paths are not handled correctly and produce an unexpected exception.

Steps to reproduce

  • Unpack the fixture archive: test/fixture/docker-archives/docker-save/nginx.tar into a subdirectory.
  • Repack the image in the subdirectory with: tar cf ../nginx.tar .
  • Run test/system/static.test.ts -> The test FAILS
  • Repack the image in the subdirectory with: tar cf ../nginx.tar *
  • Run test/system/static.test.ts -> The test PASSES

When the test fails the code produces an unexpected exception:

 FAIL  test/system/static.test.ts
 ✖ Cannot read property 'Layers' of undefined

    tarExtractor.on("entry", async (header, stream, next) => {
      if (header.type === "file") {
----------------------------------^
        if (isTarFile(header.name)) {
          layers[header.name] = await extractImageLayer(stream, extractActions);

  test: static analysis builds the expected response
  stack: |
    getLayersContentAndArchiveManifest (lib/extractor/layer.ts:32:35)
    Extract.<anonymous> (lib/extractor/layer.ts:6:50)
    finishMaybe (node_modules/readable-stream/lib/_stream_writable.js:624:14)
    node_modules/readable-stream/lib/_stream_writable.js:599:5
    Extract._final (node_modules/tar-stream/extract.js:254:3)
    callFinal (node_modules/readable-stream/lib/_stream_writable.js:590:10)
  at:
    line: 32
    column: 35
    file: lib/extractor/layer.ts
    function: getLayersContentAndArchiveManifest
  type: TypeError
  tapCaught: uncaughtException

The reason for the unexpected exception is that paths are used for computation in a not normalized form.

Header is stored in a Map
https://github.com/snyk/snyk-docker-plugin/blob/master/lib/extractor/layer.ts#L34

Layer are filtered from paths mentioned in manifest.json:
https://github.com/snyk/snyk-docker-plugin/blob/master/lib/extractor/layer.ts#L127

If the paths defined in manifest.json are not exactly like the paths in the .tar archive (check with tar -tf nginx.tar) then the plugin produces an exception.

Solution:

Please normalize paths before using them for any computation. (See: https://nodejs.org/api/path.html#path_path_normalize_path).

@mladkau
Copy link
Contributor Author

mladkau commented May 29, 2020

Relevant slack discussion: https://snyk.slack.com/archives/CGSNWAE76/p1590147147160200

@karniwl
Copy link
Contributor

karniwl commented Jun 29, 2020

@shaimendel @snyk/runtime can we get a response from you on this issue?

@ivanstanev
Copy link
Contributor

I think with our recent fix to run on Windows we normalized all paths referenced in the plugin so this is probably not an issue any more.

@ivanstanev ivanstanev self-assigned this Sep 29, 2020
@ivanstanev ivanstanev added the bug Something isn't working label Sep 29, 2020
@ivanstanev ivanstanev linked a pull request Sep 29, 2020 that will close this issue
3 tasks
@ivanstanev
Copy link
Contributor

@mladkau I'm pretty sure this is resolved now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants