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(HTTP Request Tool Node): Relax binary data detection #13048

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
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,69 @@ describe('ToolHttpRequest', () => {
}),
);
});

it('should return the error when receiving text that contains a null character', async () => {
helpers.httpRequest.mockResolvedValue({
body: 'Hello\0World',
headers: {
'content-type': 'text/plain',
},
});

executeFunctions.getNodeParameter.mockImplementation((paramName: string) => {
switch (paramName) {
case 'method':
return 'GET';
case 'url':
return 'https://httpbin.org/text/plain';
case 'options':
return {};
case 'placeholderDefinitions.values':
return [];
default:
return undefined;
}
});

const { response } = await httpTool.supplyData.call(executeFunctions, 0);
const res = await (response as N8nTool).invoke({});
expect(helpers.httpRequest).toHaveBeenCalled();
// Check that the returned string is formatted as an error message.
expect(res).toContain('error');
expect(res).toContain('Binary data is not supported');
});

it('should return the error when receiving a JSON response containing a null character', async () => {
// Provide a raw JSON string with a literal null character.
helpers.httpRequest.mockResolvedValue({
body: '{"message":"hello\0world"}',
headers: {
'content-type': 'application/json',
},
});

executeFunctions.getNodeParameter.mockImplementation((paramName: string) => {
switch (paramName) {
case 'method':
return 'GET';
case 'url':
return 'https://httpbin.org/json';
case 'options':
return {};
case 'placeholderDefinitions.values':
return [];
default:
return undefined;
}
});

const { response } = await httpTool.supplyData.call(executeFunctions, 0);
const res = await (response as N8nTool).invoke({});
expect(helpers.httpRequest).toHaveBeenCalled();
// Check that the tool returns an error string rather than resolving to valid JSON.
expect(res).toContain('error');
expect(res).toContain('Binary data is not supported');
});
});

describe('Optimize response', () => {
Expand Down
29 changes: 21 additions & 8 deletions packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { JSDOM } from 'jsdom';
import get from 'lodash/get';
import set from 'lodash/set';
import unset from 'lodash/unset';
import * as mime from 'mime-types';
import { getOAuth2AdditionalParameters } from 'n8n-nodes-base/dist/nodes/HttpRequest/GenericFunctions';
import type {
IDataObject,
Expand Down Expand Up @@ -146,6 +145,25 @@ const defaultOptimizer = <T>(response: T) => {
return String(response);
};

function isBinary(data: unknown) {
// Check if data is a Buffer
if (Buffer.isBuffer(data)) {
return true;
}

// If data is a string, assume it's text unless it contains null characters.
if (typeof data === 'string') {
// If the string contains a null character, it's likely binary.
if (data.includes('\0')) {
return true;
}
return false;
}

// For any other type, assume it's not binary.
return false;
}

const htmlOptimizer = (ctx: ISupplyDataFunctions, itemIndex: number, maxLength: number) => {
const cssSelector = ctx.getNodeParameter('cssSelector', itemIndex, '') as string;
const onlyContent = ctx.getNodeParameter('onlyContent', itemIndex, false) as boolean;
Expand Down Expand Up @@ -755,13 +773,8 @@ export const configureToolFunction = (
if (!response) {
try {
// Check if the response is binary data
if (fullResponse?.headers?.['content-type']) {
const contentType = fullResponse.headers['content-type'] as string;
const mimeType = contentType.split(';')[0].trim();

if (mime.charset(mimeType) !== 'UTF-8') {
throw new NodeOperationError(ctx.getNode(), 'Binary data is not supported');
}
if (fullResponse.body && isBinary(fullResponse.body)) {
throw new NodeOperationError(ctx.getNode(), 'Binary data is not supported');
}

response = optimizeResponse(fullResponse.body);
Expand Down
Loading