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

Escape component keys used in reactid #714

Merged
merged 1 commit into from
Dec 28, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/__tests__/ReactIdentity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ describe('ReactIdentity', function() {
});

it('should not allow scripts in keys to execute', function() {
var h4x0rKey = '"><script>window.YOUVEBEENH4X0RED=true;</script><div id="';
var h4x0rKey =
'"><script>window[\'YOUVEBEENH4X0RED\']=true;</script><div id="';

var attachedContainer = document.createElement('div');
document.body.appendChild(attachedContainer);
Expand Down
5 changes: 4 additions & 1 deletion src/utils/__tests__/ReactChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ describe('ReactChildren', function() {
var mappedForcedKeys = Object.keys(mappedChildrenForcedKeys);
expect(mappedForcedKeys).toEqual(expectedForcedKeys);

var expectedRemappedForcedKeys = ['{{keyZero}}{giraffe}', '{{keyOne}}[0]'];
var expectedRemappedForcedKeys = [
'{{keyZero^C}{giraffe}',
'{{keyOne^C}[0]'
];
var remappedChildrenForcedKeys =
ReactChildren.map(mappedChildrenForcedKeys, mapFn);
expect(
Expand Down
27 changes: 26 additions & 1 deletion src/utils/traverseAllChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ var invariant = require('invariant');
* });
*/

var userProvidedKeyEscaperLookup = {
'^': '^X',
'.': '^D',
'}': '^C'
};

var userProvidedKeyEscapeRegex = /[.^}]/g;

function userProvidedKeyEscaper(match) {
return userProvidedKeyEscaperLookup[match];
}

/**
* Generate a key string that identifies a component within a set.
*
Expand All @@ -46,6 +58,19 @@ function getComponentKey(component, index) {
return '[' + index + ']';
}

/**
* Escape a component key so that it is safe to use in a reactid.
*
* @param {*} key Component key to be escaped.
* @return {string} An escaped string.
*/
function escapeUserProvidedKey(text) {
return ('' + text).replace(
userProvidedKeyEscapeRegex,
userProvidedKeyEscaper
);
}

/**
* Wrap a `key` value explicitly provided by the user to distinguish it from
* implicitly-generated keys generated by a component's index in its parent.
Expand All @@ -54,7 +79,7 @@ function getComponentKey(component, index) {
* @return {string}
*/
function wrapUserProvidedKey(key) {
return '{' + key + '}';
return '{' + escapeUserProvidedKey(key) + '}';
}

/**
Expand Down