Skip to content

Commit

Permalink
Adds socket timeout and validate file size
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Hadka authored and Aiqiao Yan committed May 11, 2020
1 parent 9fe7ad8 commit 5a0add1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/cacheHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
IRequestOptions,
ITypedResponse
} from "@actions/http-client/interfaces";

import { SocketTimeout } from "./constants";
import {
ArtifactCacheEntry,
CommitCacheRequest,
Expand Down Expand Up @@ -123,7 +125,33 @@ export async function downloadCache(
const stream = fs.createWriteStream(archivePath);
const httpClient = new HttpClient("actions/cache");
const downloadResponse = await httpClient.get(archiveLocation);

// Abort download if no traffic received over the socket.
downloadResponse.message.socket.setTimeout(SocketTimeout, () => {
downloadResponse.message.destroy();
core.debug(
`Aborting download, socket timed out after ${SocketTimeout} ms`
);
});

await pipeResponseToStream(downloadResponse, stream);

// Validate download size.
var contentLengthHeader =
downloadResponse.message.headers["content-length"];

if (contentLengthHeader) {
const expectedLength = parseInt(contentLengthHeader);
const actualLength = utils.getArchiveFileSize(archivePath);

if (actualLength != expectedLength) {
throw new Error(
`Incomplete download. Expected file size: ${expectedLength}, actual file size: ${actualLength}`
);
}
} else {
core.debug("Unable to validate download, no Content-Length header");
}
}

// Reserve Cache
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export enum Events {
Push = "push",
PullRequest = "pull_request"
}

export const SocketTimeout = 5000;

0 comments on commit 5a0add1

Please sign in to comment.