Skip to content

Safer type.forName #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -126,12 +126,38 @@ public with sharing virtual class FinalizerHandler {
Context context
) {
Object dynamicInstance;
String namespace = finalizerMetadata.Apex_Class_Namespace__c;
String className = finalizerMetadata.Apex_Class_Name__c;
if (FinalizerHandler.isBypassed(className)) {
return;
}

try {
dynamicInstance = Type.forName(className).newInstance();
System.Type actionType = Type.forName(namespace, className);
/** Type.forName(fullyQualifiedName) allowed some messyness and ambiguity in dealing with namespace
* If config does not provide the correct namespace (likely if upgrading from older versions of this framework) we need to fallback in two scenarios
* - package and class namespaced but namespace wasn't specified
* - namespace is actually in the class field in the form namespace.classname
*/
// try shared Namespace
if( actionType == null ) {
// Get the namespace of the current class.
String[] parts = String.valueOf(MetadataTriggerHandler.class).split('\\.', 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit odd to me. MetadataTriggerHandler class will always have no namespace in the unlocked package.

Is this intended for when someone uses the Trigger Actions Framework within their own Managed package?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or namespace unlocked package, but yes, that is exactly the intent here as that is how we are using the framework internally (in Namepsaced packages)

namespace = parts.size() == 2 ? parts[0] : '';

// try again with the new namespace
actionType = Type.forName(namespace, className);
}
// try namespace in Class_Name field
if (actionType == null) {
String[] parts = className.split('\\.', 2);
if(parts.size() == 2) {
namespace = parts[0];
className = parts[1];
actionType = Type.forName(namespace, className);
}
}
dynamicInstance = actionType.newInstance();
} catch (System.NullPointerException e) {
handleFinalizerException(INVALID_CLASS_ERROR_FINALIZER, className);
}
Original file line number Diff line number Diff line change
@@ -73,6 +73,27 @@ private with sharing class FinalizerHandlerTest {
);
}

@IsTest
private static void ambiguousNamespacedFinalizerShouldExecute() {
// Invalid namespace is the same as a missing one.
// Can't have mixed namespace unit tests so using invalid to trigger the same fallback behavior
handler.allFinalizers = new List<DML_Finalizer__mdt>{
new DML_Finalizer__mdt(
Apex_Class_Namespace__c = 'NOT_A_NAMEPACE',
Apex_Class_Name__c = TEST_FOO_FINALIZER,
Bypass_Permission__c = BYPASS_PERMISSION
)
};

handler.handleDynamicFinalizers();

System.Assert.areEqual(
FOO,
finalizerLedger[0],
'The finalizer should be dynamically instantiated and executed'
);
}

@IsTest
private static void bypassedFinalizerShouldNotExecute() {
handler.allFinalizers = new List<DML_Finalizer__mdt>{
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem
private static final String QUERY_TEMPLATE = String.join(
new List<String>{
'SELECT Apex_Class_Name__c,',
'Apex_Class_Namespace__c,',
'Order__c,',
'Flow_Name__c,',
'Bypass_Permission__c,',
@@ -384,9 +385,34 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem
}
for (Trigger_Action__mdt triggerMetadata : actionMetadata) {
Object triggerAction;
String namespace = triggerMetadata.Apex_Class_Namespace__c;
String className = triggerMetadata.Apex_Class_Name__c;
try {
triggerAction = Type.forName(triggerMetadata.Apex_Class_Name__c)
.newInstance();
System.Type actionType = Type.forName(namespace, className);
/** Type.forName(fullyQualifiedName) allowed some messyness and ambiguity in dealing with namespace
* If config does not provide the correct namespace (likely if upgrading from older versions of this framework) we need to fallback in two scenarios
* - package and class namespaced but namespace wasn't specified
* - namespace is actually in the class field in the form namespace.classname
*/
// try shared Namespace
if( actionType == null ) {
// Get the namespace of the current class.
String[] parts = String.valueOf(MetadataTriggerHandler.class).split('\\.', 2);
namespace = parts.size() == 2 ? parts[0] : '';

// try again with the new namespace
actionType = Type.forName(namespace, className);
}
// try namespace in Class_Name field
if (actionType == null) {
String[] parts = className.split('\\.', 2);
if(parts.size() == 2) {
namespace = parts[0];
className = parts[1];
actionType = Type.forName(namespace, className);
}
}
triggerAction = actionType.newInstance();
if (triggerMetadata.Flow_Name__c != null) {
((TriggerActionFlow) triggerAction)
.flowName = triggerMetadata.Flow_Name__c;
Original file line number Diff line number Diff line change
@@ -737,6 +737,26 @@ private class MetadataTriggerHandlerTest {
System.Assert.areEqual(null, myException, EXCEPTION_SHOULD_NOT_BE_THROWN);
}

@IsTest
private static void actionShouldFallbackIfBadNamespace() {
// Invalid namespace is the same as a missing one.
// Can't have mixed namespace unit tests so using invalid to trigger the same fallback behavior
handler.beforeInsertActionMetadata = new List<Trigger_Action__mdt>{
new Trigger_Action__mdt(
Apex_Class_Namespace__c = 'NOT_A_NAMEPACE',
Apex_Class_Name__c = TEST_BEFORE_INSERT,
Before_Insert__r = setting,
Before_Insert__c = setting.Id,
Order__c = 1,
Bypass_Execution__c = false
)
};

handler.beforeInsert(handler.triggerNew);

System.Assert.isTrue(executed, ACTION_SHOULD_EXECUTE);
}

public class TestBeforeInsert implements TriggerAction.BeforeInsert {
public void beforeInsert(List<SObject> newList) {
MetadataTriggerHandlerTest.executed = true;
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@
<editHeading>true</editHeading>
<label>Finalizer Configuration</label>
<layoutColumns>
<layoutItems>
<behavior>Edit</behavior>
<field>Apex_Class_Namespace__c</field>
</layoutItems>
<layoutItems>
<behavior>Required</behavior>
<field>Apex_Class_Name__c</field>
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@
<editHeading>true</editHeading>
<label>Trigger Action Information</label>
<layoutColumns>
<layoutItems>
<behavior>Edit</behavior>
<field>Apex_Class_Namespace__c</field>
</layoutItems>
<layoutItems>
<behavior>Required</behavior>
<field>Apex_Class_Name__c</field>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Apex_Class_Namespace__c</fullName>
<description>Enter the apex class namespace</description>
<externalId>false</externalId>
<fieldManageability>SubscriberControlled</fieldManageability>
<inlineHelpText>Enter the apex class namespace</inlineHelpText>
<label>Apex Class Namespace</label>
<length>15</length>
<required>false</required>
<type>Text</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Apex_Class_Namespace__c</fullName>
<description>Enter the apex class namespace</description>
<externalId>false</externalId>
<fieldManageability>SubscriberControlled</fieldManageability>
<inlineHelpText>Enter the apex class namespace</inlineHelpText>
<label>Apex Class Namespace</label>
<length>15</length>
<required>false</required>
<type>Text</type>
<unique>false</unique>
</CustomField>