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.
Less strictly constitutional and more strictly awesome.
This project was motivated by the desire to take a lot of validation and error handling logic out of our services, and put it in a declarative layer.
Law is a middleware layer for tying together:
Law is framework and transport agnostic. Its focus is on enforcing the business rules specific to your application. In our applications, we have connected these services to REST, websockets, and a programmatic API through the use of adapters. I present below an example that should help to tie in as connect middleware. Hopefully there will be enough information here to make this a useful tool for those interested. If you're having trouble getting things working, let me know.
Our approach to policies was inspired by the filter lifecycle from Ruby on Rails. The argument validations take some ideas from Design By Contract, e.g. preconditions. Applying validators to default names was suggested by @JosephJNK. I am interested to see if this will be conducive to creating a 'ubiquitous language' as described by Eric Evans in Domain Driven Design.
The separation of Express and Connect has influenced our decision to do the same. I think that while frameworks can lead to gains in terms of productivity, a library has greater potential for re-use.
Many thanks to @wearefractal (@amurray, @contra), @JosephJNK, and @uberscientist for conversations and collaboration on application design with functional and declarative programming.
Following are some possible uses of Law. Further options are described in the [extended documentation].
Here's an example service. Law will construct a function from this which will enforce the required/optional parameters. Both optional and required parameters will run any associated validations.
module.exports =
required: ['sessionId']
optional: ['specialKey']
service: ({sessionId, specialKey}, done) ->
# check the sessionId against the database
done null, {role: 'Supreme Commander'}
Here's some example argument types, and their validations. Law makes these available in the definition of services. You can think of them as the language that a particular set of services share. Whenever these names are used in service arguments, their meanings will be enforced by the rules you set here.
redisId = /[a-z0-9]{16}/
mongoId = /[a-f0-9]{24}/
module.exports = [
typeName: 'String'
validation: (arg, assert) ->
assert typeof arg is 'string'
defaultArgs: ['email', 'password', 'sessionId', 'userId']
,
typeName: 'SessionId'
validation: (arg, assert) ->
assert arg.match redisId
defaultArgs: ['sessionId']
,
typeName: 'MongoId'
validation: (arg, assert) ->
assert arg.match mongoId
defaultArgs: ['userId']
]
Here's an example policy file. The filters named here are defined as regular services, but they are run in a slightly different context. If they return an error, the filter stack stops and returns it, otherwise their results are merged into the argument stream and passed on to the next service in the stack.
module.exports =
filterPrefix: 'filters'
rules:
[
{
filters: ['isLoggedIn']
except: [
'getRole'
'login'
]
}
{
filters: ['setIsOnline']
only: ['dashboard']
}
]
Since version 0.1.1 Law supports declarative dependency injection. The two built in loaders are:
This lets us do static analysis of dependencies, and can be used as a way of making side effects explicit.
module.exports =
required: ['sessionId']
dependencies:
services: ['aHelperService']
lib: ['lodash']
service: (args, done, {services, require}) ->
args = lib.lodash.merge {myOpt: 1}, args
services.aHelperService args, done
To add more loaders, just plug in a resolvers object when you load your services:
resolvers = {
myLoader: (name) -> loadIt(name)
}
law.create {services, jargon, policy, resolvers}
Through this approach we accomplish the following:
Because the structure binding these pieces together is declarative, we can easily make it visible for analysis and troubleshooting. Here is a printout from the sample application.
#> console.log law.printFilters services
{ dashboard: [ 'filters/isLoggedIn', 'filters/setIsOnline', 'dashboard' ],
'filters/isLoggedIn':
[ 'sessionId.exists',
'sessionId.isValid(SessionId)',
'filters/isLoggedIn' ],
'filters/setIsOnline': [ 'filters/setIsOnline' ],
getRole: [ 'sessionId.exists', 'sessionId.isValid(SessionId)', 'getRole' ],
login: [ 'login' ] }
npm install law
Before you can start using the facilities mentioned above in your app, you'll need to wire some things up. This being a library intended to support a not-yet-released framework, no assumptions are made about the locations of your files. You'll need something like the following to initialize and connect the services when your application starts.
should = require 'should'
{join} = require 'path'
# lib stuff
connect = require 'connect'
{load, create, printFilters} = require 'law'
# files from the sample app
serviceLocation = join __dirname, '../sample/app/domain/auth/services'
argTypes = require '../sample/app/domain/auth/jargon'
policy = require '../sample/app/domain/auth/policy'
services = load serviceLocation
services = create {services, jargon, policy}
console.log "I am the law:", printFilters services
This gives you a hash of {serviceName, serviceDef}. Now if you wanted to use that, say as connect middleware, you could write up some basic routing like so.
app = connect()
app.use (req, res) ->
# find service to call, or return 404
pathParts = req._parsedUrl.pathname.split('/').remove ''
resourceName = pathParts[0]
service = resourceMap[resourceName] or ->
res.writeHead 404
res.end()
# call service
service {
url: req.url
headers: req.headers
query: req.query || {}
pathParts: pathParts
cookies: req.cookies
}, res
Law provides standard subtypes of Error
, enriched using the tea-error
library (https://github.com/qualiancy/tea-error). This means a properties
object can be attached to the Error
instance upon construction, as well
as an indicator of the function that the stack trace should start from.
We provide a LawError
subtype of Error
that acts as a common parent to
all the more specific error subtypes. Extensions to Law should extend this
type to obtain the benefits of tea-error
, as well as permitting distinction
between instances of Law errors from application errors.
Common parent type of errors within the Law library (and extensions).
Endowed with extra metadata couresty tea-error
.
Unused.
An argument passed to a Law service exists, but has failed a validation. Occurs at call time
The args
argument of a service call was not an instance of object
.
Occurs at call time.
A serviceName that appears in the policy file has no referent amongst the actual discovered services. Occurs when processing the policy file.
A required argument was not present in the args
object passed to the service.
Occurs at call time.
Thrown when a (malformed) rule in the policy file does not have any filters. Occurs at setup time, when applying policy rules to services.
A service definition did not provide a callable (instance of function
).
Occurs when when wrapping services at setup time.
The service definition was neither a function nor a richer service definition object containing metadata and optional Law declarations. Occurs at setup time.
The return value of a service (chained in a computed stack of services) is not an object. Occurs at call time.
A particular dependency in a valid (resolvable) dependency category could not be resolved using the configuration given
A service declared an unresolvable dependency category. For example, if the
resolver configuration doesn't define a way to resolve dependencies in a
services
category, this error would be thrown. Occurs at setup time, when
resolving dependencies.
You can find [extended documentation here].
This should be considered an alpha release. The API may change. It was developed within an active project with the intent to only build the features which give us a clear advantage. Additional features will be added as required for the parent project.
Discussion/feedback is welcome. You can reach me on twitter @qbitmage.
Future goals/possibilities:
(MIT License)
Copyright (c) 2013 Torchlight Software info@torchlightsoftware.com
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
Tools to create policy and validations for services.
The npm package law receives a total of 291 weekly downloads. As such, law popularity was classified as not popular.
We found that law 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.