Skip to content

Commit

Permalink
Create checkHeader func, add env vars and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf-benjosef committed Oct 24, 2021
1 parent 482599a commit 1a2d021
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ console.log(parsed);
Parses the given link header containing [web links](http://tools.ietf.org/html/rfc5988) and returns an object keyed by
the `rel` property that contains information about each link.

### Environmental Variables

To avoid redundantly parsing of extremely long (invalid) input, the package uses 2 env variabes:

`MAX_LINKHEADER_LENGTH` - Sets the number of characters the input should be limited to - longer inputs will not be handled. Defaults to `2000`.

`PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED` - Defines behavior for when the `MAX_LINKHEADER_LENGTH` parameter is exceeded. if `true`, an error will be thrown; if `false`, the function fails silently by returning `null`. Defaults to `false`.

### Formatting a link header

The purpose of this module is to parse the link header information. To format an object generated by this module back to the link header string, use the [format-link-header](https://github.com/jonathansamines/format-link-header) module.
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var qs = require('querystring')
, url = require('url')
, xtend = require('xtend');



function hasRel(x) {
return x && x.rel;
}
Expand Down Expand Up @@ -46,13 +48,22 @@ function parseLink(link) {
}
}

module.exports = function (linkHeader) {
if (!linkHeader) return null;
const MAX_LINKHEADER_LENGTH = 2000;
function checkHeader(linkHeader){
if (!linkHeader) return false;
const MAX_LINKHEADER_LENGTH = process.env.MAX_LINKHEADER_LENGTH || 2000;
const PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED = process.env.PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED || false
if (linkHeader.length > MAX_LINKHEADER_LENGTH) {
if (PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED) {
throw new Error('Input string too long, it should be under ' + MAX_LINKHEADER_LENGTH + ' characters.');
}
else return false;
}

return true;
}

module.exports = function (linkHeader) {
if (!checkHeader(linkHeader)) return null;

return linkHeader.split(/,\s*</)
.map(parseLink)
.filter(hasRel)
Expand Down
18 changes: 18 additions & 0 deletions test/parse-link-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,21 @@ test('parsing a proper link header with matrix parameters', function (t) {
)
t.end()
})

test('parsing an extremely long link header', function (t) {
function payload (n) {
var ret = ""
for (var i = 0; i < n; i++) {
ret += " "
}
return ret
}
var linkHeader = '; rel="' + payload(10000) + '",'

t.equal(
parse(linkHeader)
, null
, 'correctly returns null when dealing with an extremely long link header'
)
t.end()
})

0 comments on commit 1a2d021

Please sign in to comment.