unjs / nanotar

📼 Tiny and fast tar utils for any JavaScript runtime!
MIT License
125 stars 7 forks source link

`attrs.mode` can return an invalid string #17

Open ayuhito opened 2 months ago

ayuhito commented 2 months ago

Environment

Node.js 18 nanotar: 0.1.1

Reproduction

Example attributes:

attrs: {
      mode: '000666 ',
      uid: 765,
      gid: 24,
      mtime: 1647357732,
      user: '',
      group: ''
    }

Describe the bug

There's a trailing space in attrs.mode: mode: '000666 '. This isn't the fault of createTar, but the tar file I'm reading is slightly malformed and seems to return this.

Technically, this wouldn't be a problem if we were returning octal literals instead. Then anything from 0644 to 0000644 or more would be valid. For now I'm calling trim on the string when calling fs.writeFile(..., mode: file.attrs?.mode?.trim()), but this feels very very very wrong to do so in practice.

Maybe an API change to return numbers instead of a string might feel more intuitive to the end user who is likely to be using fs anyways?

Additional context

No response

Logs

No response

kricsleo commented 2 months ago

Same issue here, I would suggest adding a trim internally: https://github.com/unjs/nanotar/blob/c1247bdec97163b487c8ca55003e291dfea755ab/src/parse.ts#L18

-    const mode = _readString(buffer, offset + 100, 8);
+    const mode = _readString(buffer, offset + 100, 8).trim();