Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Command-chain is an algorithm based on the command patterns that allows commands to be chained. It is used to divide large actions into smaller ones and automatically undo them when something goes wrong.
Each Chain consists of a series of sequential commands. So, given a sequence A→B→C→D→E, the Chain executes A, then B, then C, and so on... If one fails, the Chain will undo every command in reverse order when they are undoable. So if D fails, then the Chain undo C, then undo B, and so on.
The main reason why this gem was created is to show how using lambdas can produce a cleaner code than using service objects.
Install the gem and add to the application's Gemfile by executing:
$ bundle add command_chain
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install command_chain
To use the CommandChain, first, you must create the lambdas/procs responsible for the 'do' and 'undo' actions. Second, make the Chain with an Array of Arrays. If a command does not need an undo action, use an Array with just an element or nil as the second element.
do1 = lambda {|obj| puts "first execution"}
undo1 = lambda {|obj| puts "third execution"}
do2 = lambda {|obj| puts "second execution"}
do3 = lambda {|obj| raise CommandChain::Error.new("error")}
undo3 = lambda {|obj| puts "will not execute"}
do4 = lambda {|obj| puts "will not execute"}
undo4 = lambda {|obj| puts "will not execute"}
CommandChain::Chain.new(
[
[do1, undo1],
[do2, nil],
[do3, undo3],
[do4, undo4],
]
).execute
There are two ways that a Command can receive variables; one is implicit because the commands are closures, and the second is through a variable that the closure gets as an argument.
add_taxes = lambda do |order|
order.tax = 0.1
end
calculate_total = lambda do |order|
order.total_price = order.base_price + order.base_price * order.tax
end
Order = Struct.new('Order', :base_price, :tax, :total_price)
order = Order.new
order.base_price = 120.0
CommandChain::Chain.new(
[
[add_taxes, nil],
[calculate_total, nil]
]
).execute(order)
puts order.total_price # => 132.0
After checking out the repo, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://bitbucket.org/fredlinhares/command_chain.
FAQs
Unknown package
We found that command_chain 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.