コード例 #1
0
        public void CallingAMethodInACriticalTypeRequiresPrivileges()
        {
            PrepareTestForAssembly(@"
            
			public class Test
			{
				public void M1()
				{
					CriticalType.M1();
				}
			}
			public class CriticalType
            {
				static public void M1()
				{
				}
            }
");

            var criticalTypes = new[] { _cdf.FindType("CriticalType") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes      = criticalTypes,
                expectedInjections = new[] {
                    new CecilSecurityAttributeDescriptor(criticalTypes[0], SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void Test::M1()"), SecurityAttributeType.Critical)
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #2
0
        public void CallingAMethodInACriticalTypeRequiresPrivileges()
        {
            PrepareTestForAssembly(@"

            public class Test
            {
                public void M1()
                {
                    CriticalType.M1();
                }
            }
            public class CriticalType
            {
                static public void M1()
                {
                }
            }
            ");

            var criticalTypes = new[] {_cdf.FindType("CriticalType")};

            var ptd = new PropagateTestDescription()
                          {
                              criticalTypes = criticalTypes,
                              expectedInjections = new[] {
                                                             new CecilSecurityAttributeDescriptor(criticalTypes[0], SecurityAttributeType.Critical),
                                                             new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void Test::M1()"), SecurityAttributeType.Critical)
                                                         }
                          };

            PropagateAndAssert(ptd);
        }
コード例 #3
0
        public void SingleAssemblyWithCriticalType()
        {
            PrepareTestForAssembly(@"
            
            public interface IMyEnumerator
            {
                void M1();
            }

            public class CriticalType : IMyEnumerator
            {
                public void M1()
                {
                    Evil.Do();
                }
            }
            
");
            var criticaltype = _cdf.FindType("CriticalType");

            var expected = new[] {
                new CecilSecurityAttributeDescriptor(criticaltype, SecurityAttributeType.Critical),
                new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical)
            };

            var prd = new PropagateTestDescription()
            {
                criticalTypes = new[] { criticaltype },
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections         = expected
            };

            PropagateAndAssert(prd);
        }
コード例 #4
0
        public void CriticalTypeEnheritingFromTransparentType()
        {
            PrepareTestForAssembly(@"
            
            public class TransparantType
            {
                public virtual void M1()
                {
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes      = criticaltypes,
                expectedInjections = new[] {
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #5
0
        public void CriticalTypeEnheritingFromTransparentType()
        {
            PrepareTestForAssembly(@"

            public class TransparantType
            {
                public virtual void M1()
                {
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] {_cdf.FindType("CriticalType")};

            var ptd = new PropagateTestDescription()
                          {
                              criticalTypes = criticaltypes,
                              expectedInjections = new[] {
                                                             new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                                                         }
                          };

            PropagateAndAssert(ptd);
        }
コード例 #6
0
        public void IgnoredCallsGetRespected()
        {
            PrepareTestForAssembly(@"

            public class Test
            {
                public void M1()
                {
					Evil.Do();
                }
            }
");

            var ptd = new PropagateTestDescription()
            {
                methodsRequiringPrivileges = new[] { _evilDo },
                callsToIgnore = new[]
                {
                    new MethodToMethodCall(_cdf.FindMethod("System.Void Test::M1()"), _evilDo)
                },
                expectedInjections = new[] {
                    new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical)
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #7
0
        private void PropagateAndAssert(PropagateTestDescription ptd)
        {
            var propagation = new MethodPrivilegePropagation(new[] { _assembly }, ptd.methodsRequiringPrivileges, ptd.safeCriticalMethods, ptd.criticalTypes, ptd.callsToIgnore);
            var report      = propagation.CreateReportBuilder().Build();
            var injections  = report.GetInjectionsFor(_assembly).ToList();

            CollectionAssert.AreEquivalent(ptd.expectedInjections.ToList(), injections);
        }
コード例 #8
0
        public void ThreeLevelHierarchy_Gets_Correct_Method_Level_Attributes()
        {
            PrepareTestForAssembly(@"
            
            public class T1
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            public class C1 : T1
            {
                public override void M1()
                {
                }
            }
            public class C2 : C1
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("C1"), _cdf.FindType("C2") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections         = new[] {
                    new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(criticaltypes[1], SecurityAttributeType.Critical),

                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void T1::M1()"), SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void C1::M1()"), SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void C2::M1()"), SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #9
0
        public void InterfaceWithCriticalMethod_Causes_CriticalTypeImplementor_To_Have_MethodLevel_SC_On_Implementing_Method()
        {
            PrepareTestForAssembly(@"
            
            public interface I
            {
                void M1();
            }
            public class T : I
            {
                public void M1()
                {
                    Evil.Do();
                }
            }
            public class C : I
            {
                public void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("C") };
            var im1           = _cdf.FindMethod("System.Void I::M1()");
            var tm1           = _cdf.FindMethod("System.Void T::M1()");
            var cm1           = _cdf.FindMethod("System.Void C::M1()");

            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections         = new[] {
                    new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(im1, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(tm1, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(cm1, SecurityAttributeType.Critical),     //<-- this one is what this test is about.
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #10
0
        public void Throw_When_Method_Needs_To_Become_SC_Because_Of_Enheritance_Rules_But_Method_Was_Manually_Marked_SSC()
        {
            PrepareTestForAssembly(@"
            public class TestBase
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            
			public class TestChild : TestBase
			{
				public override void M1()
				{
				}
			}

");

            var baseM1  = _cdf.FindMethod("System.Void TestBase::M1()");
            var childM1 = _cdf.FindMethod("System.Void TestChild::M1()");

            var ptd = new PropagateTestDescription()
            {
                safeCriticalMethods        = new[] { childM1 },
                methodsRequiringPrivileges = new[] { _evilDo },
            };

            try
            {
                PropagateAndAssert(ptd);
            } catch (ArgumentException ae)
            {
                return;
            }
            Assert.Fail("Propagate needs to throw, because it needs to mark a method as [SC], which was manually specified as [SSC]");
        }
コード例 #11
0
        public void Method_In_Critical_Type_That_Does_Evil_Does_Not_Cause_Base_Method_To_Be_SecurityCritical()
        {
            PrepareTestForAssembly(@"
            
            public class TransparantType
            {
                public virtual void M1()
                {
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                    Evil.Do();
                }
            }
            public class TransparantType2 : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes      = criticaltypes,
                expectedInjections = new[] {
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #12
0
        public void MethodInCriticalType_WhichOverrides_CriticalMethodInBaseClass_AlsoGetsMethodLevelCriticalAttribute()
        {
            PrepareTestForAssembly(@"

            public class TransparantType
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections         = new[] {
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void TransparantType::M1()"), SecurityAttributeType.Critical),

                    //this one is what most of this test is about:
                    new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void CriticalType::M1()"), SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #13
0
        public void Method_In_Transparent_Type_That_Does_Evil_Causes_Overriden_Method_To_Be_SC_Even_If_That_Method_Lives_In_A_Critical_Type()
        {
            PrepareTestForAssembly(@"
            
            public class TransparantType
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };
            var tm1           = _cdf.FindMethod("System.Void TransparantType::M1()");
            var cm1           = _cdf.FindMethod("System.Void CriticalType::M1()");
            var ptd           = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections         = new[] {
                    new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(tm1, SecurityAttributeType.Critical),
                    new CecilSecurityAttributeDescriptor(cm1, SecurityAttributeType.Critical),      //<-- test is mostly about this one.
                    new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #14
0
        public void SecuritySafeCriticalGetsAppliedToMethodRequiringPrivilegesItself()
        {
            PrepareTestForAssembly(@"
            public class Test
            {
				public void M1()
				{
				}
            }
");

            var testM1 = _cdf.FindMethod("System.Void Test::M1()");

            var ptd = new PropagateTestDescription()
            {
                safeCriticalMethods        = new[] { testM1 },
                methodsRequiringPrivileges = new[] { testM1 },
                expectedInjections         = new[] {
                    new CecilSecurityAttributeDescriptor(testM1, SecurityAttributeType.SafeCritical)
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #15
0
        public void MethodInCriticalType_WhichOverrides_CriticalMethodInBaseClass_AlsoGetsMethodLevelCriticalAttribute()
        {
            PrepareTestForAssembly(@"

            public class TransparantType
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] {_cdf.FindType("CriticalType")};

            var ptd = new PropagateTestDescription()
                          {
                              criticalTypes = criticaltypes,
                              methodsRequiringPrivileges = new[] {_evilDo},
                              expectedInjections = new[] {
                                                             new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                                                             new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                                                             new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void TransparantType::M1()"), SecurityAttributeType.Critical),

                                                             //this one is what most of this test is about:
                                                             new CecilSecurityAttributeDescriptor(_cdf.FindMethod("System.Void CriticalType::M1()"), SecurityAttributeType.Critical),
                                                         }
                          };

            PropagateAndAssert(ptd);
        }
コード例 #16
0
        public void Method_In_Critical_Type_That_Does_Evil_Does_Not_Cause_Base_Method_To_Be_SecurityCritical()
        {
            PrepareTestForAssembly(@"

            public class TransparantType
            {
                public virtual void M1()
                {
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                    Evil.Do();
                }
            }
            public class TransparantType2 : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };

            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                expectedInjections = new[] {
                     new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #17
0
        public void Throw_When_Method_Needs_To_Become_SC_Because_Of_Enheritance_Rules_But_Method_Was_Manually_Marked_SSC()
        {
            PrepareTestForAssembly(@"
            public class TestBase
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }

            public class TestChild : TestBase
            {
                public override void M1()
                {
                }
            }

            ");

            var baseM1 = _cdf.FindMethod("System.Void TestBase::M1()");
            var childM1 = _cdf.FindMethod("System.Void TestChild::M1()");

            var ptd = new PropagateTestDescription()
                          {
                              safeCriticalMethods = new[] { childM1 },
                              methodsRequiringPrivileges = new[] {_evilDo},
                          };
            try
            {
                PropagateAndAssert(ptd);
            } catch (ArgumentException ae)
            {
                return;
            }
            Assert.Fail("Propagate needs to throw, because it needs to mark a method as [SC], which was manually specified as [SSC]");
        }
コード例 #18
0
        public void InterfaceWithCriticalMethod_Causes_CriticalTypeImplementor_To_Have_MethodLevel_SC_On_Implementing_Method()
        {
            PrepareTestForAssembly(@"

            public interface I
            {
                void M1();
            }
            public class T : I
            {
                public void M1()
                {
                    Evil.Do();
                }
            }
            public class C : I
            {
                public void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("C") };
            var im1 = _cdf.FindMethod("System.Void I::M1()");
            var tm1 = _cdf.FindMethod("System.Void T::M1()");
            var cm1 = _cdf.FindMethod("System.Void C::M1()");

            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections = new[] {
                        new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(im1, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(tm1, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(cm1, SecurityAttributeType.Critical), //<-- this one is what this test is about.
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #19
0
        public void CriticalType_Hierarchy_Does_Not_Get_Method_Level_Attributes()
        {
            PrepareTestForAssembly(@"

            public class C1
            {
                public virtual void M1()
                {
                }
            }
            public class C2 : C1
            {
                public override void M1()
                {
                }
            }
            public class C3 : C1
            {
                public override void M1()
                {
                    Evil.Do();
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("C1"), _cdf.FindType("C2"), _cdf.FindType("C3") };
            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] { _evilDo },
                expectedInjections = new[] {
                        new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(criticaltypes[1], SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(criticaltypes[2], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #20
0
        public void SecuritySafeCriticalGetsAppliedToMethodRequiringPrivilegesItself()
        {
            PrepareTestForAssembly(@"
            public class Test
            {
                public void M1()
                {
                }
            }
            ");

            var testM1 = _cdf.FindMethod("System.Void Test::M1()");

            var ptd = new PropagateTestDescription()
                          {
                              safeCriticalMethods = new[] { testM1 },
                              methodsRequiringPrivileges = new[] { testM1 },
                              expectedInjections = new[] {
                                                             new CecilSecurityAttributeDescriptor(testM1, SecurityAttributeType.SafeCritical)
                                                         }
                          };

            PropagateAndAssert(ptd);
        }
コード例 #21
0
 private void PropagateAndAssert(PropagateTestDescription ptd)
 {
     var propagation = new MethodPrivilegePropagation(new[] { _assembly }, ptd.methodsRequiringPrivileges, ptd.safeCriticalMethods, ptd.criticalTypes, ptd.callsToIgnore);
     var report = propagation.CreateReportBuilder().Build();
     var injections = report.GetInjectionsFor(_assembly).ToList();
     CollectionAssert.AreEquivalent(ptd.expectedInjections.ToList(), injections);
 }
コード例 #22
0
        public void Method_In_Transparent_Type_That_Does_Evil_Causes_Overriden_Method_To_Be_SC_Even_If_That_Method_Lives_In_A_Critical_Type()
        {
            PrepareTestForAssembly(@"

            public class TransparantType
            {
                public virtual void M1()
                {
                    Evil.Do();
                }
            }
            public class CriticalType : TransparantType
            {
                public override void M1()
                {
                }
            }
            ");

            var criticaltypes = new[] { _cdf.FindType("CriticalType") };
            var tm1 = _cdf.FindMethod("System.Void TransparantType::M1()");
            var cm1 = _cdf.FindMethod("System.Void CriticalType::M1()");
            var ptd = new PropagateTestDescription()
            {
                criticalTypes = criticaltypes,
                methodsRequiringPrivileges = new[] {_evilDo},
                expectedInjections = new[] {
                        new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(tm1, SecurityAttributeType.Critical),
                        new CecilSecurityAttributeDescriptor(cm1, SecurityAttributeType.Critical),  //<-- test is mostly about this one.
                        new CecilSecurityAttributeDescriptor(criticaltypes[0], SecurityAttributeType.Critical),
                }
            };

            PropagateAndAssert(ptd);
        }
コード例 #23
0
        public void IgnoredCallsGetRespected()
        {
            PrepareTestForAssembly(@"

            public class Test
            {
                public void M1()
                {
                    Evil.Do();
                }
            }
            ");

            var ptd = new PropagateTestDescription()
                          {
                              methodsRequiringPrivileges = new[] {_evilDo},
                              callsToIgnore = new[]
                                                  {
                                                      new MethodToMethodCall(_cdf.FindMethod("System.Void Test::M1()"), _evilDo)
                                                  },
                              expectedInjections = new[] {
                                                             new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical)
                                                         }
                          };

            PropagateAndAssert(ptd);
        }
コード例 #24
0
        public void SingleAssemblyWithCriticalType()
        {
            PrepareTestForAssembly(@"

            public interface IMyEnumerator
            {
                void M1();
            }

            public class CriticalType : IMyEnumerator
            {
                public void M1()
                {
                    Evil.Do();
                }
            }

            ");
            var criticaltype = _cdf.FindType("CriticalType");

            var expected = new[] {
                new CecilSecurityAttributeDescriptor(criticaltype, SecurityAttributeType.Critical),
                new CecilSecurityAttributeDescriptor(_evilDo, SecurityAttributeType.Critical)
            };

            var prd = new PropagateTestDescription()
                          {
                              criticalTypes = new[] {criticaltype},
                              methodsRequiringPrivileges = new[] {_evilDo},
                              expectedInjections = expected
                          };
            PropagateAndAssert(prd);
        }