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: add support for signature verification #6

Merged
merged 1 commit into from
Jul 12, 2024
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
45 changes: 35 additions & 10 deletions src/commands/apps/bundles/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import appsService from '../../../services/apps';
import appBundlesService from '../../../services/app-bundles';
import { getMessageFromUnknownError } from '../../../utils/error';
import { createHash } from '../../../utils/hash';
import { createBuffer } from '../../../utils/buffer';
import { createBuffer, createBufferFromPath } from '../../../utils/buffer';
import { createSignature } from '../../../utils/signature';
import { fileExistsAtPath } from '../../../utils/file';

export default defineCommand({
meta: {
Expand Down Expand Up @@ -44,6 +46,10 @@ export default defineCommand({
type: 'string',
description: 'Path to the bundle to upload. Must be a folder (e.g. `www` or `dist`) or a zip file.',
},
privateKey: {
type: 'string',
description: 'The path to the private key file to sign the bundle with.',
},
rollout: {
type: 'string',
description: 'The percentage of devices to deploy the bundle to. Must be a number between 0 and 1 (e.g. 0.5).',
Expand All @@ -59,7 +65,7 @@ export default defineCommand({
return;
}

const { androidMax, androidMin, rollout, iosMax, iosMin } = ctx.args;
const { androidMax, androidMin, privateKey, rollout, iosMax, iosMin } = ctx.args;
let appId = ctx.args.appId;
let channelName = ctx.args.channel;
let path = ctx.args.path;
Expand Down Expand Up @@ -92,22 +98,41 @@ export default defineCommand({
}
}
}
let privateKeyBuffer;
if (privateKey) {
if (privateKey.endsWith('.pem')) {
const fileExists = await fileExistsAtPath(privateKey);
if (fileExists) {
privateKeyBuffer = await createBufferFromPath(privateKey);
} else {
consola.error('Private key file not found.');
return;
}
} else {
consola.error('Private key must be a path to a .pem file.');
return;
}
}

// Create form data
const formData = new FormData();
if (path) {
let fileBuffer;
if (zip.isZipped(path)) {
const readStream = createReadStream(path);
const buffer = await createBuffer(readStream);
const hash = await createHash(buffer);
formData.append('file', buffer, { filename: 'bundle.zip' });
formData.append('checksum', hash);
fileBuffer = await createBuffer(readStream);
} else {
consola.start('Zipping folder...');
const zipBuffer = await zip.zipFolder(path);
const hash = await createHash(zipBuffer);
formData.append('file', zipBuffer, { filename: 'bundle.zip' });
formData.append('checksum', hash);
fileBuffer = await zip.zipFolder(path);
}
consola.start('Generating checksum...');
const hash = await createHash(fileBuffer);
formData.append('file', fileBuffer, { filename: 'bundle.zip' });
formData.append('checksum', hash);
if (privateKeyBuffer) {
consola.start('Signing bundle...');
const signature = await createSignature(privateKeyBuffer, fileBuffer);
formData.append('signature', signature);
}
}
if (url) {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ export const createBuffer = async (data: ReadStream): Promise<Buffer> => {
data.on('error', reject);
});
};

export const createBufferFromPath = async (path: string): Promise<Buffer> => {
const fs = await import('fs');
const stream = fs.createReadStream(path);
return createBuffer(stream);
};
8 changes: 8 additions & 0 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const fileExistsAtPath = async (path: string): Promise<boolean> => {
const fs = await import('fs');
return new Promise((resolve) => {
fs.access(path, fs.constants.F_OK, (err) => {
resolve(!err);
});
});
};
7 changes: 7 additions & 0 deletions src/utils/signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const createSignature = async (privateKey: Buffer, data: Buffer): Promise<string> => {
const crypto = await import('crypto');
const sign = crypto.createSign('sha256');
sign.update(data);
sign.end();
return sign.sign(privateKey).toString('base64');
};
Loading