After adding the fonctionality for apk publish7
This commit is contained in:
parent
bce8fd15d9
commit
e9b8918ea2
@ -25,12 +25,16 @@ export async function scanUploadedFile(filePath: string): Promise<void> {
|
||||
|
||||
function scanWithClamd(filePath: string, host: string, port: number): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
const socket = net.createConnection({ host, port });
|
||||
let response = '';
|
||||
let streamEnded = false;
|
||||
let socketEnded = false;
|
||||
|
||||
const finish = (err?: Error) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
socket.destroy();
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
};
|
||||
|
||||
@ -22,6 +22,8 @@ export type ApkParsedMeta = {
|
||||
const APK_SIG_BLOCK_MAGIC = Buffer.from('APK Sig Block 42', 'utf8');
|
||||
const APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a;
|
||||
const APK_SIGNATURE_SCHEME_V3_BLOCK_ID = 0xf05368c0;
|
||||
/** Enough for EOCD + typical APK Signing Block without loading the whole APK. */
|
||||
const APK_TAIL_READ_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
/** True when file starts with ZIP/APK magic bytes. */
|
||||
export function assertApkZipMagic(filePath: string): void {
|
||||
@ -46,24 +48,38 @@ function readU32LE(buf: Buffer, offset: number): number {
|
||||
}
|
||||
|
||||
function readU64LEAsNumber(buf: Buffer, offset: number): number {
|
||||
// APK sizes we care about fit in JS safe integers.
|
||||
const lo = buf.readUInt32LE(offset);
|
||||
const hi = buf.readUInt32LE(offset + 4);
|
||||
return hi * 0x100000000 + lo;
|
||||
}
|
||||
|
||||
/** Read the last N bytes of a file (for EOCD / APK Signing Block). */
|
||||
function readFileTail(filePath: string, maxBytes: number): { tail: Buffer; fileSize: number; tailStart: number } {
|
||||
const fileSize = fs.statSync(filePath).size;
|
||||
const readSize = Math.min(fileSize, maxBytes);
|
||||
const tailStart = fileSize - readSize;
|
||||
const tail = Buffer.alloc(readSize);
|
||||
const fd = fs.openSync(filePath, 'r');
|
||||
try {
|
||||
fs.readSync(fd, tail, 0, readSize, tailStart);
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
return { tail, fileSize, tailStart };
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate ZIP End of Central Directory and return central-directory offset.
|
||||
* Locate ZIP End of Central Directory inside a file tail and return absolute CD offset.
|
||||
*/
|
||||
function findCentralDirectoryOffset(fileBuf: Buffer): number {
|
||||
function findCentralDirectoryOffsetInTail(tail: Buffer, tailStart: number, fileSize: number): number {
|
||||
const maxComment = 0xffff;
|
||||
const minEocd = 22;
|
||||
const start = Math.max(0, fileBuf.length - (minEocd + maxComment));
|
||||
for (let i = fileBuf.length - minEocd; i >= start; i--) {
|
||||
if (fileBuf[i] === 0x50 && fileBuf[i + 1] === 0x4b && fileBuf[i + 2] === 0x05 && fileBuf[i + 3] === 0x06) {
|
||||
const commentLen = fileBuf.readUInt16LE(i + 20);
|
||||
if (i + minEocd + commentLen === fileBuf.length) {
|
||||
return fileBuf.readUInt32LE(i + 16);
|
||||
const start = Math.max(0, tail.length - (minEocd + maxComment));
|
||||
for (let i = tail.length - minEocd; i >= start; i--) {
|
||||
if (tail[i] === 0x50 && tail[i + 1] === 0x4b && tail[i + 2] === 0x05 && tail[i + 3] === 0x06) {
|
||||
const commentLen = tail.readUInt16LE(i + 20);
|
||||
if (tailStart + i + minEocd + commentLen === fileSize) {
|
||||
return tail.readUInt32LE(i + 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -72,8 +88,6 @@ function findCentralDirectoryOffset(fileBuf: Buffer): number {
|
||||
|
||||
/**
|
||||
* Extract first X.509 certificate DER from an APK Signing Block v2 or v3 value.
|
||||
* Layout: length-prefixed signers → each has length-prefixed signed-data →
|
||||
* digests then certificates (length-prefixed sequence of length-prefixed DER certs).
|
||||
*/
|
||||
function firstCertDerFromV2orV3Block(blockValue: Buffer): Buffer | null {
|
||||
if (blockValue.length < 4) return null;
|
||||
@ -114,46 +128,58 @@ function firstCertDerFromV2orV3Block(blockValue: Buffer): Buffer | null {
|
||||
}
|
||||
|
||||
/**
|
||||
* Read signing cert from APK Signature Scheme v2/v3 block (before ZIP central directory).
|
||||
* Read signing cert from APK Signature Scheme v2/v3 using only a file tail (no full APK load).
|
||||
*/
|
||||
export function extractSigningCertSha256FromApkSigningBlock(filePath: string): string | null {
|
||||
const fileBuf = fs.readFileSync(filePath);
|
||||
let cdOffset: number;
|
||||
let tail: Buffer;
|
||||
let fileSize: number;
|
||||
let tailStart: number;
|
||||
try {
|
||||
cdOffset = findCentralDirectoryOffset(fileBuf);
|
||||
({ tail, fileSize, tailStart } = readFileTail(filePath, APK_TAIL_READ_BYTES));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (cdOffset < 32) return null;
|
||||
|
||||
const magicOffset = cdOffset - 16;
|
||||
if (!fileBuf.subarray(magicOffset, cdOffset).equals(APK_SIG_BLOCK_MAGIC)) return null;
|
||||
let cdOffset: number;
|
||||
try {
|
||||
cdOffset = findCentralDirectoryOffsetInTail(tail, tailStart, fileSize);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (cdOffset < 32 || cdOffset > fileSize) return null;
|
||||
|
||||
const blockSizeFooter = readU64LEAsNumber(fileBuf, cdOffset - 24);
|
||||
// Signing block must sit entirely inside the tail we loaded.
|
||||
if (cdOffset < tailStart) return null;
|
||||
|
||||
const cdInTail = cdOffset - tailStart;
|
||||
const magicOffset = cdInTail - 16;
|
||||
if (magicOffset < 0) return null;
|
||||
if (!tail.subarray(magicOffset, cdInTail).equals(APK_SIG_BLOCK_MAGIC)) return null;
|
||||
|
||||
const blockSizeFooter = readU64LEAsNumber(tail, cdInTail - 24);
|
||||
if (blockSizeFooter < 24) return null;
|
||||
// Header size field sits immediately before the pairs; total block = 8 + blockSizeFooter.
|
||||
const blockStart = cdOffset - blockSizeFooter - 8;
|
||||
if (blockStart < 0) return null;
|
||||
const blockSizeHeader = readU64LEAsNumber(fileBuf, blockStart);
|
||||
const blockStartAbs = cdOffset - blockSizeFooter - 8;
|
||||
if (blockStartAbs < tailStart) return null;
|
||||
const blockStart = blockStartAbs - tailStart;
|
||||
const blockSizeHeader = readU64LEAsNumber(tail, blockStart);
|
||||
if (blockSizeHeader !== blockSizeFooter) return null;
|
||||
|
||||
let pairsOffset = blockStart + 8;
|
||||
const pairsEnd = cdOffset - 24;
|
||||
const pairsEnd = cdInTail - 24;
|
||||
let v2Value: Buffer | null = null;
|
||||
let v3Value: Buffer | null = null;
|
||||
|
||||
while (pairsOffset + 12 <= pairsEnd) {
|
||||
const pairLen = readU64LEAsNumber(fileBuf, pairsOffset);
|
||||
const pairLen = readU64LEAsNumber(tail, pairsOffset);
|
||||
pairsOffset += 8;
|
||||
if (pairLen < 4 || pairsOffset + pairLen > pairsEnd) break;
|
||||
const id = readU32LE(fileBuf, pairsOffset);
|
||||
const value = fileBuf.subarray(pairsOffset + 4, pairsOffset + pairLen);
|
||||
const id = readU32LE(tail, pairsOffset);
|
||||
const value = tail.subarray(pairsOffset + 4, pairsOffset + pairLen);
|
||||
pairsOffset += pairLen;
|
||||
if (id === APK_SIGNATURE_SCHEME_V2_BLOCK_ID) v2Value = Buffer.from(value);
|
||||
if (id === APK_SIGNATURE_SCHEME_V3_BLOCK_ID) v3Value = Buffer.from(value);
|
||||
}
|
||||
|
||||
// Prefer v3 then v2 (same first signer cert in normal dual-signed APKs).
|
||||
for (const value of [v3Value, v2Value]) {
|
||||
if (!value) continue;
|
||||
const der = firstCertDerFromV2orV3Block(value);
|
||||
@ -162,10 +188,8 @@ export function extractSigningCertSha256FromApkSigningBlock(filePath: string): s
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract SHA-256 of the first signing certificate (JAR v1 META-INF, then APK v2/v3).
|
||||
*/
|
||||
export function extractSigningCertSha256(filePath: string): string | null {
|
||||
/** JAR v1 META-INF/*.RSA|DSA|EC — loads zip central directory via AdmZip (heavier). */
|
||||
function extractSigningCertSha256FromJar(filePath: string): string | null {
|
||||
try {
|
||||
const zip = new AdmZip(filePath);
|
||||
const entries = zip.getEntries().filter((e) => {
|
||||
@ -196,10 +220,18 @@ export function extractSigningCertSha256(filePath: string): string | null {
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// fall through to v2/v3
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return extractSigningCertSha256FromApkSigningBlock(filePath);
|
||||
/**
|
||||
* Extract SHA-256 of the first signing certificate (APK v2/v3 first, then JAR v1).
|
||||
*/
|
||||
export function extractSigningCertSha256(filePath: string): string | null {
|
||||
return (
|
||||
extractSigningCertSha256FromApkSigningBlock(filePath) || extractSigningCertSha256FromJar(filePath)
|
||||
);
|
||||
}
|
||||
|
||||
/** Parse packageName / versionCode / versionName and signing cert fingerprint. */
|
||||
|
||||
@ -10,6 +10,21 @@ server {
|
||||
|
||||
client_max_body_size 200m;
|
||||
|
||||
location ~ ^/api/v1/admin/mobile-releases/[^/]+/apk$ {
|
||||
proxy_pass http://crypto_wallet_api:4000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 200m;
|
||||
client_body_timeout 1800s;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 1800s;
|
||||
proxy_read_timeout 1800s;
|
||||
proxy_request_buffering off;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://crypto_wallet_api:4000/api/;
|
||||
proxy_http_version 1.1;
|
||||
@ -17,6 +32,9 @@ server {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
location = /health {
|
||||
|
||||
@ -12,6 +12,23 @@ server {
|
||||
root /var/www/certbot;
|
||||
}
|
||||
|
||||
# Large APK uploads: long body/proxy timeouts, stream to API (no full nginx buffer).
|
||||
location ~ ^/api/v1/admin/mobile-releases/[^/]+/apk$ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 200m;
|
||||
client_body_timeout 1800s;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 1800s;
|
||||
proxy_read_timeout 1800s;
|
||||
proxy_request_buffering off;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
@ -20,6 +37,9 @@ server {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
location = /health {
|
||||
|
||||
@ -27,6 +27,22 @@ server {
|
||||
|
||||
client_max_body_size 200m;
|
||||
|
||||
location ~ ^/api/v1/admin/mobile-releases/[^/]+/apk$ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 200m;
|
||||
client_body_timeout 1800s;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 1800s;
|
||||
proxy_read_timeout 1800s;
|
||||
proxy_request_buffering off;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
@ -35,6 +51,9 @@ server {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
location = /health {
|
||||
|
||||
@ -25,6 +25,22 @@ server {
|
||||
|
||||
client_max_body_size 200m;
|
||||
|
||||
location ~ ^/api/v1/admin/mobile-releases/[^/]+/apk$ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 200m;
|
||||
client_body_timeout 1800s;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 1800s;
|
||||
proxy_read_timeout 1800s;
|
||||
proxy_request_buffering off;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
set $backend_api backend;
|
||||
proxy_pass http://$backend_api:4000;
|
||||
@ -33,6 +49,9 @@ server {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
location = /health {
|
||||
|
||||
@ -122,7 +122,7 @@ export async function uploadMobileApk(
|
||||
form.append('file', file);
|
||||
const startedAt = Date.now();
|
||||
const { data } = await api.post<MobileAppRelease>(`/api/v1/admin/mobile-releases/${id}/apk`, form, {
|
||||
timeout: 600_000,
|
||||
timeout: 1_800_000,
|
||||
maxBodyLength: Infinity,
|
||||
maxContentLength: Infinity,
|
||||
onUploadProgress: (evt) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user