Why does ILMerge delete security-related attributes?

151 views Asked by At

At some time, ILMerge was changed so that it now deletes security-related attributes.

Questions:

  • Why is this functionality implemented?
  • And/or can it (safely) be removed from a private build of the tool (used to create a published assembly)?

The implementation in the source code is here, fwiw:

  #region Deal with [ComVisible] and security attributes
  var thisAssemblyIsComVisible = GetComVisibleSettingForAssembly(a);
  AttributeNode assemblyComVisibleAttribute = null;
  if (thisAssemblyIsComVisible != targetAssemblyIsComVisible) {
    InstanceInitializer ctor = SystemTypes.ComVisibleAttribute.GetConstructor(SystemTypes.Boolean);
    assemblyComVisibleAttribute = new AttributeNode(new MemberBinding(null, ctor), new ExpressionList(new Literal(thisAssemblyIsComVisible, SystemTypes.Boolean)));
  }
  for (int i = 0, n = a.Attributes == null ? 0 : a.Attributes.Count; i < n; i++) {
    AttributeNode aNode = a.Attributes[i];
    if (aNode == null) continue;
    if (aNode.Type == SystemTypes.ComVisibleAttribute) {
      a.Attributes[i] = null;
      continue;
    }
    if (aNode.Type == SystemTypes.SecurityCriticalAttribute
      || aNode.Type == SystemTypes.SecurityTransparentAttribute
      || aNode.Type == SystemTypes.AllowPartiallyTrustedCallersAttribute
      || aNode.Type.FullName.Equals("System.Security.SecurityRules")
      ) {
      WriteToLog("Assembly level attribute '{0}' from assembly '{1}' being deleted from target assembly",
        aNode.Type.FullName, a.Name);
      a.Attributes[i] = null;
      continue;
    }
  }
  #endregion

It causes problems for some people:

I want to use ILMerge to build an assembly with the AllowPartiallyTrustedCallers attribute. I used to do that (with an older version of ILMerge) and don't know why I can't anymore.

I could presumably do it by not using ILMerge at all (i.e. by putting all the source code into one project, instead of building several projects to be merged), so I don't see what the harm is, i.e. why ILMerge will no longer do it.

1

There are 1 answers

3
ChrisW On

I'm now using ILRepack instead, whose command-line is the same as ILMerge and which doesn't have this limitation.