Created
February 1, 2016 01:52
-
-
Save domgetter/761d06dfdceca1dcee7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db = [{name: "Jim", secret: "I peed my pants.", super_secret: "I love Nickleback."}] | |
user = {name: "Jim"} | |
get_secret = -> user { secret = db.find {|db_user| db_user[:name] == user[:name]}[:secret]; user.merge({secret: secret}) } | |
get_super_secret = -> user { super_secret = db.find {|db_user| db_user[:name] == user[:name]}[:super_secret]; user.merge({super_secret: super_secret}) } | |
bind = -> x, f { x[:secure] ? f[x[:value]].merge({secure: true}) : x } # security-aware manner of calling functions/methods | |
unit = -> n { {secure: false, value: n} } # makes objects security-aware | |
lift = -> f { -> x { unit[f[x]] } } # makes functions/methods security-aware | |
securely_get_secret = lift[get_secret] | |
securely_get_super_secret = lift[get_super_secret] | |
# because of type-mismatch of doing things this way, we cannot do the following | |
securely_get_secret[{secure: true, value: user}] | |
securely_get_super_secret[{secure: true, value: user}] | |
# so we have to | |
bind[bind[{secure: true, value: user}, securely_get_secret], securely_get_super_secret] | |
bind[{secure: false, value: user}, securely_get_super_secret] | |
bind[unit[user], lift[get_secret]] | |
# Be aware that "security-aware" and "secure" are two different things | |
securely_get_secret[user] #=> returns {:secure=>false, :value=>{:name=>"Jim", :secret=>"I peed my pants."}} | |
bind[unit[user], securely_get_secret] #=> {:secure=>false, :value=>{:name=>"Jim"}} | |
# Only the bind function will enforce security rules. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment