Skip to content

Do not resolve symbolic links in posix locator if they exceed the count limit #21658

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

Merged
merged 6 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class PosixKnownPathsLocator extends Locator<BasicEnvInfo> {
// those binaries as environments.
const knownDirs = (await commonPosixBinPaths()).filter((dirname) => !isPyenvShimDir(dirname));
let pythonBinaries = await getPythonBinFromPosixPaths(knownDirs);
traceVerbose(`Found ${pythonBinaries.length} python binaries in posix paths`);

// Filter out MacOS system installs of Python 2 if necessary.
if (isMacPython2Deprecated) {
Expand Down
11 changes: 8 additions & 3 deletions src/client/pythonEnvironments/common/externalDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IDisposable, IConfigurationService } from '../../common/types';
import { chain, iterable } from '../../common/utils/async';
import { getOSType, OSType } from '../../common/utils/platform';
import { IServiceContainer } from '../../ioc/types';
import { traceVerbose } from '../../logging';
import { traceError, traceVerbose } from '../../logging';

let internalServiceContainer: IServiceContainer;
export function initializeExternalDependencies(serviceContainer: IServiceContainer): void {
Expand Down Expand Up @@ -93,16 +93,21 @@ export function arePathsSame(path1: string, path2: string): boolean {
return normCasePath(path1) === normCasePath(path2);
}

export async function resolveSymbolicLink(absPath: string, stats?: fsapi.Stats): Promise<string> {
export async function resolveSymbolicLink(absPath: string, stats?: fsapi.Stats, count?: number): Promise<string> {
stats = stats ?? (await fsapi.lstat(absPath));
if (stats.isSymbolicLink()) {
if (count && count > 5) {
traceError(`Detected a potential symbolic link loop at ${absPath}, terminating resolution.`);
return absPath;
}
const link = await fsapi.readlink(absPath);
// Result from readlink is not guaranteed to be an absolute path. For eg. on Mac it resolves
// /usr/local/bin/python3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9
//
// The resultant path is reported relative to the symlink directory we resolve. Convert that to absolute path.
const absLinkPath = path.isAbsolute(link) ? link : path.resolve(path.dirname(absPath), link);
return resolveSymbolicLink(absLinkPath);
count = count ? count + 1 : 1;
return resolveSymbolicLink(absLinkPath, undefined, count);
}
return absPath;
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/pythonEnvironments/common/posixUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path';
import { uniq } from 'lodash';
import { getSearchPathEntries } from '../../common/utils/exec';
import { resolveSymbolicLink } from './externalDependencies';
import { traceError, traceInfo } from '../../logging';
import { traceError, traceInfo, traceVerbose } from '../../logging';

/**
* Determine if the given filename looks like the simplest Python executable.
Expand Down Expand Up @@ -123,6 +123,7 @@ export async function getPythonBinFromPosixPaths(searchDirs: string[]): Promise<
// Ensure that we have a collection of unique global binaries by
// resolving all symlinks to the target binaries.
try {
traceVerbose(`Attempting to resolve symbolic link: ${filepath}`);
const resolvedBin = await resolveSymbolicLink(filepath);
if (binToLinkMap.has(resolvedBin)) {
binToLinkMap.get(resolvedBin)?.push(filepath);
Expand Down