Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/cli/unpack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unzipSync } from "fflate";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`

Check warning on line 2 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Replace `·chmodSync,·existsSync,·mkdirSync,·readFileSync,·writeFileSync·` with `⏎··chmodSync,⏎··existsSync,⏎··mkdirSync,⏎··readFileSync,⏎··writeFileSync,⏎`
import { join, resolve } from "path";

import { extractSignatureBlock } from "../node/sign.js";
Expand Down Expand Up @@ -34,6 +34,51 @@
const fileContent = readFileSync(resolvedDxtPath);
const { originalContent } = extractSignatureBlock(fileContent);

// Parse file attributes from ZIP central directory
const fileAttributes = new Map<string, number>();
const isUnix = process.platform !== "win32";

if (isUnix) {
// Parse ZIP central directory to extract file attributes
const zipBuffer = originalContent;

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Delete `······`

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Delete `······`

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Delete `······`

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Delete `······`

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Delete `······`

Check warning on line 44 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Delete `······`
// Find end of central directory record
let eocdOffset = -1;
for (let i = zipBuffer.length - 22; i >= 0; i--) {
if (zipBuffer.readUInt32LE(i) === 0x06054b50) {
eocdOffset = i;
break;
}
}

if (eocdOffset !== -1) {
const centralDirOffset = zipBuffer.readUInt32LE(eocdOffset + 16);
const centralDirEntries = zipBuffer.readUInt16LE(eocdOffset + 8);

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Delete `········`

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Delete `········`

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Delete `········`

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Delete `········`

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Delete `········`

Check warning on line 57 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Delete `········`
let offset = centralDirOffset;

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Delete `········`

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Delete `········`

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Delete `········`

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Delete `········`

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Delete `········`

Check warning on line 59 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Delete `········`
for (let i = 0; i < centralDirEntries; i++) {
if (zipBuffer.readUInt32LE(offset) === 0x02014b50) {
const externalAttrs = zipBuffer.readUInt32LE(offset + 38);
const filenameLength = zipBuffer.readUInt16LE(offset + 28);
const filename = zipBuffer.toString('utf8', offset + 46, offset + 46 + filenameLength);

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 64 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Replace `'utf8',·offset·+·46,·offset·+·46·+·filenameLength);` with `⏎··············"utf8",⏎··············offset·+·46,⏎··············offset·+·46·+·filenameLength,`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Insert `);⏎`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Insert `);⏎`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Insert `);⏎`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Insert `);⏎`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Insert `);⏎`

Check warning on line 65 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Insert `);⏎`
// Extract Unix permissions from external attributes (upper 16 bits)
const mode = (externalAttrs >> 16) & 0o777;
if (mode > 0) {
fileAttributes.set(filename, mode);
}

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Delete `············`

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Delete `············`

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Delete `············`

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Delete `············`

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Delete `············`

Check warning on line 71 in src/cli/unpack.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Delete `············`
const extraFieldLength = zipBuffer.readUInt16LE(offset + 30);
const commentLength = zipBuffer.readUInt16LE(offset + 32);
offset += 46 + filenameLength + extraFieldLength + commentLength;
} else {
break;
}
}
}
}

const decompressed = unzipSync(originalContent);

for (const relativePath in decompressed) {
Expand All @@ -45,6 +90,18 @@
mkdirSync(dir, { recursive: true });
}
writeFileSync(fullPath, data);

// Restore Unix file permissions if available
if (isUnix && fileAttributes.has(relativePath)) {
try {
const mode = fileAttributes.get(relativePath);
if (mode !== undefined) {
chmodSync(fullPath, mode);
}
} catch (error) {
// Silently ignore permission errors

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should something be logged out to have an audit that it failed?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that's a solid exercise for the future but this PR as it is already makes things clearly better, thank you!

}
}
}
}

Expand Down
78 changes: 78 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,83 @@
);
expect(originalFile2).toEqual(unpackedFile2);
});

it("should preserve executable file permissions after packing and unpacking", () => {
// Skip this test on Windows since it doesn't support Unix permissions
if (process.platform === "win32") {
return;
}

const tempExecDir = join(__dirname, "temp-exec-test");
const execPackedFilePath = join(__dirname, "test-exec-extension.dxt");
const execUnpackedDir = join(__dirname, "temp-exec-unpack-test");

try {
// Create a temporary directory with an executable file
fs.mkdirSync(tempExecDir, { recursive: true });
fs.writeFileSync(
join(tempExecDir, "manifest.json"),
JSON.stringify({
dxt_version: "1.0",
name: "Test Executable Extension",
version: "1.0.0",
description: "A test extension with executable files",
author: {
name: "DXT",
},
server: {
type: "node",
entry_point: "server/index.js",
mcp_config: {
command: "node",
},
},
}),
);

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Delete `········`

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Delete `········`

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Delete `········`

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Delete `········`

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Delete `········`

Check warning on line 207 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Delete `········`
// Create an executable script
const executableScript = join(tempExecDir, "run-script.sh");
fs.writeFileSync(executableScript, "#!/bin/bash\necho 'Hello from executable'");

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`

Check warning on line 210 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Replace `executableScript,·"#!/bin/bash\necho·'Hello·from·executable'"` with `⏎··········executableScript,⏎··········"#!/bin/bash\necho·'Hello·from·executable'",⏎········`
fs.chmodSync(executableScript, 0o755); // Make it executable

// Create a regular file for comparison
const regularFile = join(tempExecDir, "regular-file.txt");
fs.writeFileSync(regularFile, "regular content");
fs.chmodSync(regularFile, 0o644); // Regular file permissions

// Pack the extension
execSync(`node ${cliPath} pack ${tempExecDir} ${execPackedFilePath}`, {
encoding: "utf-8",
});

// Unpack the extension
execSync(`node ${cliPath} unpack ${execPackedFilePath} ${execUnpackedDir}`, {

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, macos-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, macos-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, ubuntu-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, ubuntu-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.19.x, windows-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`

Check warning on line 224 in test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.17.x, windows-latest)

Replace ``node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,` with `⏎··········`node·${cliPath}·unpack·${execPackedFilePath}·${execUnpackedDir}`,⏎·········`
encoding: "utf-8",
});

// Check that the executable file preserved its permissions
const originalStats = fs.statSync(executableScript);
const unpackedStats = fs.statSync(join(execUnpackedDir, "run-script.sh"));

// Check that executable permissions are preserved (0o755)
expect(unpackedStats.mode & 0o777).toBe(0o755);
expect(originalStats.mode & 0o777).toBe(unpackedStats.mode & 0o777);

// Check that regular file permissions are preserved (0o644)
const originalRegularStats = fs.statSync(regularFile);
const unpackedRegularStats = fs.statSync(join(execUnpackedDir, "regular-file.txt"));

expect(unpackedRegularStats.mode & 0o777).toBe(0o644);
expect(originalRegularStats.mode & 0o777).toBe(unpackedRegularStats.mode & 0o777);

} finally {
// Clean up
fs.rmSync(tempExecDir, { recursive: true, force: true });
fs.rmSync(execUnpackedDir, { recursive: true, force: true });
if (fs.existsSync(execPackedFilePath)) {
fs.unlinkSync(execPackedFilePath);
}
}
});
});
});