Skip to content

Commit

Permalink
Fixed provided form-selector version validation.
Browse files Browse the repository at this point in the history
dbd379ab25ad598222d9c2f8c8d4de92b7605174
  • Loading branch information
cvgalagan committed Aug 19, 2024
1 parent 61bea21 commit 342b8e1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/providers/remoteControl/__tests__/inline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe('remoteControl / inline', () => {

const windowStub = {
JSON,
/* eslint-disable no-restricted-globals */
isFinite,
isNaN,
/* eslint-enable no-restricted-globals */
} as unknown as Window;

beforeEach(() => {
Expand Down
31 changes: 21 additions & 10 deletions src/providers/remoteControl/__tests__/remoteControl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ describe('isAllowedOrigin', () => {
});

describe('getResourceUrl', () => {
// eslint-disable-next-line no-restricted-globals
const ctx = { isFinite, isNaN } as Window;
it('Only allowed langs', () => {
['ru', 'en', 'tr'].forEach((lang) => {
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang,
appVersion: '1.2.3',
fileId: 'button',
Expand All @@ -115,7 +117,7 @@ describe('getResourceUrl', () => {
);
});
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'de',
appVersion: '1.2.3',
fileId: 'button',
Expand All @@ -126,7 +128,7 @@ describe('getResourceUrl', () => {
it('Only allowed ids', () => {
['button', 'form'].forEach((fileId) => {
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '1.2.3',
fileId,
Expand All @@ -136,7 +138,7 @@ describe('getResourceUrl', () => {
);
});
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '1.2.3',
fileId: '',
Expand All @@ -146,7 +148,7 @@ describe('getResourceUrl', () => {

it('Validate version', () => {
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '11.22.33',
fileId: 'button',
Expand All @@ -155,7 +157,7 @@ describe('getResourceUrl', () => {
'https://yastatic.net/s3/metrika/11.22.33/form-selector/button_ru.js',
);
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '1684933',
fileId: 'button',
Expand All @@ -164,31 +166,40 @@ describe('getResourceUrl', () => {
'https://yastatic.net/s3/metrika/1684933/form-selector/button_ru.js',
);
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: 'invalidVer',
fileId: 'button',
}),
).to.eq('https://yastatic.net/s3/metrika/form-selector/button_ru.js');
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '1.a',
fileId: 'button',
}),
).to.eq('https://yastatic.net/s3/metrika/1/form-selector/button_ru.js');
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '/.//.',
fileId: 'button',
}),
).to.eq('https://yastatic.net/s3/metrika/form-selector/button_ru.js');
chai.expect(
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '0.0.0',
fileId: 'button',
}),
).to.eq(
'https://yastatic.net/s3/metrika/0.0.0/form-selector/button_ru.js',
);
});

it('Beta url', () => {
chai.expect(
remoteControl.getResourceUrl({
remoteControl.getResourceUrl(ctx, {
lang: 'ru',
appVersion: '1.2.3',
fileId: 'button',
Expand Down
21 changes: 10 additions & 11 deletions src/providers/remoteControl/remoteControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@ import {
firstArg,
ctxBindArgs,
call,
curry2,
} from 'src/utils/function';
import { getPath } from 'src/utils/object';
import { cEvent } from 'src/utils/events';
import { flags } from '@inject';
import {
arrayJoin,
cForEach,
cMap,
filterFalsy,
includes,
} from 'src/utils/array';
import { arrayJoin, cFilter, cForEach, cMap, includes } from 'src/utils/array';
import { closestButton, selectButtons } from 'src/utils/dom/button';
import { closestForm, getFormData, selectForms } from 'src/utils/dom/form';
import { checkStatusFn } from 'src/providers/statusCheck/statusCheckFn';
import { parseDecimalInt } from 'src/utils/number';
import { isNumber, parseDecimalInt } from 'src/utils/number';
import { AnyFunc } from 'src/utils/function/types';

/* eslint-disable camelcase */
Expand Down Expand Up @@ -164,7 +159,10 @@ const AVAILABLE_FILES = ['form', 'button', 'status'];
const BETA_URL = 'https://s3.mds.yandex.net/internal-metrika-betas';
const URL = 'https://yastatic.net/s3/metrika';

export const getResourceUrl = (message: InlineMessageProps): string => {
export const getResourceUrl = (
ctx: Window,
message: InlineMessageProps,
): string => {
const {
['lang']: lang = '',
['appVersion']: appVersion = '',
Expand All @@ -173,7 +171,8 @@ export const getResourceUrl = (message: InlineMessageProps): string => {
} = message;
const validVersion = arrayJoin(
SPLITTER,
filterFalsy(
cFilter(
curry2(isNumber)(ctx),
cMap(pipe(firstArg, parseDecimalInt), appVersion.split(SPLITTER)),
),
);
Expand Down Expand Up @@ -255,7 +254,7 @@ export const handleMessage = memo(
const args = [ctx, event, message];
cForEach(pipe(ctxBindArgs(args), call), REMOTE_CONTROL_LISTENERS);
if (message['inline']) {
const src = getResourceUrl(message);
const src = getResourceUrl(ctx, message);
const { id = '' } = message;
setupUtilsAndLoadScript(ctx, src, id);
} else if (
Expand Down
2 changes: 1 addition & 1 deletion src/providers/statusCheck/__tests__/statusCheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('CHECK_STATUS_FEATURE', () => {
const [, callback, time] = setDefer.getCall(0).args;
chai.expect(time).to.equal(0);
callback();
sinon.assert.calledOnceWithExactly(getResourceUrlStub, {
sinon.assert.calledOnceWithExactly(getResourceUrlStub, ctx, {
['lang']: 'ru',
['fileId']: 'status',
});
Expand Down
2 changes: 1 addition & 1 deletion src/providers/statusCheck/statusCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const checkStatusRaw = (ctx: Window, counterOptions: CounterOptions) => {
counterOptions.id === id &&
counterOptions.counterType === DEFAULT_COUNTER_TYPE
) {
const src = getResourceUrl({
const src = getResourceUrl(ctx, {
['lang']: langForCheck(ctx),
['fileId']: 'status',
});
Expand Down

0 comments on commit 342b8e1

Please sign in to comment.