Skip to content

fix: prevent linking text with directional change char #388

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/parser/parse-matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { UrlMatch, UrlMatchType } from '../match/url-match';
import { Match } from '../match/match';
import { remove, assertNever } from '../utils';
import {
hasDirectionalChar,
httpSchemeRe,
isDomainLabelChar,
isDomainLabelStartChar,
Expand Down Expand Up @@ -411,7 +412,7 @@ export function parseMatches(text: string, args: ParseMatchesArgs): Match[] {
} else if (isUrlSuffixStartChar(char)) {
// '/', '?', or '#'
stateMachine.state = State.Path;
} else if (isDomainLabelChar(char)) {
} else if (isDomainLabelChar(char) || hasDirectionalChar(char)) {
// Stay in the DomainLabelChar state
} else {
// Anything else, end the domain name
Expand Down
14 changes: 14 additions & 0 deletions src/parser/uri-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export function isDomainLabelChar(char: string): boolean {
return char === '_' || isDomainLabelStartChar(char);
}

export function hasDirectionalChar(char: string) {
Copy link
Owner

@gregjacobs gregjacobs Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but can we rename this to isDirectionOverrideChar, and add a jsdoc comment above it explaining why it's used?

Can add a link for #377 too inside the jsdoc to point developers to the original issue that this new function solves

return /[\u202a-\u202e\u200e-\u200f]/g.test(char);
}

/**
* Determines if the character is a path character ("pchar") as defined by
* https://tools.ietf.org/html/rfc3986#appendix-A
Expand Down Expand Up @@ -168,6 +172,11 @@ export function isValidSchemeUrl(url: string): boolean {
return false;
}

// If url contains directional change character prevent it from linking
if (hasDirectionalChar(url)) {
return false;
}

const isAuthorityMatch = !!schemeMatch![1];
const host = schemeMatch![2];
if (isAuthorityMatch) {
Expand Down Expand Up @@ -213,6 +222,11 @@ export function isValidTldMatch(url: string): boolean {
return false;
}

// If url contains directional change character prevent it from linking
if (hasDirectionalChar(url)) {
return false;
}

const tld = hostLabels[hostLabels.length - 1];
if (!isKnownTld(tld)) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions tests/autolinker-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,13 @@ describe('Autolinker Url Matching >', () => {
});
});

describe('unicode exploits', () => {
fit('text with directional change characters should not be linked', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful: fit -> it

expect(autolinker.link('foo.com\u202Ebar.com')).toBe('foo.com\u202Ebar.com');
expect(autolinker.link('foo.com\u202Emoc.rab')).toBe('foo.com\u202Emoc.rab');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the domain name tests here, we should add tests for when the \u202E char appears in the path, query string, and hash sections of the URL.

Also, can we add tests for each of the direction override chars like you had in #386?

});
});

function generateCombinationTests({
schemes,
hosts,
Expand Down