Skip to content

Commit

Permalink
Fix issue with invalid characters in SplitSDKMachineName header
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Nov 27, 2024
1 parent cd09ac7 commit 9afd29f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.0.2 (XXX XX, 2024)
- Bugfixing - Sanitize the `SplitSDKMachineName` header value to avoid exceptions on HTTP/S requests when it contains non ISO-8859-1 characters (Related to issue https://github.com/splitio/javascript-client/issues/847).

2.0.1 (November 25, 2024)
- Bugfixing - Fixed an issue with the SDK_UPDATE event on server-side, where it was not being emitted if there was an empty segment and the SDK received a feature flag update notification.

Expand Down
9 changes: 8 additions & 1 deletion src/services/__tests__/decorateHeaders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ISettings } from '../../types';
import { decorateHeaders } from '../decorateHeaders';
import { decorateHeaders, removeNonISO88591 } from '../decorateHeaders';

const HEADERS = {
Authorization: 'Bearer SDK-KEY',
Expand Down Expand Up @@ -48,3 +48,10 @@ describe('decorateHeaders', () => {
expect(settings.log.error).toHaveBeenCalledWith('Problem adding custom headers to request decorator: Error: Unexpected error');
});
});

test('removeNonISO88591', () => {
expect(removeNonISO88591('')).toBe('');
expect(removeNonISO88591('This is a test')).toBe('This is a test');
expect(removeNonISO88591('This is a test ó \u0FFF 你')).toBe('This is a test ó ');
expect(removeNonISO88591('Emiliano’s-MacBook-Pro')).toBe('Emilianos-MacBook-Pro');
});
5 changes: 5 additions & 0 deletions src/services/decorateHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export function decorateHeaders(settings: ISettings, headers: Record<string, str
}
return headers;
}

export function removeNonISO88591(input: string) {
// eslint-disable-next-line no-control-regex
return input.replace(/[^\x00-\xFF]/g, '');
}
4 changes: 2 additions & 2 deletions src/services/splitHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { objectAssign } from '../utils/lang/objectAssign';
import { ERROR_HTTP, ERROR_CLIENT_CANNOT_GET_READY } from '../logger/constants';
import { ISettings } from '../types';
import { IPlatform } from '../sdkFactory/types';
import { decorateHeaders } from './decorateHeaders';
import { decorateHeaders, removeNonISO88591 } from './decorateHeaders';

const messageNoFetch = 'Global fetch API is not available.';

Expand All @@ -30,7 +30,7 @@ export function splitHttpClientFactory(settings: ISettings, { getOptions, getFet
};

if (ip) commonHeaders['SplitSDKMachineIP'] = ip;
if (hostname) commonHeaders['SplitSDKMachineName'] = hostname;
if (hostname) commonHeaders['SplitSDKMachineName'] = removeNonISO88591(hostname);

return function httpClient(url: string, reqOpts: IRequestOptions = {}, latencyTracker: (error?: NetworkError) => void = () => { }, logErrorsAsInfo: boolean = false): Promise<IResponse> {

Expand Down

0 comments on commit 9afd29f

Please sign in to comment.