identity: forbid underscores in GitHub logins (#1414)

Summary:
GitHub logins may not have underscores, because underscores are not
valid characters in DNS labels. We already have a good-enough regular
expression for validating GitHub usernames; this commit updates the
alias parser to use that.

Discourse usernames are more permissive than what is listed here, but we
leave that unchanged for now.

Test Plan:
Unit tests updated.

wchargin-branch: alias-no-underscore
This commit is contained in:
William Chargin 2019-10-19 09:10:38 -07:00 committed by GitHub
parent 28b25c2910
commit f577ae7c1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -1,6 +1,7 @@
// @flow // @flow
import {type NodeAddressT} from "../../core/graph"; import {type NodeAddressT} from "../../core/graph";
import {githubOwnerPattern} from "../../core/repoId";
import {loginAddress as githubAddress} from "../github/nodes"; import {loginAddress as githubAddress} from "../github/nodes";
import {userAddress as discourseAddress} from "../discourse/address"; import {userAddress as discourseAddress} from "../discourse/address";
@ -18,7 +19,7 @@ import {userAddress as discourseAddress} from "../discourse/address";
export type Alias = string; export type Alias = string;
const _VALID_ALIAS = /^(\w+)[/](.*)$/; const _VALID_ALIAS = /^(\w+)[/](.*)$/;
const _VALID_GITHUB_NAME = /^@?([0-9a-z_-]+)$/i; const _VALID_GITHUB_NAME = new RegExp(`^@?(${githubOwnerPattern})$`);
const _VALID_DISCOURSE_NAME = /^@?([0-9a-z_-]+)$/i; const _VALID_DISCOURSE_NAME = /^@?([0-9a-z_-]+)$/i;
export function resolveAlias( export function resolveAlias(

View File

@ -38,6 +38,11 @@ describe("src/plugins/identity/alias", () => {
"Invalid GitHub username" "Invalid GitHub username"
); );
}); });
it("a github login with underscores", () => {
expect(() => resolveAlias("github/foo_bar", null)).toThrow(
"Invalid GitHub username"
);
});
it("a discourse login with invalid characters", () => { it("a discourse login with invalid characters", () => {
expect(() => resolveAlias("discourse/!@#$", "url")).toThrow( expect(() => resolveAlias("discourse/!@#$", "url")).toThrow(
"Invalid Discourse username" "Invalid Discourse username"
@ -50,9 +55,9 @@ describe("src/plugins/identity/alias", () => {
const expected = githubAddress("login"); const expected = githubAddress("login");
expect(actual).toEqual(expected); expect(actual).toEqual(expected);
}); });
it("a github login with underscores and dashes", () => { it("a github login with hyphens", () => {
const actual = resolveAlias("github/login_foo-bar", null); const actual = resolveAlias("github/login-foo-bar", null);
const expected = githubAddress("login_foo-bar"); const expected = githubAddress("login-foo-bar");
expect(actual).toEqual(expected); expect(actual).toEqual(expected);
}); });
it("a discourse login", () => { it("a discourse login", () => {