All files index.js

100% Statements 415/415
100% Branches 41/41
100% Functions 4/4
100% Lines 415/415

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 4161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 848x 848x 848x 848x 848x 848x 424x 424x 424x 848x 4x 4x 882x 882x 882x 882x 882x 882x 882x 882x 440x 440x 440x 440x 442x 442x 438x 438x 438x 4x 4x 4x 4x 4x 878x 878x 882x 2x 2x 2x 876x 876x 876x 876x 882x 4x 4x 4x 872x 872x 882x 434x 434x 434x 434x 4x 4x 4x 430x 430x 434x 216x 216x 216x 216x 2x 2x 2x 216x 428x 428x 434x 4x 4x 4x 424x 424x 424x 424x 424x 1068x 1068x 1068x 1068x 424x 424x 424x 424x 424x 424x 424x 424x 424x 424x 424x 424x 424x 438x 438x 434x 434x 434x 434x 4x 4x 4x 430x 430x 434x 216x 216x 216x 216x 2x 2x 2x 216x 428x 428x 434x 4x 4x 4x 424x 424x 424x 424x 1068x 1068x 1068x 1068x 424x 424x 424x 424x 424x 424x 424x 644x 644x 644x 644x 424x 424x 424x 424x 424x 424x 424x 4x 4x 4x 4x 4x 4x 4x 4x 1x  
/*
# txf   [![Build Status](https://github.com/davedoesdev/txf/actions/workflows/ci.yml/badge.svg)](https://github.com/davedoesdev/txf/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/davedoesdev/txf/badge.png?branch=master&service=github)](https://coveralls.io/r/davedoesdev/txf?branch=master) [![NPM version](https://badge.fury.io/js/txf.png)](http://badge.fury.io/js/txf)
 
File transfer service which pipes PUT requests to matching GET requests.
 
- Uses shared secrets to make request paths unguessable.
- Doesn't store data.
 
The API is described [here](#api).
 
## Example
 
Create a `txf` service on port 8000:
 
```javascript
var server = require('http').createServer();
server.listen(8000);
require('txf')(server, { test: { sender: '', receiver: '' } });
```
 
Send data to the service:
 
```shell
$ echo 'some data' | curl -T - http://localhost:8000/test/hello
```
 
Receive data from the service:
 
```shell
$ curl http://localhost:8000/test/hello
some data
```
 
Note you can also start the receiver first — `txf` won't start the
response to the receiver until the sender connects.
 
## Access Control
 
Requests to a `txf` service should use paths of the following form:
 
```
/namespace/[hmac-sha256-hex/]resource
```
 
The first component in the path we call the `txf` _namespace_.
 
When creating the service, specify secrets for each namespace you want to
support; one secret for senders (PUT requests) and one for receivers
(GET requests).
 
If a namespace's secrets are empty strings then no access control is applied.
Anyone can PUT or GET files to the namespace. In this case, the second component
in the path we call the `txf` resource.
 
If the secrets aren't empty strings then they're used to apply access control
to the namespace. In this case, the third component of the path must be the
resource. The second component must be the hex-encoded HMAC-SHA256 of the
resource, calculated using one of the secrets (depending on the request type)
as the key.
 
Here's the same example as above but with access control. First the service:
 
```javascript
var server = require('http').createServer();
server.listen(8000);
require('txf')(server, { test: { sender: 'secret1', receiver: 'secret2' } });
```
 
Send data to the service:
 
```shell
$ echo 'some data' | curl -T - http://localhost:8000/test/$(echo -n hello | openssl dgst -sha256 -hmac secret1 | awk '{print $2}')/hello
```
 
Receive data from the service:
 
```shell
$ curl http://localhost:8000/test/$(echo -n hello | openssl dgst -sha256 -hmac secret2 | awk '{print $2}')/hello
some data
```
 
## Status Codes
 
`txf` can return the following HTTP status codes:
 
<table>
<thead>
<tr>
<th>
Status Code
</th>
<th>
PUT
</th>
<th>
GET
</th>
</tr>
<tbody>
<tr>
<td>
200
</td>
<td>
All data was forwarded to a receiver. This doesn't mean the receiver has
necessarily received it all.
</td>
<td>
A sender has connected and its data will follow in the response.
</td>
</tr>
<tr>
<td>
400
</td>
<td colspan="2">
Path has fewer than 2 or more than 3 components.
</td>
</tr>
<tr>
<td>
403
</td>
<td colspan="2">
No secrets (empty or otherwise) were supplied for the namespace.
</td>
</tr>
<tr>
<td>
404
</td>
<td colspan="2">
HMAC supplied in the path is incorrect for the resource.
</td>
</tr>
<tr>
<td>
405
</td>
<td colspan="2">
Request method was not PUT or GET.
</td>
</tr>
<tr>
<td>
409
</td>
<td>
A PUT request is already in progress for the resource.
</td>
<td>
A GET request is already in progress for the resource.
</td>
</tr>
<tr>
<td>
504
</td>
<td>
The receiver's connection was closed before all data could be forwarded to it.
</td>
<td>
</td>
</tr>
</tbody>
</table>
 
## Installation
 
```shell
npm install txf
```
 
## Licence
 
[MIT](LICENCE)
 
## Test
 
```shell
grunt test
```
 
## Lint
 
```shell
grunt lint
```
 
## Code Coverage
 
```shell
grunt coverage
```
 
[c8](https://github.com/bcoe/c8) results are available [here](http://rawgit.davedoesdev.com/davedoesdev/txf/master/coverage/lcov-report/index.html).
 
Coveralls page is [here](https://coveralls.io/r/davedoesdev/txf).
 
# API
*/
var url = require('url'),
    crypto = require('crypto');
 
/**
Create a `txf` service which pipes a PUT request's incoming stream to a GET
request's outgoing stream on the same path. Request paths should be of the form
`/namespace/[hmac-sha256-hex/]resource`. If access control is defined for
`namespace` (see the `secrets` parameter below) then `hmac-sha256-hex` must be
the hex-encoded HMAC-SHA256 of `resource`. Otherwise `hmac-sha256-hex` is
not required.
 
@param {http.Server|https.Server} server [HTTP](https://nodejs.org/dist/latest-v4.x/docs/api/http.html#http_class_http_server) or [HTTPS](https://nodejs.org/dist/latest-v4.x/docs/api/https.html#https_class_https_server) server which `txf` will use to listen for requests. It's up to the caller to set up the server (e.g. listen on a port). HTTPS servers will need to know about their certificates and keys. See `make_server()` in `test/test.js` for an example.
 
@param {Object} secrets A dictionary of valid namespaces. Each entry in `secrets` should be a mapping from a namespace to a dictionary of HMAC keys, one for PUT requests (senders) and one for GET requests (receivers).
 
For example:
 
```javascript
{ foo: { sender: 'some secret', receiver: 'another secret' } }
```
 
would mean PUT requests to the `foo` namespace for resource `bar` would have
to use the following path:
 
```
/foo/c6eb7ab0584d4a8f62d64de6767798e09c9566308533cedb86d14547fcab3211/bar
```
 
and GET requests to the `foo` namespace for resource `bar` would have to use
the following path:
 
```
/foo/be83aaf02fb252a1b21f522c95c09f5db563dc6180db034c9c236fb3ffc849ff/bar
```
 
If you specify empty strings for a namespace's secrets:
 
```javascript
{ foo: { sender: '', receiver: '' } }
```
 
then the HMAC-SHA256 is not required. In that case, PUT and GET requests could
use the following path:
 
```
/foo/bar
```
 
Requests to a namespace which don't have an entry in `secrets` will be rejected.
*/
module.exports = function (server, secrets)
{
    var senders = new Map(),
        receivers = new Map();
 
    function check(id)
    {
        var sender = senders.get(id),
            receiver = receivers.get(id);
 
        if (sender && receiver)
        {
            receiver.setHeader('Content-Type', sender.headers['content-type'] || 'application/octet-stream');
            sender.pipe(receiver);
        }
    }
 
    server.on('request', function (request, response)
    {
        var pathname = url.parse(request.url).pathname.split('/'),
            namespace,
            digest,
            id;
 
        if (pathname.length === 4)
        {
            namespace = pathname[1];
            digest = pathname[2];
            id = pathname[3];
        }
        else if (pathname.length === 3)
        {
            namespace = pathname[1];
            id = pathname[2];
        }
        else
        {
            response.statusCode = 400;
            return response.end();
        }
 
        if (!secrets)
        {
            response.statusCode = 403;
            return response.end();
        }
 
        var keys = secrets[namespace];
 
        if (keys === undefined)
        {
            response.statusCode = 403;
            return response.end();
        }
 
        if (request.method === 'PUT')
        {
            var sender_key = keys.sender;
 
            if (sender_key === undefined)
            {
                response.statusCode = 403;
                return response.end();
            }
 
            if (sender_key.length > 0)
            {
                var sender_hmac = crypto.createHmac('sha256', sender_key);
                sender_hmac.update(id);
                if (sender_hmac.digest('hex') !== digest)
                {
                    response.statusCode = 404;
                    return response.end();
                }
            }
 
            if (senders.has(id))
            {
                response.statusCode = 409;
                return response.end();
            }
 
            request.response = response;
            senders.set(id, request);
 
            var sender_done = function ()
            {
                var sender = senders.get(id);
                if (sender === request)
                {
                    senders.delete(id);
                }
            };
 
            request.on('end', function ()
            {
                response.end();
            });
            request.on('close', sender_done);
            request.on('end', sender_done);
 
            check(id);
        }
        else if (request.method === 'GET')
        {
            var receiver_key = keys.receiver;
 
            if (receiver_key === undefined)
            {
                response.statusCode = 403;
                return response.end();
            }
 
            if (receiver_key.length > 0)
            {
                var receiver_hmac = crypto.createHmac('sha256', receiver_key);
                receiver_hmac.update(id);
                if (receiver_hmac.digest('hex') !== digest)
                {
                    response.statusCode = 404;
                    return response.end();
                }
            }
 
            if (receivers.has(id))
            {
                response.statusCode = 409;
                return response.end();
            }
 
            receivers.set(id, response);
 
            var receiver_done = function ()
            {
                var receiver = receivers.get(id);
                if (receiver === response)
                {
                    receivers.delete(id);
                }
            };
 
            response.on('pipe', function (sender)
            {
                response.on('close', function ()
                {
                    sender.unpipe(response);
                    sender.response.statusCode = 504;
                    sender.response.end();
                });
            });
            response.on('close', receiver_done);
            response.on('finish', receiver_done);
 
            check(id);
        }
        else
        {
            response.statusCode = 405;
            return response.end();
        }
    });
 
    return { senders: senders, receivers: receivers }; // for testing
};