简体   繁体   English

CA2122 DoNotIndirectExposeMethodsWithLinkDemands

[英]CA2122 DoNotIndirectlyExposeMethodsWithLinkDemands

I got error CA2122 DoNotIndirectlyExposeMethodsWithLinkDemands on this function : 我在此函数上收到错误CA2122 DoNotIndirectExposeMethodsWithLinkDemands:

  internal static string GetProcessID()
        {

                return Process.GetCurrentProcess().Id.ToString(CultureInfo.CurrentCulture);

        }

How to fix it? 如何解决?

I got error CA2122 我收到错误CA2122

It is not an error, just a warning. 这不是错误,只是警告。 The code analysis tool you are using checks for lots of obscure corner-cases, the kind that the C# compiler does not complain about but might be a bad practice. 您使用的代码分析工具会检查很多晦涩的极端情况,这种情况是C#编译器不会抱怨的,但可能不是一个好习惯。 And the kind that programmers are often unaware of. 以及程序员通常不知道的那种。 It was originally designed as an internal tool used by Microsoft programmers working on framework code. 它最初设计为内部工具,供从事框架代码工作的Microsoft程序员使用。 The rules they must follow are pretty draconian since they can't predict how their code is going to be used. 他们必须遵循的规则非常严苛,因为它们无法预测将如何使用其代码。

...WithLinkDemands ... WithLinkDemands

A link demand is Code Access Security (CAS) detail. 链接需求是代码访问安全性(CAS)详细信息。 It ensures that code has sufficient rights to execute. 它确保代码具有执行的足够权限。 Link demands are very cheap, they are checked only once, happens when the code is just-in-time compiled. 链接需求非常便宜,它们仅被检查一次,在即时编译代码时发生。 The "only-once" clause is what the warning is talking about, it is technically possible for code that has sufficient rights to execute first, thus allowing the method to be jitted, but used later by non-trusted code, thus bypassing the check. 警告仅指“ on-once”子句,具有足够权限的代码在技术上有可能首先执行,从而允许方法被伪装,但随后由不可信代码使用,从而绕过检查。 The tool just assumes that this might happen because the method is public, it doesn't know for a fact that this actually happens in your program. 该工具仅假定可能由于该方法是公共方法而发生这种情况,因此并不知道这种情况实际上是在您的程序中发生的。

return Process.GetCurrentProcess()... 返回Process.GetCurrentProcess()...

It is the Process class that has the link demand. 具有链接需求的是Process类。 You can tell from the MSDN article which demands it makes. 您可以从MSDN文章中了解哪些要求。 It verifies that the calling code runs in full trust, that it doesn't run in a restrictive unmanaged host like SQL Server and that a derived class meets these demands as well. 它验证了调用代码是否完全信任地运行,没有在诸如SQL Server这样的不受限制的非托管主机中运行,并且派生类也满足了这些要求。 The Process class is a bit risky, untrusted code could do naughty things by starting a process to bypass CAS checks or to learn too much about the process it runs in and tinker with its configuration. Process类有点冒险,不受信任的代码可以通过启动一个过程来绕过CAS检查或对它运行的过程了解太多并修改其配置,从而做恶作剧。

How to fix it? 如何解决?

More than one possible approach. 不止一种可能的方法。 Roughly in order: 大致顺序为:

  1. Always high odds that this warning just doesn't apply to your program. 总是有很大的可能性,这个警告并不适用于您的程序。 In other words, there is no risk of it ever executing code that you don't trust. 换句话说,它永远不会执行您不信任的代码。 Your program would have to support plug-ins, written by programmers you don't know about but still have access to the machine to tell your program to load their plug-in. 您的程序必须支持由您不认识的程序员编写的插件,但仍然可以访问该计算机以告诉您的程序加载其插件。 Not very common. 不是很常见。 Proper approach then is to configure the tool to match your program's behavior, you'd disable the rule. 正确的方法是配置工具以匹配程序的行为,然后禁用该规则。

  2. Evaluate the risk of untrusted code using this method. 使用此方法评估不受信任的代码的风险。 That ought to be a low one for this specific method, exposing the process ID does not give away any major secrets. 对于该特定方法,这应该是一个较低的值,因为公开进程ID不会泄露任何主要机密。 It is just a number, it doesn't get to be a risky number until it is used by code that uses Process.GetProcessById(). 它只是一个数字,在使用Process.GetProcessById()的代码使用它之前,它不会成为危险数字。 So you'd consider to suppress the warning, apply the [SuppressMessage] attribute to the method. 因此,您可以考虑取消警告,将[SuppressMessage]属性应用于该方法。 This is a common outcome, the framework source code has lots and lots of them. 这是一个常见的结果,框架源代码有很多。

  3. Follow the tool's advice and apply the CAS attributes to this method as well. 遵循工具的建议,并将CAS属性也应用于此方法。 Simply a copy-paste from the link demands you saw in the MSDN article. 只需从MSDN文章中看到的链接中复制粘贴即可。 This closes the "only-once" loophole, the untrusted code will now fail to jit and can't execute. 这消除了“仅一次”漏洞,不受信任的代码现在将无法启动并且无法执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM