|
| 1 | +local utils = require("utils") |
| 2 | +local skynet_access = require("skynet.access") |
| 3 | +local skynet_utils = require("skynet.utils") |
| 4 | + |
| 5 | +describe("match_allowed_internal_networks", function() |
| 6 | + it("should return true for addresses from 127.0.0.0/8 host network", function() |
| 7 | + -- start and end of the range |
| 8 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.0.0.0")) |
| 9 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.255.255.255")) |
| 10 | + -- random addresses from the network |
| 11 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.0.0.1")) |
| 12 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.10.0.100")) |
| 13 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.99.210.3")) |
| 14 | + assert.is_true(skynet_access.match_allowed_internal_networks("127.210.20.99")) |
| 15 | + end) |
| 16 | + |
| 17 | + it("should return true for addresses from 10.0.0.0/8 private network", function() |
| 18 | + -- start and end of the range |
| 19 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.0.0.0")) |
| 20 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.255.255.255")) |
| 21 | + -- random addresses from the network |
| 22 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.10.1.0")) |
| 23 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.10.10.10")) |
| 24 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.87.10.120")) |
| 25 | + assert.is_true(skynet_access.match_allowed_internal_networks("10.210.23.255")) |
| 26 | + end) |
| 27 | + |
| 28 | + it("should return true for addresses from 172.16.0.0/12 private network", function() |
| 29 | + -- start and end of the range |
| 30 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.0.0")) |
| 31 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.255.255")) |
| 32 | + -- random addresses from the network |
| 33 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.1.0")) |
| 34 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.10.10")) |
| 35 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.10.120")) |
| 36 | + assert.is_true(skynet_access.match_allowed_internal_networks("172.16.230.55")) |
| 37 | + end) |
| 38 | + |
| 39 | + it("should return true for addresses from 192.168.0.0/16 private network", function() |
| 40 | + -- start and end of the range |
| 41 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.0.0")) |
| 42 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.255.255")) |
| 43 | + -- random addresses from the network |
| 44 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.1.0")) |
| 45 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.10.10")) |
| 46 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.10.120")) |
| 47 | + assert.is_true(skynet_access.match_allowed_internal_networks("192.168.230.55")) |
| 48 | + end) |
| 49 | + |
| 50 | + it("should return false for addresses from outside of allowed networks", function() |
| 51 | + assert.is_false(skynet_access.match_allowed_internal_networks("8.8.8.8")) |
| 52 | + assert.is_false(skynet_access.match_allowed_internal_networks("16.12.0.1")) |
| 53 | + assert.is_false(skynet_access.match_allowed_internal_networks("115.23.44.17")) |
| 54 | + assert.is_false(skynet_access.match_allowed_internal_networks("198.19.0.20")) |
| 55 | + assert.is_false(skynet_access.match_allowed_internal_networks("169.254.1.1")) |
| 56 | + assert.is_false(skynet_access.match_allowed_internal_networks("212.32.41.12")) |
| 57 | + end) |
| 58 | +end) |
| 59 | + |
| 60 | +describe("should_block_access", function() |
| 61 | + local remote_addr = "127.0.0.1" |
| 62 | + |
| 63 | + before_each(function() |
| 64 | + stub(utils, "getenv") |
| 65 | + stub(skynet_access, "match_allowed_internal_networks") |
| 66 | + end) |
| 67 | + |
| 68 | + after_each(function() |
| 69 | + mock.revert(utils) |
| 70 | + mock.revert(skynet_access) |
| 71 | + end) |
| 72 | + |
| 73 | + it("should not block access if DENY_PUBLIC_ACCESS is not set", function() |
| 74 | + utils.getenv.on_call_with("DENY_PUBLIC_ACCESS").returns(nil) |
| 75 | + |
| 76 | + assert.is_false(skynet_access.should_block_access(remote_addr)) |
| 77 | + end) |
| 78 | + |
| 79 | + it("should not block access if DENY_PUBLIC_ACCESS is set to false", function() |
| 80 | + utils.getenv.on_call_with("DENY_PUBLIC_ACCESS").returns(false) |
| 81 | + |
| 82 | + assert.is_false(skynet_access.should_block_access(remote_addr)) |
| 83 | + end) |
| 84 | + |
| 85 | + it("should not block access if DENY_PUBLIC_ACCESS is set to true but request is from internal network", function() |
| 86 | + utils.getenv.on_call_with("DENY_PUBLIC_ACCESS").returns(true) |
| 87 | + skynet_access.match_allowed_internal_networks.on_call_with(remote_addr).returns(true) |
| 88 | + |
| 89 | + assert.is_false(skynet_access.should_block_access(remote_addr)) |
| 90 | + end) |
| 91 | + |
| 92 | + it("should block access if DENY_PUBLIC_ACCESS is set to true and request is not from internal network", function() |
| 93 | + utils.getenv.on_call_with("DENY_PUBLIC_ACCESS", "boolean").returns(true) |
| 94 | + skynet_access.match_allowed_internal_networks.on_call_with(remote_addr).returns(false) |
| 95 | + |
| 96 | + assert.is_true(skynet_access.should_block_access(remote_addr)) |
| 97 | + end) |
| 98 | +end) |
| 99 | + |
| 100 | +describe("exit_public_access_forbidden", function() |
| 101 | + before_each(function() |
| 102 | + stub(skynet_utils, "exit") |
| 103 | + end) |
| 104 | + |
| 105 | + after_each(function() |
| 106 | + mock.revert(skynet_utils) |
| 107 | + end) |
| 108 | + |
| 109 | + it("should call exit with status code 403 and proper message", function() |
| 110 | + skynet_access.exit_public_access_forbidden() |
| 111 | + |
| 112 | + assert.stub(skynet_utils.exit).was_called_with(403, "Server public access denied") |
| 113 | + end) |
| 114 | +end) |
0 commit comments