Code Monkey home page Code Monkey logo

raise-if-root's Introduction

Raise If Root

Gem Version Build status Code Climate Inline Docs

Raise If Root is a small gem that helps prevent your application from ever running as the root user (uid 0).

Why?

Many software systems rely on user privilege separation for security reasons. Especially within containers or chroots, running as a non-privileged user gives stronger isolation.

Raise If Root helps enforce that you never inadvertently load your application code as root.

Will it protect you if your attacker is already running as root? Probably not. But it does help remove opportunities for error, where you might accidentally run root rake tasks, cron jobs, or deploy scripts.

Usage

Add the gem to your application's Gemfile:

gem 'raise-if-root', '~> 0'

Require it from your main application code:

require 'raise-if-root'

There is no step three! This will raise RaiseIfRoot::AssertionFailed if the current uid is 0.

More complex patterns

See the YARD documentation.

If you want to enforce that the application is running as a particular user, there are several more specific functions available.

Load the library, which doesn't immediately raise when you load it.

# load the library, which doesn't raise
require 'raise-if-root/library'

# raise if running as uid 1000
RaiseIfRoot.raise_if_uid(1000)

# raise unless user is nobody
RaiseIfRoot.raise_if(username_not: 'nobody')

# raise with multiple conditions
RaiseIfRoot.raise_if(uid_not: 1000, gid_not: 500)

Notification callbacks

If you want to sound the alarm with something more than just the exception, you can add callbacks to send emails, smoke signals, etc.

# load the library, which doesn't raise
require 'raise-if-root/library'

RaiseIfRoot.add_assertion_callback do |err|
  Mail.deliver do
    from    '[email protected]'
    to      '[email protected]'
    subject 'App was run as root'
    body    "RaiseIfRoot is raising an exception:\n  #{err.inspect}\n"
  end
end

# ensure we're not root
RaiseIfRoot.raise_if_root

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

raise-if-root's People

Contributors

ab avatar

Stargazers

Alberto Colón Viera avatar Alberto Colón Viera avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.