fix(res.links): skip empty-array rel values to prevent trailing comma in Link header#7320
Open
JSap0914 wants to merge 1 commit into
Open
fix(res.links): skip empty-array rel values to prevent trailing comma in Link header#7320JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
… in Link header
When res.links() is called with an empty array for a rel value, the inner
.map().join(', ') produces an empty string ''. The outer .join(', ') then
includes that empty entry, resulting in either a trailing comma:
res.links({ next: 'http://example.com/2', prev: [] })
// Before: Link: <http://example.com/2>; rel="next",
// After: Link: <http://example.com/2>; rel="next"
or a dangling separator when a second call appends nothing:
res.links({ next: 'http://example.com/2' })
res.links({ prev: [] })
// Before: Link: <http://example.com/2>; rel="next",
// After: Link: <http://example.com/2>; rel="next"
Fix: collect new link entries first, filter out empty strings with
.filter(Boolean), then only prepend the ', ' separator when both the
existing Link value and the new entries are non-empty.
Fixes: RFC 8288 compliance — Link header values must be a comma-separated
list of link values; an empty string or trailing comma is invalid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When
res.links()is called with an empty array[]for a rel value, the inner.map().join(', ')produces an empty string''. The outer.join(', ')then includes that empty entry, yielding an invalid Link header:A second call appending only empty-array rels also produced a trailing comma:
RFC 8288 requires the
Linkheader to be a comma-separated list of link-values; a trailing comma or empty entry is invalid and may confuse clients or intermediaries.Fix
Extract the new link entries into
newLinks, filter out empty strings with.filter(Boolean), then only prepend', 'when both the existingLinkvalue and the new entries are non-empty.Verification