Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions packages/aws-cdk-lib/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import type { AssetOptions } from './assets';
import { AssetHashType, FileAssetPackaging } from './assets';
import type { BundlingOptions } from './bundling';
import { BundlingFileAccess, BundlingOutput, PERF_BUNDLING_SRC_SYM } from './bundling';
import { AssumptionError, ValidationError } from './errors';
import { AssumptionError, UnscopedValidationError, ValidationError } from './errors';
import type { FingerprintOptions } from './fs';
import { FileSystem } from './fs';
import { FileSystem, SymlinkFollowMode } from './fs';
import { clearLargeFileFingerprintCache } from './fs/fingerprint';
import { Names } from './names';
import { AssetBundlingVolumeCopy, AssetBundlingBindMount } from './private/asset-staging';
import { Cache } from './private/cache';
import { stackOf, stageOf } from './private/core-construct-finders';
import type { Stack } from './stack';
import * as cxapi from '../../cx-api';
import { lit } from './private/literal-string';
import { profileSpan } from './private/perf';
import type { Stack } from './stack';
import * as cxapi from '../../cx-api';
import { isInternalPath, resolveLinkTarget } from './fs/utils';

const ARCHIVE_EXTENSIONS = ['.tar.gz', '.zip', '.jar', '.tar', '.tgz'];

Expand Down Expand Up @@ -181,6 +182,11 @@ export class AssetStaging extends Construct {
throw new ValidationError(lit`CannotFindAsset`, `Cannot find asset at ${this.sourcePath}`, this);
}

// look for invalid (external) symlinks
if (props.follow == SymlinkFollowMode.BLOCK_EXTERNAL) {
findInvalidSymlinks(this.sourcePath);
}

this._sourceStats = fs.statSync(this.sourcePath);

const outdir = stageOf(this)?.assetOutdir;
Expand Down Expand Up @@ -573,6 +579,31 @@ function determineHashType(scope: Construct, assetHashType?: AssetHashType, cust
return hashType;
}

/**
* Walk the directory tree, throw if we find external symlinks
* @param root true root of the directory
* @param subRoot used for walking subdirectories
*/
function findInvalidSymlinks(root: string, subRoot: string = root) {
const entries = fs.readdirSync(root, { withFileTypes: true });
for (const entry of entries) {
const childPath = path.join(subRoot, entry.name);
if (entry.isSymbolicLink()) {
// we check whether this is internal or external, throw on external
const linkPath = fs.readlinkSync(childPath);
const resolvedPath = resolveLinkTarget(childPath, linkPath);
if (!isInternalPath(root, resolvedPath)) {
throw new UnscopedValidationError(
lit`BundlingFileSymlinkForbidden`,
`The file ${resolvedPath} is a symbolic link that is forbidden due to follow mode internal-only. Set \`follow\` to a mode that will follow symlinks (ALWAYS or EXTERNAL) or emit a regular file`,
);
}
} else if (entry.isDirectory()) {
findInvalidSymlinks(root, childPath);
}
}
}

/**
* Calculates a cache key from the props. Normalize by sorting keys.
*/
Expand Down Expand Up @@ -636,6 +667,7 @@ function findSingleFile(scope: Construct, directory: string, archiveOnly: boolea
if (content.length === 1) {
const file = path.join(directory, content[0]);
const extension = getExtension(content[0]).toLowerCase();

if (fs.statSync(file).isFile() && (!archiveOnly || ARCHIVE_EXTENSIONS.includes(extension))) {
return file;
}
Expand Down
10 changes: 2 additions & 8 deletions packages/aws-cdk-lib/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FingerprintDiskCache } from './fingerprint-disk-cache';
import { IgnoreStrategy } from './ignore';
import type { FingerprintOptions } from './options';
import { IgnoreMode, SymlinkFollowMode } from './options';
import { isInternalPath } from './utils';
import { isInternalPath, resolveLinkTarget } from './utils';
import { UnscopedValidationError } from '../errors';
import { lit } from '../private/literal-string';

Expand Down Expand Up @@ -98,12 +98,6 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
}
}

function _resolveLinkTarget(realPath: string, linkTarget: string): string {
return path.isAbsolute(linkTarget)
? path.resolve(linkTarget)
: path.resolve(path.dirname(realPath), linkTarget);
}

// --- Core traversal ---

function _processDirectory(symbolicPath: string, realPath: string) {
Expand Down Expand Up @@ -131,7 +125,7 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions

function _processSymlink(symbolicPath: string, realPath: string) {
const linkTarget = fs.readlinkSync(realPath);
const resolvedLinkTarget = _resolveLinkTarget(realPath, linkTarget);
const resolvedLinkTarget = resolveLinkTarget(realPath, linkTarget);

if (!_shouldFollowLink(resolvedLinkTarget)) {
// Not following — hash the link target string itself
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk-lib/core/lib/fs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ export function shouldFollow(mode: SymlinkFollowMode, sourceRoot: string, realPa
export function isInternalPath(rootPath: string, targetPath: string): boolean {
return rootPath === targetPath || targetPath.startsWith(rootPath + path.sep);
}

export function resolveLinkTarget(realPath: string, linkTarget: string): string {
return path.isAbsolute(linkTarget)
? path.resolve(linkTarget)
: path.resolve(path.dirname(realPath), linkTarget);
}
Binary file modified packages/aws-cdk-lib/core/test/fs/fixtures.tar.gz
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file4
3 changes: 3 additions & 0 deletions packages/aws-cdk-lib/core/test/fs/fs-copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('fs copy', () => {
' .hidden',
' subdir3 (D)',
' file3.txt',
'subdir4 (D)',
' file4.txt',
' local-link4.txt => file4.txt',
]);
});

Expand Down
28 changes: 28 additions & 0 deletions packages/aws-cdk-lib/core/test/fs/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,32 @@ describe('utils', () => {
expect(util.isInternalPath(root, path.resolve(path.join('source', 'elsewhere', 'file.txt')))).toEqual(false);
});
});

describe('resolveLinkTarget', () => {
test('an absolute link target is resolved as-is', () => {
const realPath = path.join('source', 'root', 'link');
const linkTarget = path.resolve(path.join('somewhere', 'else', 'referent'));

expect(util.resolveLinkTarget(realPath, linkTarget)).toEqual(path.resolve(linkTarget));
});

test('a relative link target is resolved against the directory of the link', () => {
const realPath = path.join('source', 'root', 'link');
const linkTarget = 'referent';

// Resolved relative to the link's directory ('source/root'), not the cwd.
expect(util.resolveLinkTarget(realPath, linkTarget)).toEqual(
path.resolve(path.join('source', 'root'), 'referent'),
);
});

test('a relative link target with parent segments is normalized', () => {
const realPath = path.join('source', 'root', 'nested', 'link');
const linkTarget = path.join('..', 'sibling', 'referent');

expect(util.resolveLinkTarget(realPath, linkTarget)).toEqual(
path.resolve(path.join('source', 'root', 'sibling', 'referent')),
);
});
});
});
Loading
Loading