|
| 1 | +import test from "node:test" |
| 2 | +import assert from "node:assert/strict" |
| 3 | + |
| 4 | +import { CONSOLE_SCOPES } from "../../lib/console-permissions" |
| 5 | +import { hasConsolePermission, type ConsolePolicy } from "../../lib/console-policy-parser" |
| 6 | + |
| 7 | +const maintainerPolicy: ConsolePolicy = { |
| 8 | + Version: "2012-10-17", |
| 9 | + Statement: [ |
| 10 | + { |
| 11 | + Effect: "Allow", |
| 12 | + Action: ["s3:*"], |
| 13 | + Resource: ["arn:aws:s3:::*"], |
| 14 | + }, |
| 15 | + { |
| 16 | + Effect: "Deny", |
| 17 | + Action: ["s3:DeleteObject"], |
| 18 | + Resource: ["arn:aws:s3:::*"], |
| 19 | + }, |
| 20 | + ], |
| 21 | +} |
| 22 | + |
| 23 | +test("a denied browser capability does not hide the Browser page", () => { |
| 24 | + assert.equal(hasConsolePermission(maintainerPolicy, CONSOLE_SCOPES.VIEW_BROWSER), true) |
| 25 | +}) |
| 26 | + |
| 27 | +test("a denied browser capability remains unavailable", () => { |
| 28 | + assert.equal(hasConsolePermission(maintainerPolicy, "s3:DeleteObject", "arn:aws:s3:::test/object.txt"), false) |
| 29 | +}) |
| 30 | + |
| 31 | +test("an explicit Browser scope deny hides the Browser page", () => { |
| 32 | + const policy: ConsolePolicy = { |
| 33 | + ...maintainerPolicy, |
| 34 | + Statement: [ |
| 35 | + ...maintainerPolicy.Statement, |
| 36 | + { |
| 37 | + Effect: "Deny", |
| 38 | + Action: [CONSOLE_SCOPES.VIEW_BROWSER], |
| 39 | + Resource: ["console"], |
| 40 | + }, |
| 41 | + ], |
| 42 | + } |
| 43 | + |
| 44 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_BROWSER), false) |
| 45 | +}) |
| 46 | + |
| 47 | +test("a Console scope wildcard deny hides the Browser page", () => { |
| 48 | + const policy: ConsolePolicy = { |
| 49 | + ...maintainerPolicy, |
| 50 | + Statement: [ |
| 51 | + ...maintainerPolicy.Statement, |
| 52 | + { |
| 53 | + Effect: "Deny", |
| 54 | + Action: ["console:*"], |
| 55 | + Resource: ["console"], |
| 56 | + }, |
| 57 | + ], |
| 58 | + } |
| 59 | + |
| 60 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_BROWSER), false) |
| 61 | +}) |
| 62 | + |
| 63 | +test("a global S3 wildcard deny hides the Browser page", () => { |
| 64 | + const policy: ConsolePolicy = { |
| 65 | + Version: "2012-10-17", |
| 66 | + Statement: [ |
| 67 | + { |
| 68 | + Effect: "Allow", |
| 69 | + Action: ["s3:*"], |
| 70 | + Resource: ["*"], |
| 71 | + }, |
| 72 | + { |
| 73 | + Effect: "Deny", |
| 74 | + Action: ["s3:*"], |
| 75 | + Resource: ["arn:aws:s3:::*"], |
| 76 | + }, |
| 77 | + ], |
| 78 | + } |
| 79 | + |
| 80 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_BROWSER), false) |
| 81 | +}) |
| 82 | + |
| 83 | +test("a bucket-scoped S3 wildcard deny does not hide the Browser page", () => { |
| 84 | + const policy: ConsolePolicy = { |
| 85 | + Version: "2012-10-17", |
| 86 | + Statement: [ |
| 87 | + { |
| 88 | + Effect: "Allow", |
| 89 | + Action: ["s3:*"], |
| 90 | + Resource: ["arn:aws:s3:::*"], |
| 91 | + }, |
| 92 | + { |
| 93 | + Effect: "Deny", |
| 94 | + Action: ["s3:*"], |
| 95 | + Resource: ["arn:aws:s3:::restricted/*"], |
| 96 | + }, |
| 97 | + ], |
| 98 | + } |
| 99 | + |
| 100 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_BROWSER), true) |
| 101 | +}) |
| 102 | + |
| 103 | +test("an admin wildcard deny hides the Users page", () => { |
| 104 | + const policy: ConsolePolicy = { |
| 105 | + Version: "2012-10-17", |
| 106 | + Statement: [ |
| 107 | + { |
| 108 | + Effect: "Allow", |
| 109 | + Action: ["admin:*"], |
| 110 | + }, |
| 111 | + { |
| 112 | + Effect: "Deny", |
| 113 | + Action: ["admin:*"], |
| 114 | + }, |
| 115 | + ], |
| 116 | + } |
| 117 | + |
| 118 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_USERS), false) |
| 119 | +}) |
| 120 | + |
| 121 | +test("multiple denies that cover all Users capabilities hide the page", () => { |
| 122 | + const policy: ConsolePolicy = { |
| 123 | + Version: "2012-10-17", |
| 124 | + Statement: [ |
| 125 | + { |
| 126 | + Effect: "Allow", |
| 127 | + Action: ["admin:*"], |
| 128 | + }, |
| 129 | + { |
| 130 | + Effect: "Deny", |
| 131 | + Action: ["admin:ListUsers", "admin:CreateUser", "admin:GetUser"], |
| 132 | + }, |
| 133 | + { |
| 134 | + Effect: "Deny", |
| 135 | + Action: ["admin:EnableUser", "admin:DisableUser", "admin:DeleteUser"], |
| 136 | + }, |
| 137 | + ], |
| 138 | + } |
| 139 | + |
| 140 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_USERS), false) |
| 141 | +}) |
| 142 | + |
| 143 | +test("a total deny for one service does not hide a mixed-service page", () => { |
| 144 | + const policy: ConsolePolicy = { |
| 145 | + Version: "2012-10-17", |
| 146 | + Statement: [ |
| 147 | + { |
| 148 | + Effect: "Allow", |
| 149 | + Action: ["kms:*"], |
| 150 | + }, |
| 151 | + { |
| 152 | + Effect: "Deny", |
| 153 | + Action: ["admin:*"], |
| 154 | + }, |
| 155 | + ], |
| 156 | + } |
| 157 | + |
| 158 | + assert.equal(hasConsolePermission(policy, CONSOLE_SCOPES.VIEW_SSE_SETTINGS), true) |
| 159 | +}) |
0 commit comments