
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
= Handshake
Handshake is an informal AOP and design-by-contract system written in pure Ruby. It's intended to allow Ruby developers to apply simple, clear constraints to their methods and classes.
=== Features
=== Examples
Here's an example of Handshake in action on a hypothetical BankAccount class:
class BankAccount attr_reader :balance
class << self
def less_than_balance?
all? positive_number?, clause {|n| n <= balance}
end
end
invariant { balance >= 0 }
contract positive_number? => anything
def initialize(balance)
@balance = balance
end
contract less_than_balance? => positive_number?
def withdraw(amount)
new_balance = @balance - amount
@balance = new_balance
return new_balance
end
end
Here's an example that uses an invariant to enforce a constraint on a subclass of Array:
class NonEmptyArray < Array include Handshake invariant { not empty? } end
Further specializing the subclass:
class NonEmptyStringArray < NonEmptyArray contract :initialize, [[ String ]] => anything contract :<<, String => self contract :+, many?(String) => self contract :each, Block(String => anything) => self end
Handshake can also define pre- and post-conditions on your methods.
class Foo before do assert( not @widget.nil? ) end def something_that_requires_widget ... end end
See Handshake::ClassMethods for more documentation on exact syntax and capabilities. Handshake::ClauseMethods contains a number of helper and combinator clauses for defining contract signatures.
=== Caveats
Handshake works by wrapping any class that includes it with a proxy object that performs the relevant contract checks. It acts as a barrier between an object and its callers. Unfortunately, this means that internal calls, for example to private methods, that do not pass across this barrier, are unchecked. Here's an example:
class UncheckedCall include Handshake
contract String => Numeric
def checked_public(str); str.to_i; end
def checked_public_delegates(str)
checked_private(str)
end
private
contract String => Numeric
def checked_private(str); str.to_i; end
end
In this example, we have a public checked method protected by a contract. Any external call to this method will be checked. The method marked as checked_public_delegates calls a private method that is itself protected by a contract. But because the call to that private method is internal, and does not pass across the contract barrier, no contract will be applied.
You can get around this problem by calling private methods on the special private method +checked_self+:
class UncheckedCall ... def checked_public_delegates(str) checked_self.checked_private(str) end ... end
=== License (MIT)
Copyright (c) 2010 Brian Guthrie
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
Unknown package
We found that handshake 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
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.