BUGFIX: Fix recursive alias detection causing infinite recursion (#2610)

This commit is contained in:
Lee Stutzman
2026-04-05 01:01:21 +01:00
committed by GitHub
parent 8b3c7c13c5
commit fbd7930ab2
2 changed files with 23 additions and 6 deletions

View File

@@ -93,7 +93,7 @@ function applyAliases(origCommand: string, depth = 0, currentlyProcessingAliases
// First get non-global aliases, and recursively apply them
// (unless there are any reference loops or the reference chain is too deep)
const localAlias = Aliases.get(commandArray[0]);
if (localAlias && !currentlyProcessingAliases.includes(localAlias)) {
if (localAlias && !currentlyProcessingAliases.includes(commandArray[0])) {
const appliedAlias = applyAliases(localAlias, depth + 1, [commandArray[0], ...currentlyProcessingAliases]);
commandArray.splice(0, 1, ...appliedAlias.split(" "));
}
@@ -101,7 +101,7 @@ function applyAliases(origCommand: string, depth = 0, currentlyProcessingAliases
// Once local aliasing is complete (or if none are present) handle any global aliases
const processedCommands = commandArray.reduce((resolvedCommandArray: string[], command) => {
const globalAlias = GlobalAliases.get(command);
if (globalAlias && !currentlyProcessingAliases.includes(globalAlias)) {
if (globalAlias && !currentlyProcessingAliases.includes(command)) {
const appliedAlias = applyAliases(globalAlias, depth + 1, [command, ...currentlyProcessingAliases]);
resolvedCommandArray.push(appliedAlias);
} else {

View File

@@ -1,5 +1,22 @@
import { substituteAliases, parseAliasDeclaration } from "../../../src/Alias";
import { Aliases, GlobalAliases, substituteAliases, parseAliasDeclaration } from "../../../src/Alias";
describe("substituteAliases Tests", () => {
beforeEach(() => {
Aliases.clear();
GlobalAliases.clear();
});
it("Should not infinitely recurse when alias value contains the alias name", () => {
parseAliasDeclaration("buy=buy -l");
const result = substituteAliases("buy");
expect(result).toEqual("buy -l");
});
it("Should not infinitely recurse when global alias value contains the alias name", () => {
parseAliasDeclaration("scan=scan -d 5", true);
const result = substituteAliases("scan");
expect(result).toEqual("scan -d 5");
});
it("Should gracefully handle recursive local aliases", () => {
parseAliasDeclaration("recursiveAlias=b");
parseAliasDeclaration("b=c");
@@ -7,7 +24,7 @@ describe("substituteAliases Tests", () => {
parseAliasDeclaration("d=recursiveAlias");
const result = substituteAliases("recursiveAlias");
expect(result).toEqual("d");
expect(result).toEqual("recursiveAlias");
});
it("Should only change local aliases if they are the start of the command", () => {
@@ -27,7 +44,7 @@ describe("substituteAliases Tests", () => {
parseAliasDeclaration("d=a", true);
const result = substituteAliases("a b c d");
expect(result).toEqual("d a b c");
expect(result).toEqual("a b c d");
});
it("Should gracefully handle recursive mixed local and global aliases", () => {
@@ -37,7 +54,7 @@ describe("substituteAliases Tests", () => {
parseAliasDeclaration("d=recursiveAlias", false);
const result = substituteAliases("recursiveAlias");
expect(result).toEqual("d");
expect(result).toEqual("recursiveAlias");
});
it("Should replace chained aliases", () => {