Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit linkHeader length, throw error if exceeds #25

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:

`PARSE_LINK_HEADER_MAXLEN` - 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 `PARSE_LINK_HEADER_MAXLEN` parameter is exceeded. if defined, an error will be thrown; if it's `null`, the function fails silently by returning `null`. Defaults to `null`.

### 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.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var qs = require('querystring')
, url = require('url')
, xtend = require('xtend');

const PARSE_LINK_HEADER_MAXLEN = parseInt(process.env.PARSE_LINK_HEADER_MAXLEN) || 2000;
const PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED = process.env.PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED != null

function hasRel(x) {
return x && x.rel;
}
Expand Down Expand Up @@ -46,8 +49,21 @@ function parseLink(link) {
}
}

function checkHeader(linkHeader){
if (!linkHeader) return false;

if (linkHeader.length > PARSE_LINK_HEADER_MAXLEN) {
if (PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED) {
throw new Error('Input string too long, it should be under ' + PARSE_LINK_HEADER_MAXLEN + ' characters.');
} else {
return false;
}
}
assaf-benjosef marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

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

return linkHeader.split(/,\s*</)
.map(parseLink)
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()
})