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
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -127,11 +127,20 @@ public with sharing virtual class FinalizerHandler {
) {
Object dynamicInstance;
String className = finalizerMetadata.Apex_Class_Name__c;
String namespace = finalizerMetadata.Apex_Class_Namespace__c;
if (FinalizerHandler.isBypassed(className)) {
return;
}

try {
dynamicInstance = Type.forName(className).newInstance();
try {
Copy link
Owner

Choose a reason for hiding this comment

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

Double nested try blocks?

dynamicInstance = Type.forName(namespace, className).newInstance();
} catch (Exception e) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we catch an explicit type of exception here?

// fallback to package namespace to not break existing implementations that are distributed in a namespace
String[] parts = String.valueOf(FinalizerHandler.class).split('\\.', 2);
namespace = parts.size() == 2 ? parts[0] : '';
dynamicInstance = Type.forName(namespace, className).newInstance();
}
} catch (System.NullPointerException e) {
handleFinalizerException(INVALID_CLASS_ERROR_FINALIZER, className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,',
Expand Down Expand Up @@ -385,8 +386,15 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem
for (Trigger_Action__mdt triggerMetadata : actionMetadata) {
Object triggerAction;
try {
triggerAction = Type.forName(triggerMetadata.Apex_Class_Name__c)
.newInstance();
String namespace = triggerMetadata.Apex_Class_Namespace__c;
try {
Copy link
Owner

@mitchspano mitchspano Jul 1, 2024

Choose a reason for hiding this comment

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

Double nested try blocks?

triggerAction = Type.forName(namespace, triggerMetadata.Apex_Class_Name__c).newInstance();
} catch (Exception e) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we catch an explicit type of exception here?

// fallback to package namespace to not break existing implementations that are distributed in a namespace
String[] parts = String.valueOf(MetadataTriggerHandler.class).split('\\.', 2);
namespace = parts.size() == 2 ? parts[0] : '';
triggerAction = Type.forName(namespace, triggerMetadata.Apex_Class_Name__c).newInstance();
}
if (triggerMetadata.Flow_Name__c != null) {
((TriggerActionFlow) triggerAction)
.flowName = triggerMetadata.Flow_Name__c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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>