Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); [Flight] Add more DoS mitigations to Flight Reply, and harden Flight … · react/react@1068027 · GitHub
Skip to content

Commit 1068027

Browse files
unstubbablesebmarkbagegnofflubieowoceeps1lon
authored
[Flight] Add more DoS mitigations to Flight Reply, and harden Flight (#35632)
This fixes security vulnerabilities in Server Functions. --------- Co-authored-by: Sebastian Markbåge <sebastian@calyptus.eu> Co-authored-by: Josh Story <josh.c.story@gmail.com> Co-authored-by: Janka Uryga <lolzatu2@gmail.com> Co-authored-by: Sebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
1 parent 699abc8 commit 1068027

18 files changed

Lines changed: 835 additions & 263 deletions

File tree

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
9494

9595
import{getOwnerStackByComponentInfoInDev}from'shared/ReactComponentInfoStack';
9696

97+
importhasOwnPropertyfrom'shared/hasOwnProperty';
98+
9799
import{injectInternals}from'./ReactFlightClientDevToolsHook';
98100

99101
import{OMITTED_PROP_ERROR}from'shared/ReactFlightPropertyAccess';
@@ -159,6 +161,8 @@ const INITIALIZED = 'fulfilled';
159161
constERRORED='rejected';
160162
constHALTED='halted';// DEV-only. Means it never resolves even if connection closes.
161163

164+
const__PROTO__='__proto__';
165+
162166
typePendingChunk<T>={
163167
status: 'pending',
164168
value: null|Array<InitializationReference|(T=>mixed)>,
@@ -1544,7 +1548,16 @@ function fulfillReference(
15441548
}
15451549
}
15461550
}
1547-
value=value[path[i]];
1551+
constname=path[i];
1552+
if(
1553+
typeofvalue=== 'object' &&
1554+
value!==null&&
1555+
hasOwnProperty.call(value,name)
1556+
){
1557+
value=value[name];
1558+
} else {
1559+
thrownewError('Invalid reference.');
1560+
}
15481561
}
15491562

