|
| 1 | +--- permissions manager. |
| 2 | +-- |
| 3 | +-- # Capabilities |
| 4 | +-- |
| 5 | +-- # Usage |
| 6 | +-- |
| 7 | +-- * Add `require "permissions_manager"` to your `config.rc`. |
| 8 | +-- * Copy example of permissions.cfg to ~/.local/share/luakit/permissions.cfg |
| 9 | +-- * Edit its content according to your needs |
| 10 | +-- |
| 11 | +-- # Troubleshooting |
| 12 | +-- |
| 13 | +-- # Files and Directories |
| 14 | +-- |
| 15 | +-- @module permissions_manager |
| 16 | +-- @author Serg Kozhemyakin <serg.kozhemyakin@gmail.com> |
| 17 | +-- @copyright 2017-2018 Serg Kozhemyakin <serg.kozhemyakin@gmail.com> |
| 18 | + |
| 19 | +local webview = require ("webview") |
| 20 | +local lousy = require("lousy") |
| 21 | + |
| 22 | +local function to_grant_or_not_to_grant(cfg, uri) |
| 23 | + local grant = nil |
| 24 | + local scheme = uri.scheme |
| 25 | + local host = uri.host |
| 26 | + local port = uri.port |
| 27 | + local default = false |
| 28 | + |
| 29 | + if cfg then |
| 30 | + default = cfg['default'] or 'denied' -- by default deny or requests |
| 31 | + local key |
| 32 | + if default == 'allowed' then |
| 33 | + default = true |
| 34 | + key = 'denied' |
| 35 | + else |
| 36 | + default = false |
| 37 | + key = 'allowed' |
| 38 | + end |
| 39 | + cfg = cfg[key] |
| 40 | + if not cfg then return grant end |
| 41 | + |
| 42 | + local origin = scheme.."://"..host..":"..port; |
| 43 | + for _,v in ipairs(cfg) do |
| 44 | + if string.match(origin, v) then |
| 45 | + grant = not default |
| 46 | + break |
| 47 | + end |
| 48 | + if grant ~= nil then break end |
| 49 | + end |
| 50 | + end |
| 51 | + -- check for default value if there's no host specific value |
| 52 | + return grant == nil and default or grant |
| 53 | +end |
| 54 | + |
| 55 | +local function check_permission_request(view, what, params) |
| 56 | + local grant = nil |
| 57 | + local uri = lousy.uri.parse(view.uri) |
| 58 | + msg.info("Checking permission '"..what.."' for '"..uri.scheme.."://"..uri.host..":"..uri.port) |
| 59 | + |
| 60 | + local config = luakit.data_dir .. '/permissions.cfg' |
| 61 | + msg.info("Loading config file '"..config.."'") |
| 62 | + |
| 63 | + local f,r = io.open(config, "r") |
| 64 | + if f then |
| 65 | + local cfg = f:read("*all") |
| 66 | + local permissions = nil |
| 67 | + local code, message = loadstring("return "..cfg) |
| 68 | + f:close() |
| 69 | + if message then |
| 70 | + msg.error("Loading cfg failed: "..message) |
| 71 | + return false |
| 72 | + else |
| 73 | + permissions = code() |
| 74 | + end |
| 75 | + if not permissions or not permissions[what] then |
| 76 | + msg.warn("There's no key '"..what.."' in loaded permissions") |
| 77 | + return false |
| 78 | + end |
| 79 | + permissions = permissions[what] |
| 80 | + if params then |
| 81 | + if type(params) == 'table' then |
| 82 | + for k,_ in pairs(params) do |
| 83 | + grant = to_grant_or_not_to_grant(permissions[params][k], uri) |
| 84 | + if grant ~= nil and not grant then break end |
| 85 | + end |
| 86 | + else |
| 87 | + grant = to_grant_or_not_to_grant(permissions[params], uri) |
| 88 | + end |
| 89 | + else |
| 90 | + grant = to_grant_or_not_to_grant(permissions, uri) |
| 91 | + end |
| 92 | + else |
| 93 | + msg.error("Failed to open config file: "..r) |
| 94 | + end |
| 95 | + |
| 96 | + grant = grant == nil and false or grant |
| 97 | + msg.info("Permission '"..what.."' for '"..uri.scheme.."://"..uri.host..":"..uri.port.." was "..(grant and "granted" or "denied")) |
| 98 | + return grant |
| 99 | +end |
| 100 | + |
| 101 | +webview.add_signal("init", function (view) |
| 102 | + view:add_signal("permission-request", check_permission_request) |
| 103 | +end) |
| 104 | + |
| 105 | +-- vim: et:sw=4:ts=8:sts=4:tw=80 |
0 commit comments