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(url): handle special characters in connection params #2608

Merged
merged 2 commits into from
Aug 14, 2024
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
16 changes: 11 additions & 5 deletions packages/shared/lib/clients/oauth2.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AuthorizationCode } from 'simple-oauth2';
import connectionsManager from '../services/connection.service.js';
import type { ServiceResponse } from '../models/Generic.js';
import { LogActionEnum } from '../models/Telemetry.js';
import { interpolateString } from '../utils/utils.js';
import { interpolateString, encodeParameters } from '../utils/utils.js';
import Boom from '@hapi/boom';
import { NangoError } from '../utils/error.js';
import errorManager, { ErrorSourceEnum } from '../utils/error.manager.js';
Expand All @@ -18,10 +18,9 @@ export function getSimpleOAuth2ClientConfig(
connectionConfig: Record<string, string>
): Merge<ModuleOptions, { http: WreckHttpOptions }> {
const templateTokenUrl = typeof template.token_url === 'string' ? template.token_url : (template.token_url!['OAUTH2'] as string);
const strippedTokenUrl = templateTokenUrl.replace(/connectionConfig\./g, '');
const tokenUrl = new URL(interpolateString(strippedTokenUrl, connectionConfig));
const strippedAuthorizeUrl = template.authorization_url!.replace(/connectionConfig\./g, '');
const authorizeUrl = new URL(interpolateString(strippedAuthorizeUrl, connectionConfig));
const tokenUrl = makeUrl(templateTokenUrl, connectionConfig);
const authorizeUrl = makeUrl(template.authorization_url!, connectionConfig);

const headers = { 'User-Agent': 'Nango' };

const authConfig = template as ProviderTemplateOAuth2;
Expand Down Expand Up @@ -157,3 +156,10 @@ export async function getFreshOAuth2Credentials(
return { success: false, error, response: null };
}
}

function makeUrl(template: string, config: Record<string, any>): URL {
const cleanTemplate = template.replace(/connectionConfig\./g, '');
const encodedParams = encodeParameters(config);
const interpolatedUrl = interpolateString(cleanTemplate, encodedParams);
return new URL(interpolatedUrl);
}
4 changes: 4 additions & 0 deletions packages/shared/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@ export function getConnectionConfig(queryParams: any): Record<string, string> {
const arr = Object.entries(queryParams).filter(([, v]) => typeof v === 'string'); // Filter strings
return Object.fromEntries(arr) as Record<string, string>;
}

export function encodeParameters(params: Record<string, any>): Record<string, string> {
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, encodeURIComponent(String(value))]));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this === to new URLSearchParams(params).toString()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they are the same, but in our code, encodeParameters is used to prepare the connectionConfig object for interpolation into a URL. This is necessary because interpolateStringFromObject expects an object with pre-encoded values, ensuring that when these values are interpolated into a URL, they do not introduce any invalid characters or break the URL format.

}
25 changes: 25 additions & 0 deletions packages/shared/lib/utils/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,28 @@ describe('Proxy service Construct Header Tests', () => {
expect(utils.isValidHttpUrl('/api/v2/tickets?per_page=100&include=requester,description&page=3')).toBe(false);
});
});
describe('encodeParameters Function Tests', () => {
it('should encode parameters correctly', () => {
const params = {
redirectUri: 'https://redirectme.com'
};

const expected = {
redirectUri: 'https%3A%2F%2Fredirectme.com'
};

expect(utils.encodeParameters(params)).toEqual(expected);
});

it('should handle parameters with special characters', () => {
const params = {
redirectUri: 'https://redirectme.com?param=value&another=value'
};

const expected = {
redirectUri: 'https%3A%2F%2Fredirectme.com%3Fparam%3Dvalue%26another%3Dvalue'
};

expect(utils.encodeParameters(params)).toEqual(expected);
});
});
Loading