Skip to content
Open
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
58 changes: 58 additions & 0 deletions cypress/e2e/secure-option-values.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe('Secure option values', () => {
const existingValueMountId = 'vs-secure-existing-value';
const newValueMountId = 'vs-secure-new-value';

it('preserves the original value in the hidden form input', () => {
cy.visit('get-started');

cy.window().then((win) => {
const $ele = win.document.createElement('div');
$ele.id = existingValueMountId;
win.document.body.appendChild($ele);

// @ts-expect-error - VirtualSelect is attached to window by the bundle
win.VirtualSelect.init({
ele: $ele,
enableSecureText: true,
multiple: true,
setValueAsArray: true,
options: [{ value: '<', label: 'Value with unsafe char <' }],
});
});

cy.get(`#${existingValueMountId}`).find('.vscomp-toggle-button').click();
cy.get(`#${existingValueMountId}`).find('.vscomp-option').click();

cy.get(`#${existingValueMountId}`).find('.vscomp-hidden-input').should('have.value', '["<"]');
cy.get(`#${existingValueMountId}`).find('.vscomp-option-text').should('contain.text', 'Value with unsafe char <');
});

it('preserves new values without inserting their markup', () => {
const payload = '"><img src=x onerror="window.__vsValueXss=true">';

cy.visit('get-started');

cy.window().then((win) => {
const $ele = win.document.createElement('div');
$ele.id = newValueMountId;
win.document.body.appendChild($ele);

// @ts-expect-error - VirtualSelect is attached to window by the bundle
win.VirtualSelect.init({
ele: $ele,
allowNewOption: true,
enableSecureText: true,
multiple: true,
setValueAsArray: true,
options: [],
});

// @ts-expect-error - setValue is attached to the initialized element
$ele.setValue([payload]);
});

cy.get(`#${newValueMountId}`).find('.vscomp-hidden-input').should('have.value', JSON.stringify([payload]));
cy.get(`#${newValueMountId}`).find('img').should('not.exist');
cy.window().its('__vsValueXss').should('be.undefined');
});
});
2 changes: 1 addition & 1 deletion dist-archive/virtual-select-1.3.0.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/virtual-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ class VirtualSelect {
}
}
html += `<div role="option" aria-selected="${isSelected}" id="vscomp-option-${uniqueId}-${index}"
class="${optionClasses}" data-value="${d.value}" data-index="${index}" data-visible-index="${d.visibleIndex}"
class="${optionClasses}" data-value="${this.secureText(d.value)}" data-index="${index}" data-visible-index="${d.visibleIndex}"
tabindex=${tabIndexValue} ${groupIndexText} ${ariaDisabledText} ${ariaLabel} ${ariaAttrs}
>
${leftSection}
Expand Down Expand Up @@ -1979,7 +1979,7 @@ class VirtualSelect {
[labelKey]: d
};
}
const value = secureText(getString(d[valueKey]));
const value = getString(d[valueKey]);
const label = secureText(getString(d[labelKey]));
const childOptions = d.options;
const isGroupTitle = !!childOptions;
Expand Down Expand Up @@ -2468,7 +2468,7 @@ class VirtualSelect {
const newOption = this.getNewOption();
if (newOption) {
const newIndex = newOption.index;
this.setOptionProp(newIndex, 'value', this.secureText(value));
this.setOptionProp(newIndex, 'value', value);
this.setOptionProp(newIndex, 'label', this.secureText(value));
} else {
const data = {
Expand Down Expand Up @@ -2697,7 +2697,7 @@ class VirtualSelect {
getString
} = Utils;
const secureText = this.secureText.bind(this);
const value = secureText(getString(data.value));
const value = getString(data.value);
const label = secureText(getString(data.label));
const description = secureText(getString(data.description));
return {
Expand Down
2 changes: 1 addition & 1 deletion dist/virtual-select.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/assets/virtual-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ class VirtualSelect {
}
}
html += `<div role="option" aria-selected="${isSelected}" id="vscomp-option-${uniqueId}-${index}"
class="${optionClasses}" data-value="${d.value}" data-index="${index}" data-visible-index="${d.visibleIndex}"
class="${optionClasses}" data-value="${this.secureText(d.value)}" data-index="${index}" data-visible-index="${d.visibleIndex}"
tabindex=${tabIndexValue} ${groupIndexText} ${ariaDisabledText} ${ariaLabel} ${ariaAttrs}
>
${leftSection}
Expand Down Expand Up @@ -1979,7 +1979,7 @@ class VirtualSelect {
[labelKey]: d
};
}
const value = secureText(getString(d[valueKey]));
const value = getString(d[valueKey]);
const label = secureText(getString(d[labelKey]));
const childOptions = d.options;
const isGroupTitle = !!childOptions;
Expand Down Expand Up @@ -2468,7 +2468,7 @@ class VirtualSelect {
const newOption = this.getNewOption();
if (newOption) {
const newIndex = newOption.index;
this.setOptionProp(newIndex, 'value', this.secureText(value));
this.setOptionProp(newIndex, 'value', value);
this.setOptionProp(newIndex, 'label', this.secureText(value));
} else {
const data = {
Expand Down Expand Up @@ -2697,7 +2697,7 @@ class VirtualSelect {
getString
} = Utils;
const secureText = this.secureText.bind(this);
const value = secureText(getString(data.value));
const value = getString(data.value);
const label = secureText(getString(data.label));
const description = secureText(getString(data.description));
return {
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/virtual-select.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/virtual-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export class VirtualSelect {
}

html += `<div role="option" aria-selected="${isSelected}" id="vscomp-option-${uniqueId}-${index}"
class="${optionClasses}" data-value="${d.value}" data-index="${index}" data-visible-index="${d.visibleIndex}"
class="${optionClasses}" data-value="${this.secureText(d.value)}" data-index="${index}" data-visible-index="${d.visibleIndex}"
tabindex=${tabIndexValue} ${groupIndexText} ${ariaDisabledText} ${ariaLabel} ${ariaAttrs}
>
${leftSection}
Expand Down Expand Up @@ -1558,7 +1558,7 @@ export class VirtualSelect {
d = { [valueKey]: d, [labelKey]: d };
}

const value = secureText(getString(d[valueKey]));
const value = getString(d[valueKey]);
const label = secureText(getString(d[labelKey]));
const childOptions = d.options;
const isGroupTitle = !!childOptions;
Expand Down Expand Up @@ -2129,7 +2129,7 @@ export class VirtualSelect {
if (newOption) {
const newIndex = newOption.index;

this.setOptionProp(newIndex, 'value', this.secureText(value));
this.setOptionProp(newIndex, 'value', value);
this.setOptionProp(newIndex, 'label', this.secureText(value));
} else {
const data = {
Expand Down Expand Up @@ -2385,7 +2385,7 @@ export class VirtualSelect {

const { getString } = Utils;
const secureText = this.secureText.bind(this);
const value = secureText(getString(data.value));
const value = getString(data.value);
const label = secureText(getString(data.label));
const description = secureText(getString(data.description));

Expand Down