Skip to content

Commit 68d3269

Browse files
authored
fix: address code scanning alerts in yok and the CocoaPods service (#6228)
- yok: match the optional argument list of a dynamic call once instead of with a nested quantifier, removing exponential backtracking. Matches are unchanged. - cocoapods: write plugin Podfile headers with the raw path and build the section-removal regex with regExpEscape, so plugin paths containing regex characters (pnpm "+" and "(peer@x)" segments) have their sections removed. Headers written by older versions with "\+" still match. [skip ci]
1 parent bae5b1c commit 68d3269

3 files changed

Lines changed: 76 additions & 14 deletions

File tree

‎lib/common/yok.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ export class Yok extends Injector implements IInjector {
657657
* runtime-traced via reportDeprecation.
658658
*/
659659
public get dynamicCallRegex(): RegExp {
660-
return /#{([^.]+)\.([^}]+?)(\((.+)\))*}/;
660+
return /#{([^.]+)\.([^}]+?)(\((.+)\))?}/;
661661
}
662662

663663
/**

‎lib/services/cocoapods-service.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,9 @@ end`.trim();
339339

340340
extensionsPodfile.forEach(({ targetName, podfilePath }) => {
341341
// Remove the data between #Begin Podfile and #EndPodfile
342-
const regExpToRemove = new RegExp(
343-
`${this.getExtensionPodfileHeader(
344-
podfilePath,
345-
targetName,
346-
)}[\\s\\S]*?${this.getExtensionPodfileEnd()}`,
347-
"mg",
342+
const regExpToRemove = this.getPodfileSectionRegExp(
343+
this.getExtensionPodfileHeader(podfilePath, targetName),
344+
this.getExtensionPodfileEnd(),
348345
);
349346
projectPodFileContent = projectPodFileContent.replace(regExpToRemove, "");
350347

@@ -448,11 +445,9 @@ end`.trim();
448445
this.getProjectPodfilePath(projectRoot),
449446
);
450447
// Remove the data between #Begin Podfile and #EndPodfile
451-
const regExpToRemove = new RegExp(
452-
`${this.getPluginPodfileHeader(
453-
podfilePath,
454-
)}[\\s\\S]*?${this.getPluginPodfileEnd()}`,
455-
"mg",
448+
const regExpToRemove = this.getPodfileSectionRegExp(
449+
this.getPluginPodfileHeader(podfilePath),
450+
this.getPluginPodfileEnd(),
456451
);
457452
projectPodFileContent = projectPodFileContent.replace(regExpToRemove, "");
458453
projectPodFileContent = this.removePostInstallHook(
@@ -655,11 +650,18 @@ end`.trim();
655650
}
656651

657652
private getPluginPodfileHeader(pluginPodFilePath: string): string {
658-
// escape special + from the podfile path (pnpm)
659-
pluginPodFilePath = pluginPodFilePath.replace(/\+/g, "\\+");
660653
return `# Begin Podfile - ${pluginPodFilePath}`;
661654
}
662655

656+
private getPodfileSectionRegExp(header: string, end: string): RegExp {
657+
// Podfiles written by older CLI versions have "+" in the header path escaped as "\+".
658+
const headerPattern = header
659+
.split("+")
660+
.map((part) => regExpEscape(part))
661+
.join("\\\\?\\+");
662+
return new RegExp(`${headerPattern}[\\s\\S]*?${regExpEscape(end)}`, "mg");
663+
}
664+
663665
private getPluginPodfileEnd(): string {
664666
return `# End Podfile${EOL}`;
665667
}

‎test/cocoapods-service.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,66 @@ end`,
810810
);
811811
});
812812
});
813+
814+
describe("when the plugin path contains regular expression characters", () => {
815+
const pnpmPluginData: any = {
816+
name: "plugin1",
817+
pluginPlatformsFolderPath: () =>
818+
"node_modules/.pnpm/plugin1@1.0.0_a+b(peer@2.0.0)/pluginPlatformsFolderPath",
819+
};
820+
821+
const projectPodfileWithHeader = (headerPath: string) => `use_frameworks!
822+
823+
target "projectName" do
824+
# Begin Podfile - ${headerPath}
825+
826+
pod 'GoogleAnalytics', '~> 3.1'
827+
828+
# End Podfile
829+
830+
end`;
831+
832+
it("removes the Podfile section added for the plugin", async () => {
833+
const pluginPodfilePath =
834+
cocoapodsService.getPluginPodfilePath(pnpmPluginData);
835+
mockFileSystem(testInjector, "pod 'GoogleAnalytics', '~> 3.1'");
836+
await cocoapodsService.applyPodfileToProject(
837+
pnpmPluginData.name,
838+
pluginPodfilePath,
839+
mockProjectData,
840+
mockPlatformData,
841+
);
842+
assert.include(newPodfileContent, pluginPodfilePath);
843+
844+
cocoapodsService.removePodfileFromProject(
845+
pnpmPluginData.name,
846+
pluginPodfilePath,
847+
mockProjectData,
848+
nativeProjectPath,
849+
);
850+
851+
assert.isNull(newPodfileContent);
852+
});
853+
854+
it("removes a Podfile section whose header has + escaped", async () => {
855+
const pluginPodfilePath =
856+
cocoapodsService.getPluginPodfilePath(pnpmPluginData);
857+
mockFileSystem(
858+
testInjector,
859+
"",
860+
projectPodfileWithHeader(pluginPodfilePath.split("+").join("\\+")),
861+
);
862+
863+
cocoapodsService.removePodfileFromProject(
864+
pnpmPluginData.name,
865+
pluginPodfilePath,
866+
mockProjectData,
867+
nativeProjectPath,
868+
);
869+
870+
assert.isNull(newPodfileContent);
871+
});
872+
});
813873
});
814874

815875
describe("executePodInstall", () => {

0 commit comments

Comments
 (0)