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

fix(http-request): suppress failure upon size check #1075

Merged
merged 1 commit into from
May 8, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ type THeaderEntry = {

type THeaderEntries = THeaderEntry[];

function isPossiblyNotTextBased(contentType: string) {
function isPossiblyTextBased(contentType: string) {
if (!contentType) return false;

return contentType.startsWith('application/json')
|| contentType.startsWith('text/');
}

function throwIfFileSizeExceedsLimit(contentLength: string) {
const maxFileSize = 25 * 1024 * 1024; // 25MB

if (Number(contentLength) > maxFileSize) {
throw new Error(
`Response is too large. Maximum size is 25MB. Actual size is ${contentLength}`
);
}
}

export default defineAction({
name: 'Custom Request',
key: 'customRequest',
Expand Down Expand Up @@ -87,35 +99,50 @@ export default defineAction({
const data = $.step.parameters.data as string;
const url = $.step.parameters.url as string;
const headers = $.step.parameters.headers as THeaderEntries;
const maxFileSize = 25 * 1024 * 1024; // 25MB

const headersObject = headers.reduce((result, entry) => ({ ...result, [entry.key]: entry.value }), {})
const headersObject: Record<string, string> = headers.reduce((result, entry) => {
const key = entry.key?.toLowerCase();
const value = entry.value;

if (key && value) {
return {
...result,
[entry.key?.toLowerCase()]: entry.value
}
}

const metadataResponse = await $.http.head(url, { headers: headersObject });
return result;
}, {});

if (Number(metadataResponse.headers['content-length']) > maxFileSize) {
throw new Error(
`Response is too large. Maximum size is 25MB. Actual size is ${metadataResponse.headers['content-length']}`
);
}
let contentType = headersObject['content-type'];

// in case HEAD request is not supported by the URL
try {
const metadataResponse = await $.http.head(url, { headers: headersObject });
contentType = metadataResponse.headers['content-type'];

throwIfFileSizeExceedsLimit(metadataResponse.headers['content-length']);
// eslint-disable-next-line no-empty
} catch { }

const contentType = metadataResponse.headers['content-type'];
const requestData: AxiosRequestConfig = {
url,
method,
data,
headers: headersObject,
};

if (!isPossiblyNotTextBased(contentType)) {
if (!isPossiblyTextBased(contentType)) {
requestData.responseType = 'arraybuffer';
}

const response = await $.http.request(requestData);

throwIfFileSizeExceedsLimit(response.headers['content-length']);

let responseData = response.data;

if (!isPossiblyNotTextBased(contentType)) {
if (!isPossiblyTextBased(contentType)) {
responseData = Buffer.from(responseData as string).toString('base64');
}

Expand Down