fix(image): preserve source headers with crossOrigin and referrerPolicy (#58319)

Summary:
When `crossOrigin="use-credentials"` or `referrerPolicy` is used on an `Image`, `getImageSourcesFromImageProps` currently fully replaces `source.headers`, removing custom headers such as `Authorization`.

This fix merges the generated headers with the user-provided source headers instead (user-provided headers take precedence).

## Changelog:

[GENERAL] [FIXED] - Preserve Image source headers when using crossOrigin or referrerPolicy

Pull Request resolved: https://github.com/react/react-native/pull/58319

Test Plan: Confirmed existing tests are unchanged & added a regression test

Reviewed By: christophpurrer

Differential Revision: D118797893

Pulled By: javache

fbshipit-source-id: 8018846647232f97cb9e12d6095774826999c89b
This commit is contained in:
Maximilian Krause authored and meta-codesync[bot] committed 2026-09-04 14:19:29 -07:00
1 parent 3ca6ea3eca
commit 08c7781e5a
2 files changed
+21 -4

No files matched your search

+9 -1
View File
@@ -12,6 +12,7 @@
import type {ResolvedAssetSource} from './AssetSourceResolver';
import type {ImageProps} from './ImageProps';
import type {ImageURISource} from './ImageSource';
import resolveAssetSource from './resolveAssetSource';
@@ -87,7 +88,14 @@ export function getImageSourcesFromImageProps(imageProps: ImageProps):
} else if (src != null) {
sources = [{uri: src, headers: headers, width, height}];
} else if (source != null && source.uri && Object.keys(headers).length > 0) {
sources = [{...source, headers}];
const sourceHeaders = (source as ImageURISource).headers;
sources = [
{
...source,
headers:
sourceHeaders != null ? {...headers, ...sourceHeaders} : headers,
},
];
} else {
sources = source;
}
@@ -102,19 +102,28 @@ describe('<Image>', () => {
);
});
it('sets the "Access-Control-Allow-Credentials" header in "use-credentials" mode', () => {
it('adds the credentials header without replacing source headers', () => {
const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(
<Image crossOrigin="use-credentials" source={LOGO_SOURCE} />,
<Image
crossOrigin="use-credentials"
source={{
...LOGO_SOURCE,
headers: {Authorization: 'Bearer token'},
}}
/>,
);
});
expect(
root.getRenderedOutput({props: ['source-header']}).toJSX(),
).toEqual(
<rn-image source-header-Access-Control-Allow-Credentials="true" />,
<rn-image
source-header-Access-Control-Allow-Credentials="true"
source-header-Authorization="Bearer token"
/>,
);
});
});