15501563
while(
@@ -1580,7 +1593,9 @@ function fulfillReference(
15801593
}
15811594

15821595
constmappedValue=map(response,value,parentObject,key);
1583-
parentObject[key]=mappedValue;
1596+
if(key!==__PROTO__){
1597+
parentObject[key]=mappedValue;
1598+
}
15841599

15851600
// If this is the root object for a model reference, where `handler.value`
15861601
// is a stale `null`, the resolved value can be used directly.
@@ -1849,7 +1864,9 @@ function loadServerReference<A: Iterable<any>, T>(
18491864
response._encodeFormAction,
18501865
);
18511866

1852-
parentObject[key]=resolvedValue;
1867+
if(key!==__PROTO__){
1868+
parentObject[key]=resolvedValue;
1869+
}
18531870

18541871
// If this is the root object for a model reference, where `handler.value`
18551872
// is a stale `null`, the resolved value can be used directly.
@@ -2231,29 +2248,31 @@ function defineLazyGetter<T>(
22312248
): any {
22322249
// We don't immediately initialize it even if it's resolved.
22332250
// Instead, we wait for the getter to get accessed.
2234-
Object.defineProperty(parentObject,key,{
2235-
get: function(){
2236-
if(chunk.status===RESOLVED_MODEL){
2237-
// If it was now resolved, then we initialize it. This may then discover
2238-
// a new set of lazy references that are then asked for eagerly in case
2239-
// we get that deep.
2240-
initializeModelChunk(chunk);
2241-
}
2242-
switch(chunk.status){
2243-
caseINITIALIZED: {
2244-
returnchunk.value;
2251+
if(key!==__PROTO__){
2252+
Object.defineProperty(parentObject,key,{
2253+
get: function(){
2254+
if(chunk.status===RESOLVED_MODEL){
2255+
// If it was now resolved, then we initialize it. This may then discover
2256+
// a new set of lazy references that are then asked for eagerly in case
2257+
// we get that deep.
2258+
initializeModelChunk(chunk);
22452259
}
2246-
caseERRORED:
2247-
throwchunk.reason;
2248-
}
2249-
// Otherwise, we didn't have enough time to load the object before it was
2250-
// accessed or the connection closed. So we just log that it was omitted.
2251-
// TODO: We should ideally throw here to indicate a difference.
2252-
returnOMITTED_PROP_ERROR;
2253-
},
2254-
enumerable: true,
2255-
configurable: false,
2256-
});
2260+
switch(chunk.status){
2261+
caseINITIALIZED: {
2262+
returnchunk.value;
2263+
}
2264+
caseERRORED:
2265+
throwchunk.reason;
2266+
}
2267+
// Otherwise, we didn't have enough time to load the object before it was
2268+
// accessed or the connection closed. So we just log that it was omitted.
2269+
// TODO: We should ideally throw here to indicate a difference.
2270+
returnOMITTED_PROP_ERROR;
2271+
},
2272+
enumerable: true,
2273+
configurable: false,
2274+
});
2275+
}
22572276
return null;
22582277
}
22592278

@@ -2564,14 +2583,16 @@ function parseModelString(
25642583
// In DEV mode we encode omitted objects in logs as a getter that throws
25652584
// so that when you try to access it on the client, you know why that
25662585
// happened.
2567-
Object.defineProperty(parentObject,key,{
2568-
get: function(){
2569-
// TODO: We should ideally throw here to indicate a difference.
2570-
returnOMITTED_PROP_ERROR;
2571-
},
2572-
enumerable: true,
2573-
configurable: false,
2574-
});
2586+
if(key!==__PROTO__){
2587+
Object.defineProperty(parentObject,key,{
2588+
get: function(){
2589+
// TODO: We should ideally throw here to indicate a difference.
2590+
returnOMITTED_PROP_ERROR;
2591+
},
2592+
enumerable: true,
2593+
configurable: false,
2594+
});
2595+
}
25752596
returnnull;
25762597
}
25772598
// Fallthrough
@@ -5183,6 +5204,9 @@ function parseModel<T>(response: Response, json: UninitializedModel): T {
51835204
function createFromJSONCallback(response: Response) {
51845205
// $FlowFixMe[missing-this-annot]
51855206
returnfunction(key: string,value: JSONValue){
5207+
if(key===__PROTO__){
5208+
returnundefined;
5209+
}
51865210
if(typeofvalue==='string'){
51875211
// We can't use .bind here because we need the "this" value.
51885212
returnparseModelString(response,this,key,value);

‎packages/react-client/src/ReactFlightReplyClient.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export type ReactServerValue =
9595

9696
type ReactServerObject = {+[key: string]: ReactServerValue};
9797

98+
const __PROTO__ = '__proto__';
99+
98100
function serializeByValueID(id: number): string {
99101
return'$'+id.toString(16);
100102
}
@@ -361,6 +363,15 @@ export function processReply(
361363
): ReactJSONValue {
362364
constparent=this;
363365

366+
if(__DEV__){
367+
if(key===__PROTO__){
368+
console.error(
369+
'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370+
describeObjectForErrorMessage(parent,key),
371+
);
372+
}
373+
}
374+
364375
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
365376
if(__DEV__){
366377
// $FlowFixMe[incompatible-use]
@@ -780,6 +791,10 @@ export function processReply(
780791
if (typeof value === 'function') {
781792
constreferenceClosure=knownServerReferences.get(value);
782793
if(referenceClosure!==undefined){
794+
const existingReference =writtenObjects.get(value);
795+
if(existingReference!==undefined){
796+
return existingReference;
797+
}
783798
const{id, bound}=referenceClosure;
784799
constreferenceClosureJSON=JSON.stringify({id, bound},resolveToJSON);
785800
if(formData===null){
@@ -789,7 +804,10 @@ export function processReply(
789804
// The reference to this function came from the same client so we can pass it back.
790805
constrefId=nextPartId++;
791806
formData.set(formFieldPrefix+refId,referenceClosureJSON);
792-
returnserializeServerReferenceID(refId);
807+
constserverReferenceId=serializeServerReferenceID(refId);
808+
// Store the server reference ID for deduplication.
809+
writtenObjects.set(value,serverReferenceId);
810+
returnserverReferenceId;
793811
}
794812
if (temporaryReferences !== undefined &&key.indexOf(':')===-1){
795813
// TODO: If the property name contains a colon, we don't dedupe. Escape instead.

‎packages/react-client/src/forks/ReactFlightClientConfig.markup.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveClientReference<T>(
4343

4444
exportfunctionresolveServerReference<T>(
4545
config: ServerManifest,
46-
id: ServerReferenceId,
46+
id: mixed,
4747
): ClientReference<T>{
4848
thrownewError(
4949
'renderToHTML should not have emitted Server References. This is a bug in React.',

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ function prerenderToNodeStream(
328328
functiondecodeReplyFromBusboy<T>(
329329
busboyStream: Busboy,
330330
moduleBasePath: ServerManifest,
331-
options?: {temporaryReferences?: TemporaryReferenceSet},
331+
options?: {
332+
temporaryReferences?: TemporaryReferenceSet,
333+
arraySizeLimit?: number,
334+
},
332335
): Thenable<T>{
333336
const response =createResponse(
334337
moduleBasePath,
335338
'',
336339
options ? options.temporaryReferences : undefined,
340+
undefined,
341+
options ? options.arraySizeLimit : undefined,
337342
);
338343
letpendingFiles=0;
339344
constqueuedFields: Array<string>=[];
@@ -399,7 +404,10 @@ function decodeReplyFromBusboy<T>(
399404
functiondecodeReply<T>(
400405
body: string|FormData,
401406
moduleBasePath: ServerManifest,
402-
options?: {temporaryReferences?: TemporaryReferenceSet},
407+
options?: {
408+
temporaryReferences?: TemporaryReferenceSet,
409+
arraySizeLimit?: number,
410+
},
403411
): Thenable<T>{
404412
if(typeofbody=== 'string'){
405413
constform=newFormData();
@@ -411,6 +419,7 @@ function decodeReply<T>(
411419
'',
412420
options ? options.temporaryReferences : undefined,
413421
body,
422+
options ? options.arraySizeLimit : undefined,
414423
);
415424
constroot=getRoot<T>(response);
416425
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ export function registerServerActions(manifest: ServerManifest) {
245245

246246
exportfunctiondecodeReply<T>(
247247
body: string|FormData,
248-
options?: {temporaryReferences?: TemporaryReferenceSet},
248+
options?: {
249+
temporaryReferences?: TemporaryReferenceSet,
250+
arraySizeLimit?: number,
251+
},
249252
): Thenable<T>{
250253
if(typeofbody==='string'){
251254
constform=newFormData();
@@ -257,6 +260,7 @@ export function decodeReply<T>(
257260
'',
258261
options ? options.temporaryReferences : undefined,
259262
body,
263+
options ? options.arraySizeLimit : undefined,
260264
);
261265
constroot=getRoot<T>(response);
262266
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerEdge.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ export function registerServerActions(manifest: ServerManifest) {
250250

251251
exportfunctiondecodeReply<T>(
252252
body: string|FormData,
253-
options?: {temporaryReferences?: TemporaryReferenceSet},
253+
options?: {
254+
temporaryReferences?: TemporaryReferenceSet,
255+
arraySizeLimit?: number,
256+
},
254257
): Thenable<T>{
255258
if(typeofbody==='string'){
256259
constform=newFormData();
@@ -262,6 +265,7 @@ export function decodeReply<T>(
262265
'',
263266
options ? options.temporaryReferences : undefined,
264267
body,
268+
options ? options.arraySizeLimit : undefined,
265269
);
266270
constroot=getRoot<T>(response);
267271
close(response);

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ export function registerServerActions(manifest: ServerManifest) {
556556

557557
exportfunctiondecodeReplyFromBusboy<T>(
558558
busboyStream: Busboy,
559-
options?: {temporaryReferences?: TemporaryReferenceSet},
559+
options?: {
560+
temporaryReferences?: TemporaryReferenceSet,
561+
arraySizeLimit?: number,
562+
},
560563
): Thenable<T>{
561564
const response =createResponse(
562565
serverManifest,
563566
'',
564567
options ? options.temporaryReferences : undefined,
568+
undefined,
569+
options ? options.arraySizeLimit : undefined,
565570
);
566571
letpendingFiles=0;
567572
constqueuedFields: Array<string>=[];
@@ -626,7 +631,10 @@ export function decodeReplyFromBusboy<T>(
626631

627632
exportfunctiondecodeReply<T>(
628633
body: string|FormData,
629-
options?: {temporaryReferences?: TemporaryReferenceSet},
634+
options?: {
635+
temporaryReferences?: TemporaryReferenceSet,
636+
arraySizeLimit?: number,
637+
},
630638
): Thenable<T>{
631639
if(typeofbody=== 'string'){
632640
constform=newFormData();
@@ -638,6 +646,7 @@ export function decodeReply<T>(
638646
'',
639647
options ? options.temporaryReferences : undefined,
640648
body,
649+
options ? options.arraySizeLimit : undefined,
641650
);
642651
constroot=getRoot<T>(response);
643652
close(response);
@@ -646,7 +655,10 @@ export function decodeReply<T>(
646655

647656
exportfunctiondecodeReplyFromAsyncIterable<T>(
648657
iterable: AsyncIterable<[string,string|File]>,
649-
options?: {temporaryReferences?: TemporaryReferenceSet},
658+
options?: {
659+
temporaryReferences?: TemporaryReferenceSet,
660+
arraySizeLimit?: number,
661+
},
650662
): Thenable<T>{
651663
constiterator: AsyncIterator<[string,string|File]>=
652664
iterable[ASYNC_ITERATOR]();
@@ -655,6 +667,8 @@ export function decodeReplyFromAsyncIterable<T>(
655667
serverManifest,
656668
'',
657669
options ? options.temporaryReferences : undefined,
670+
undefined,
671+
options ? options.arraySizeLimit : undefined,
658672
);
659673

660674
functionprogress(

‎packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerBrowser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function prerender(
239239
functiondecodeReply<T>(
240240
body: string|FormData,
241241
turbopackMap: ServerManifest,
242-
options?: {temporaryReferences?: TemporaryReferenceSet},
242+
options?: {
243+
temporaryReferences?: TemporaryReferenceSet,
244+
arraySizeLimit?: number,
245+
},
243246
): Thenable<T>{
244247
if(typeofbody==='string'){
245248
constform=newFormData();
@@ -251,6 +254,7 @@ function decodeReply<T>(
251254
'',
252255
options ? options.temporaryReferences : undefined,
253256
body,
257+
options ? options.arraySizeLimit : undefined,
254258
);
255259
constroot=getRoot<T>(response);
256260
close(response);

0 commit comments

Comments
 (0)