public override bool Execute() { TrustInfo info = new TrustInfo { IsFullTrust = false }; FrameworkName name = null; string str = string.Empty; if (!string.IsNullOrEmpty(this.TargetFrameworkMoniker)) { name = new FrameworkName(this.TargetFrameworkMoniker); str = name.Version.ToString(); } if ((this.BaseManifest != null) && File.Exists(this.BaseManifest.ItemSpec)) { try { info.ReadManifest(this.BaseManifest.ItemSpec); } catch (Exception exception) { base.Log.LogErrorWithCodeFromResources("GenerateManifest.ReadInputManifestFailed", new object[] { this.BaseManifest.ItemSpec, exception.Message }); return(false); } } if (!string.IsNullOrEmpty(this.ExcludedPermissions)) { base.Log.LogWarningFromResources("GenerateManifest.ExcludedPermissionsNotSupported", new object[0]); } try { if ((!string.IsNullOrEmpty(this.targetZone) && (info.PermissionSet != null)) && ((info.PermissionSet.Count > 0) && !string.Equals(this.targetZone, "Custom", StringComparison.OrdinalIgnoreCase))) { base.Log.LogErrorFromResources("GenerateManifest.KnownTargetZoneCannotHaveAdditionalPermissionType", new object[0]); return(false); } info.PermissionSet = SecurityUtilities.ComputeZonePermissionSetHelper(this.TargetZone, info.PermissionSet, this.applicationDependencies, this.TargetFrameworkMoniker); if (info.PermissionSet == null) { base.Log.LogErrorWithCodeFromResources("GenerateManifest.NoPermissionSetForTargetZone", new object[] { str }); return(false); } } catch (ArgumentNullException) { base.Log.LogErrorWithCodeFromResources("GenerateManifest.NoPermissionSetForTargetZone", new object[] { str }); return(false); } catch (ArgumentException exception2) { if (!string.Equals(exception2.ParamName, "TargetZone", StringComparison.OrdinalIgnoreCase)) { throw; } base.Log.LogWarningWithCodeFromResources("GenerateManifest.InvalidItemValue", new object[] { "TargetZone", this.TargetZone }); } info.Write(this.TrustInfoFile.ItemSpec); return(true); }
public void Basic() { TrustInfo t = new TrustInfo(); string file = FileUtilities.GetTemporaryFile(); File.Delete(file); t.WriteManifest(file); Assert.IsTrue(File.Exists(file)); // Writing a second time is an in-place modification. t.WriteManifest(file); Assert.IsTrue(File.Exists(file)); File.Delete(file); }
public override bool Execute() { var trustInfo = new TrustInfo { IsFullTrust = false }; string dotNetVersion = string.Empty; if (!string.IsNullOrEmpty(TargetFrameworkMoniker)) { var fn = new FrameworkNameVersioning(TargetFrameworkMoniker); dotNetVersion = fn.Version.ToString(); } // Read trust-info from app.manifest if (BaseManifest != null && File.Exists(BaseManifest.ItemSpec)) { try { trustInfo.ReadManifest(BaseManifest.ItemSpec); } catch (Exception ex) { Log.LogErrorWithCodeFromResources("GenerateManifest.ReadInputManifestFailed", BaseManifest.ItemSpec, ex.Message); return(false); } } if (!String.IsNullOrEmpty(ExcludedPermissions)) { Log.LogWarningFromResources("GenerateManifest.ExcludedPermissionsNotSupported"); } try { // If it's a known zone and the user add additional permission to it. if (!String.IsNullOrEmpty(TargetZone) && trustInfo.PermissionSet != null && trustInfo.PermissionSet.Count > 0 && !String.Equals(TargetZone, Custom, StringComparison.OrdinalIgnoreCase)) { Log.LogErrorFromResources("GenerateManifest.KnownTargetZoneCannotHaveAdditionalPermissionType"); return(false); } else { trustInfo.PermissionSet = SecurityUtilities.ComputeZonePermissionSetHelper(TargetZone, trustInfo.PermissionSet, ApplicationDependencies, TargetFrameworkMoniker); if (trustInfo.PermissionSet == null) { Log.LogErrorWithCodeFromResources("GenerateManifest.NoPermissionSetForTargetZone", dotNetVersion); return(false); } } } catch (ArgumentNullException) { Log.LogErrorWithCodeFromResources("GenerateManifest.NoPermissionSetForTargetZone", dotNetVersion); return(false); } catch (ArgumentException ex) { if (String.Equals(ex.ParamName, "TargetZone", StringComparison.OrdinalIgnoreCase)) { Log.LogWarningWithCodeFromResources("GenerateManifest.InvalidItemValue", "TargetZone", TargetZone); } else { throw; } } // Write trust-info back to a stand-alone trust file trustInfo.Write(TrustInfoFile.ItemSpec); return(true); }
internal TrustInfo Resolve() { if (Value is null) { return(new TrustInfo()); } var caseCorrected = Value?.ToLowerInvariant() switch { "full" => "Full", "internet" => "Internet", "localintranet" => "LocalIntranet", _ => Value }; switch (caseCorrected) { case "Internet": case "LocalIntranet": return(new TrustInfo { IsFullTrust = false, PermissionSet = SecurityUtilities.ComputeZonePermissionSet(caseCorrected, null, null), SameSiteAccess = sameSiteAccess ? "site" : "none" }); case "Full": return(new TrustInfo()); default: var trustFile = Path.Combine(sourcePath, Value); if (!File.Exists(trustFile)) { throw new ApplicationException($"Specified trust file '{trustFile}' not found."); } var customTrust = new TrustInfo(); // need to add namespaces if not already present var trustXml = new XmlDocument(); try { trustXml.Load(trustFile); } catch { throw new ApplicationException($"Specified trust file '{trustFile}' could not be read."); } if (trustXml.DocumentElement.Name.ToLowerInvariant() != "trustinfo") { throw new ApplicationException($"Specified trust file '{trustFile}' has an invalid root element"); } trustXml.GetOrCreateXmlDeclaration("1.0", "utf-8"); trustXml.DocumentElement.SetAttribute("xmlns", "urn:schemas-microsoft-com:asm.v2"); trustXml.DocumentElement.SetAttribute("xmlns:asmv2", "urn:schemas-microsoft-com:asm.v2"); var xmlStream = new MemoryStream(); trustXml.Save(xmlStream); xmlStream.Flush(); xmlStream.Position = 0; try { customTrust.Read(xmlStream); } catch { throw new ApplicationException($"Specified trust file '{trustFile}' could not be read."); } return(customTrust); } } }
private ApplicationManifest CreateApplicationManifest(string entryPoint, out string configFileName, out string entryPointFilePath) { entryPointFilePath = null; string frameworkVersion; if (string.IsNullOrEmpty(TargetFramework)) { frameworkVersion = "3.5"; } else { FrameworkName fn = new FrameworkName(TargetFramework); frameworkVersion = fn.Version.ToString(); } ApplicationManifest manifest = new ApplicationManifest(frameworkVersion); manifest.IsClickOnceManifest = true; manifest.IconFile = IconFile; configFileName = null; Dictionary <string, AssemblyIdentity> addedIdentities = new Dictionary <string, AssemblyIdentity>(); string basePath = Path.GetFullPath(BasePath); foreach (var taskItem in Files.Where(MatchFilter)) { string filePath = taskItem.GetMetadata("FullPath"); string targetPath = null; string dir = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); BaseReference reference = null; if (!dir.Equals(basePath, StringComparison.InvariantCultureIgnoreCase) && dir.StartsWith(basePath, StringComparison.InvariantCultureIgnoreCase)) { int index = basePath.Length; if (dir[index] == Path.DirectorySeparatorChar) { index++; } targetPath = Path.Combine(dir.Substring(index), fileName); } AssemblyIdentity identity = null; try { identity = AssemblyIdentity.FromFile(filePath); if (LinkAssembliesWithManifestAsFile && HasEmbeddedManifest(filePath)) { identity = null; } } catch (BadImageFormatException) { } if (identity != null) { string identityFullName = identity.GetFullName(AssemblyIdentity.FullNameFlags.All); if (addedIdentities.ContainsKey(identityFullName)) { throw new DuplicateAssemblyReferenceException(identityFullName); } else { addedIdentities.Add(identityFullName, identity); } AssemblyReference asmRef = new AssemblyReference(fileName); reference = asmRef; asmRef.AssemblyIdentity = identity; manifest.AssemblyReferences.Add(asmRef); if (manifest.EntryPoint == null && (string.IsNullOrEmpty(entryPoint) || string.Equals(entryPoint, fileName, StringComparison.InvariantCultureIgnoreCase)) && Path.GetExtension(fileName).Equals(".exe", StringComparison.InvariantCultureIgnoreCase)) { configFileName = SetEntryPointAndConfig(manifest, filePath, asmRef); entryPointFilePath = filePath; } } else { FileReference fileRef = new FileReference(fileName); reference = fileRef; manifest.FileReferences.Add(fileRef); } Log.LogMessage(MessageImportance.Low, "TargetPath for {0}: {1}", fileName, targetPath); reference.TargetPath = targetPath; } List <string> searchPaths = new List <string>(); searchPaths.Add(BasePath); if (ConfigFile != null) { searchPaths.Add(Path.GetDirectoryName(ConfigFile.ItemSpec)); } manifest.ResolveFiles(searchPaths.ToArray()); manifest.UpdateFileInfo(); TrustInfo trust = new TrustInfo(); trust.IsFullTrust = true; manifest.TrustInfo = trust; if (manifest.EntryPoint == null) { Log.LogError("Cannot determine EntryPoint. EntryPoint property = '{0}'", entryPoint ?? string.Empty); } return(manifest); }