Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
A promise library for Salesforce.
This was inspired by a 2016 Dreamforce Sessions Apex Promises by Kevin Poorman. I thought it would be fun to take it a step further and see how close you could get to a reusable "Promise" implementation.
Resolve
function is passed into the nextFor Q's without Callouts, Inner-Classes and Non-Serializable types can be used. The Q Library will chain these without using the future method. Below is a trivial example Encypts and Base64 encodes an Account Number field:
public class EnycriptAccountNumber{
public EnycriptAccountNumber(Account acc){
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob key = Crypto.generateAesKey(128);
new Q(new EncryptionAction(exampleIv, key))
.then(new Base64EncodingAction())
.then(new ExceptionAction(false)) //set to true to see error handling
.error(new ErrorHandler(acc))
.done(new DoneHandler(acc))
.execute(Blob.valueOf(acc.AccountNumber));
}
//=== ACTION Handlers ===
private class EncryptionAction implements Q.Action{
private Blob vector;
private Blob key;
public EncryptionAction(Blob vector, Blob key){
this.vector = vector;
this.key = key;
}
public Object resolve(Object input){
Blob inputBlob = (Blob) input;
return Crypto.encrypt('AES128', key, vector, inputBlob);
}
}
private class Base64EncodingAction implements Q.Action {
public Object resolve(Object input){
Blob inputBlob = (Blob) input;
return EncodingUtil.base64Encode(inputBlob);
}
}
private class ExceptionAction implements Q.Action {
private Boolean throwException;
public ExceptionAction(Boolean throwException){
this.throwException = throwException;
}
public Object resolve(Object input){
if(throwException){
System.debug(100/0);
}
return input;
}
}
//=== Done Handler ===
private class DoneHandler implements Q.Done{
private Account acc;
public DoneHandler(Account acc){
this.acc = acc;
}
public void done(Object input){
if(input != null){
acc.AccountNumber = (String) input;
insert acc;
System.debug(acc);
}
}
}
//=== Error Handler ===
private class ErrorHandler implements Q.Error{
private Account acc;
public ErrorHandler(Account acc){
this.acc = acc;
}
//failed! set account number to null
public Object error(Exception e){
//do stuff with exception
System.debug(e.getMessage());
//return object for done
acc.AccountNumber = null;
return acc;
}
}
}
The most common use case for a pattern like this would probably be to chain multiple Callout actions. Unforuntely, due to the lack of proper reflection in Salesforce, the implementation here is less than ideal and rules must be followed:
QFuture.TypedSerializable
To Specify a Promise with callouts, just use QFuture
in place of Q
:
public class EnycriptAccountNumber{
//ALL Promise Implementation Classes defined at top level!
public EnycriptAccountNumber(Account acc){
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob key = Crypto.generateAesKey(128);
new QFuture(new EncryptionAction(exampleIv, key))
.then(new Base64EncodingAction())
.then(new ExceptionAction(false)) //set to true to see error handling
.error(new ErrorHandler(acc))
.done(new DoneHandler(acc))
.execute(Blob.valueOf(acc.AccountNumber));
}
}
//TOP LEVEL CLASS!
public with sharing class EncryptionAction implements Q.Action{
private Blob vector;
private Blob key;
public EncryptionAction(Blob vector, Blob key){
this.vector = vector;
this.key = key;
}
public QFuture.TypedSerializable resolve(Object input){
Blob inputBlob = (Blob) input;
return new QFuture.TypedSerializable(Crypto.encrypt('AES128', key, vector, inputBlob),
Blob.class);
}
}
//... Rest of implementations. (Also top level)
IMPLEMENT AT YOUR OWN RISK. I have not use this in an actual implementation. Has not been throughly tested. Needs Unit Testing
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A Salesforce Promise-ish library
We found that apex-q demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.