|
| 1 | +const express = require('express'); |
| 2 | +const TestObj = require('@example/test'); |
| 3 | + |
| 4 | +function basicTaint() { |
| 5 | + const app = express(); |
| 6 | + app.use((req, res, next) => { |
| 7 | + req.tainted = source(); |
| 8 | + req.safe = 'safe'; |
| 9 | + next(); |
| 10 | + }); |
| 11 | + app.get('/', (req, res) => { |
| 12 | + sink(req.tainted); // NOT OK |
| 13 | + sink(req.safe); // OK |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +function basicApiGraph() { |
| 18 | + const app = express(); |
| 19 | + app.use((req, res, next) => { |
| 20 | + req.obj = new TestObj(); |
| 21 | + next(); |
| 22 | + }); |
| 23 | + app.get('/', (req, res) => { |
| 24 | + sink(req.obj.getSource()); // NOT OK |
| 25 | + req.obj.getSink(source()); // NOT OK |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function noTaint() { |
| 30 | + const app = express(); |
| 31 | + function middleware(req, res, next) { |
| 32 | + req.tainted = source(); |
| 33 | + req.safe = 'safe'; |
| 34 | + next(); |
| 35 | + } |
| 36 | + app.get('/unsafe', middleware, (req, res) => { |
| 37 | + sink(req.tainted); // NOT OK |
| 38 | + sink(req.safe); // OK |
| 39 | + }); |
| 40 | + app.get('/safe', (req, res) => { |
| 41 | + sink(req.tainted); // OK - not preceded by middleware |
| 42 | + sink(req.safe); // OK |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +function looseApiGraphStep() { |
| 47 | + const app = express(); |
| 48 | + function middleware(req, res, next) { |
| 49 | + req.obj = new TestObj(); |
| 50 | + next(); |
| 51 | + } |
| 52 | + app.get('/unsafe', middleware, (req, res) => { |
| 53 | + sink(req.obj.getSource()); // NOT OK |
| 54 | + }); |
| 55 | + app.get('/safe', (req, res) => { |
| 56 | + sink(req.obj.getSource()); // NOT OK - we allow API graph steps within the same app |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function chainMiddlewares() { |
| 61 | + const app = express(); |
| 62 | + function step1(req, res, next) { |
| 63 | + req.taint = source(); |
| 64 | + next(); |
| 65 | + } |
| 66 | + function step2(req, res, next) { |
| 67 | + req.locals.alsoTaint = req.taint; |
| 68 | + next(); |
| 69 | + } |
| 70 | + app.get('/', step1, step2, (req, res) => { |
| 71 | + sink(req.taint); // NOT OK |
| 72 | + sink(req.locals.alsoTaint); // NOT OK |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function routerEscapesIntoParameter() { |
| 77 | + const app = express(); |
| 78 | + function setupMiddlewares(router) { |
| 79 | + router.use((req, res, next) => { |
| 80 | + req.taint = source(); |
| 81 | + next(); |
| 82 | + }); |
| 83 | + } |
| 84 | + app.get('/before', (req, res) => { |
| 85 | + sink(req.taint); // OK |
| 86 | + }); |
| 87 | + setupMiddlewares(app); |
| 88 | + app.get('/after', (req, res) => { |
| 89 | + sink(req.taint); // NOT OK |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +function routerReturned() { |
| 94 | + const app = express(); |
| 95 | + function makeMiddlewares() { |
| 96 | + let router = new express.Router(); |
| 97 | + router.use((req, res, next) => { |
| 98 | + req.taint = source(); |
| 99 | + next(); |
| 100 | + }); |
| 101 | + return router; |
| 102 | + } |
| 103 | + app.get('/before', (req, res) => { |
| 104 | + sink(req.taint); // OK |
| 105 | + }); |
| 106 | + app.use(makeMiddlewares()); |
| 107 | + app.get('/after', (req, res) => { |
| 108 | + sink(req.taint); // NOT OK |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +function routerCaptured() { |
| 113 | + const app = express(); |
| 114 | + function addMiddlewares() { |
| 115 | + app.use((req, res, next) => { |
| 116 | + req.taint = source(); |
| 117 | + next(); |
| 118 | + }); |
| 119 | + } |
| 120 | + app.get('/before', (req, res) => { |
| 121 | + sink(req.taint); // OK |
| 122 | + }); |
| 123 | + addMiddlewares(); |
| 124 | + app.get('/after', (req, res) => { |
| 125 | + sink(req.taint); // NOT OK |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +function withPath() { |
| 130 | + const app = express(); |
| 131 | + app.use('/foo', (req, res, next) => { |
| 132 | + req.taint = source(); |
| 133 | + next(); |
| 134 | + }); |
| 135 | + app.get('/foo', (req, res) => { |
| 136 | + sink(req.taint); // NOT OK |
| 137 | + }); |
| 138 | + app.get('/bar', (req, res) => { |
| 139 | + sink(req.taint); // OK |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +function withNestedPath() { |
| 145 | + const app = express(); |
| 146 | + app.use('/foo/bar', (req, res, next) => { |
| 147 | + req.taint = source(); |
| 148 | + next(); |
| 149 | + }); |
| 150 | + function makeRouter() { |
| 151 | + const router = express.Router(); |
| 152 | + router.get('/bar', (req, res) => { |
| 153 | + sink(req.taint); // NOT OK |
| 154 | + }) |
| 155 | + router.get('/baz', (req, res) => { |
| 156 | + sink(req.taint); // OK |
| 157 | + }) |
| 158 | + return router; |
| 159 | + } |
| 160 | + app.get('/foo', makeRouter()); |
| 161 | + app.get('/bar', (req, res) => { |
| 162 | + sink(req.taint); // OK |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +function withHttpMethods() { |
| 167 | + const app = express(); |
| 168 | + app.get('/foo', (req, res, next) => { |
| 169 | + req.taintGet = source(); |
| 170 | + next(); |
| 171 | + }); |
| 172 | + app.post('/foo', (req, res, next) => { |
| 173 | + req.taintPost = source(); |
| 174 | + next(); |
| 175 | + }); |
| 176 | + app.all('/foo', (req, res, next) => { |
| 177 | + req.taintAll = source(); |
| 178 | + next(); |
| 179 | + }); |
| 180 | + app.get('/foo/a', (req, res) => { |
| 181 | + sink(req.taintGet); // NOT OK |
| 182 | + sink(req.taintPost); // OK - not tainted for GET requests |
| 183 | + sink(req.taintAll); // NOT OK |
| 184 | + }); |
| 185 | + app.post('/foo/b', (req, res) => { |
| 186 | + sink(req.taintGet); // OK - not tainted for POST requests |
| 187 | + sink(req.taintPost); // NOT OK |
| 188 | + sink(req.taintAll); // NOT OK |
| 189 | + }); |
| 190 | + app.all('/foo/c', (req, res) => { |
| 191 | + sink(req.taintGet); // NOT OK |
| 192 | + sink(req.taintPost); // NOT OK |
| 193 | + sink(req.taintAll); // NOT OK |
| 194 | + }); |
| 195 | +} |
| 196 | + |
| 197 | +function withChaining() { |
| 198 | + const app = express(); |
| 199 | + app.use('/', (req, res) => { |
| 200 | + sink(req.taint); // OK |
| 201 | + }); |
| 202 | + function middleware() { |
| 203 | + return express.Router() |
| 204 | + .use((req, res, next) => { |
| 205 | + req.taint = source(); |
| 206 | + next(); |
| 207 | + }) |
| 208 | + .use(blah()); |
| 209 | + } |
| 210 | + app.use(middleware()); |
| 211 | + app.use('/', (req, res) => { |
| 212 | + sink(req.taint); // NOT OK |
| 213 | + }); |
| 214 | +} |
| 215 | + |
| 216 | +function withClass() { |
| 217 | + class App { |
| 218 | + constructor(router) { |
| 219 | + this.router = router; |
| 220 | + this.earlyRoutes(); |
| 221 | + this.addMiddleware(); |
| 222 | + this.lateRoutes(); |
| 223 | + } |
| 224 | + earlyRoutes() { |
| 225 | + this.router.get('/', (req, res) => { |
| 226 | + sink(req.taint); // OK |
| 227 | + }) |
| 228 | + } |
| 229 | + addMiddleware() { |
| 230 | + this.router.use((req, res, next) => { |
| 231 | + req.taint = source(); |
| 232 | + next(); |
| 233 | + }) |
| 234 | + } |
| 235 | + lateRoutes() { |
| 236 | + this.router.get('/', (req, res) => { |
| 237 | + sink(req.taint); // NOT OK |
| 238 | + }) |
| 239 | + } |
| 240 | + } |
| 241 | + let app = new App(express()); |
| 242 | +} |
| 243 | + |
| 244 | +function withArray() { |
| 245 | + const app = express(); |
| 246 | + app.get('/before', (req, res) => { |
| 247 | + sink(req.taint); // OK |
| 248 | + }); |
| 249 | + app.use([ |
| 250 | + (req, res, next) => { |
| 251 | + sink(req.taint); // OK |
| 252 | + next(); |
| 253 | + }, |
| 254 | + (req, res, next) => { |
| 255 | + req.taint = source(); |
| 256 | + next(); |
| 257 | + }, |
| 258 | + (req, res, next) => { |
| 259 | + sink(req.taint); // NOT OK |
| 260 | + next(); |
| 261 | + } |
| 262 | + ]); |
| 263 | + app.get('/after', (req, res) => { |
| 264 | + sink(req.taint); // NOT OK |
| 265 | + }); |
| 266 | +} |
| 267 | + |
| 268 | +function withForLoop() { |
| 269 | + const app = express(); |
| 270 | + let middlewares = [ |
| 271 | + (req, res, next) => { |
| 272 | + sink(req.taint); // OK |
| 273 | + next(); |
| 274 | + }, |
| 275 | + (req, res, next) => { |
| 276 | + req.taint = source(); |
| 277 | + next(); |
| 278 | + }, |
| 279 | + (req, res, next) => { |
| 280 | + sink(req.taint); // NOT OK |
| 281 | + next(); |
| 282 | + } |
| 283 | + ]; |
| 284 | + app.get('/before', (req, res) => { |
| 285 | + sink(req.taint); // OK |
| 286 | + }); |
| 287 | + for (let route of middlewares) { |
| 288 | + app.use(route); |
| 289 | + } |
| 290 | + app.get('/after', (req, res) => { |
| 291 | + sink(req.taint); // NOT OK |
| 292 | + }); |
| 293 | +} |
0 commit comments