Skip to content

Commit b57ace3

Browse files
authored
fix: Check gzip log via magic numbers (#1891)
* We are directly checking the bytes content of the log. If, for any reason, the content is not compressed, or is compressed but with wrong file extension, we are still going to properly decompress the file content. Closes #1887
1 parent 42470bf commit b57ace3

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

dashboard/src/api/logViewer.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,29 @@ const STALE_DURATION_MS = minutesToMilliseconds(60);
1616
type FetchAndDecompressLogsResponse = {
1717
content: string;
1818
};
19+
20+
const isGzipBytes = (data: Uint8Array): boolean =>
21+
// eslint-disable-next-line no-magic-numbers
22+
data.length >= 2 && data[0] === 0x1f && data[1] === 0x8b;
23+
1924
async function fetchAndDecompressLog(
2025
url: string,
2126
): Promise<FetchAndDecompressLogsResponse> {
2227
const proxyUrl = `/api/proxy/?url=${encodeURIComponent(url)}`;
23-
const urlPathname = new URL(url).pathname;
24-
const isGzipped = urlPathname.endsWith('.gz');
2528

2629
try {
27-
if (isGzipped) {
28-
const response = await RequestData.get<ArrayBuffer>(proxyUrl, {
29-
responseType: 'arraybuffer',
30-
});
31-
32-
const uint8ArrayResponse = new Uint8Array(response);
33-
const decompressedData = pako.inflate(uint8ArrayResponse);
34-
const textDecoder = new TextDecoder('utf-8');
35-
const decompressedText = textDecoder.decode(decompressedData);
36-
37-
return { content: decompressedText };
38-
} else {
39-
// For non-gzipped files, request as text
40-
const response = await RequestData.get<string>(proxyUrl, {
41-
responseType: 'text',
42-
});
43-
44-
return { content: response };
30+
const response = await RequestData.get<ArrayBuffer>(proxyUrl, {
31+
responseType: 'arraybuffer',
32+
});
33+
34+
let byteResponse = new Uint8Array(response);
35+
const textDecoder = new TextDecoder('utf-8');
36+
37+
while (isGzipBytes(byteResponse)) {
38+
byteResponse = pako.inflate(byteResponse);
4539
}
40+
41+
return { content: textDecoder.decode(byteResponse) };
4642
} catch (error) {
4743
console.error(error);
4844

0 commit comments

Comments
 (0)