Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving assets outside of the project root #27932

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ class AssetSourceResolver {
*/
scaledAssetURLNearBundle(): ResolvedAssetSource {
const path = this.jsbundleUrl || 'file://';
return this.fromSource(path + getScaledAssetPath(this.asset));
return this.fromSource(
// Assets can have relative paths outside of the project root.
// When bundling them we replace `../` with `_` to make sure they
// don't end up outside of the expected assets directory.
path + getScaledAssetPath(this.asset).replace(/\.\.\//g, '_'),
);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions Libraries/Image/__tests__/resolveAssetSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ describe('resolveAssetSource', () => {
},
);
});

it('resolves an image with a relative path outside of root', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/module/a',
httpServerLocation: '/assets/../../module/a',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: 'logo',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: 'file:///Path/To/Sample.app/assets/__module/a/logo.png',
scale: 1,
},
);
});
});

describe('bundle was loaded from assets on Android', () => {
Expand Down Expand Up @@ -175,6 +198,29 @@ describe('resolveAssetSource', () => {
},
);
});

it('resolves an image with a relative path outside of root', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/module/a',
httpServerLocation: '/assets/../../module/a',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: 'logo',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: '__module_a_logo',
scale: 1,
},
);
});
});

describe('bundle was loaded from file on Android', () => {
Expand Down