Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Improved package name validation #2687

Merged
merged 1 commit into from
Sep 23, 2020
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
18 changes: 16 additions & 2 deletions packages/expo-cli/src/commands/eject/ConfigValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@ function validateBundleId(value: string): boolean {
return /^[a-zA-Z][a-zA-Z0-9\-.]+$/.test(value);
}

function validatePackage(value: string): boolean {
return /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$/.test(value);
/**
* Validates an Android package name given the following constraints: https://developer.android.com/studio/build/application-id
Copy link
Contributor Author

Choose a reason for hiding this comment

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

*
* @param value
*/
export function validatePackage(value: string): boolean {
if (
!/^(?:[a-zA-Z]+(?:\d*[a-zA-Z_]*)*)(?:\.[a-zA-Z]+(?:\d*[a-zA-Z_]*)*)+$/.test(value) ||
// No android reserved words https://stackoverflow.com/a/39331217
!/(?!^abstract$|^abstract\..*|.*\.abstract\..*|.*\.abstract$|^assert$|^assert\..*|.*\.assert\..*|.*\.assert$|^boolean$|^boolean\..*|.*\.boolean\..*|.*\.boolean$|^break$|^break\..*|.*\.break\..*|.*\.break$|^byte$|^byte\..*|.*\.byte\..*|.*\.byte$|^case$|^case\..*|.*\.case\..*|.*\.case$|^catch$|^catch\..*|.*\.catch\..*|.*\.catch$|^char$|^char\..*|.*\.char\..*|.*\.char$|^class$|^class\..*|.*\.class\..*|.*\.class$|^const$|^const\..*|.*\.const\..*|.*\.const$|^continue$|^continue\..*|.*\.continue\..*|.*\.continue$|^default$|^default\..*|.*\.default\..*|.*\.default$|^do$|^do\..*|.*\.do\..*|.*\.do$|^double$|^double\..*|.*\.double\..*|.*\.double$|^else$|^else\..*|.*\.else\..*|.*\.else$|^enum$|^enum\..*|.*\.enum\..*|.*\.enum$|^extends$|^extends\..*|.*\.extends\..*|.*\.extends$|^final$|^final\..*|.*\.final\..*|.*\.final$|^finally$|^finally\..*|.*\.finally\..*|.*\.finally$|^float$|^float\..*|.*\.float\..*|.*\.float$|^for$|^for\..*|.*\.for\..*|.*\.for$|^goto$|^goto\..*|.*\.goto\..*|.*\.goto$|^if$|^if\..*|.*\.if\..*|.*\.if$|^implements$|^implements\..*|.*\.implements\..*|.*\.implements$|^import$|^import\..*|.*\.import\..*|.*\.import$|^instanceof$|^instanceof\..*|.*\.instanceof\..*|.*\.instanceof$|^int$|^int\..*|.*\.int\..*|.*\.int$|^interface$|^interface\..*|.*\.interface\..*|.*\.interface$|^long$|^long\..*|.*\.long\..*|.*\.long$|^native$|^native\..*|.*\.native\..*|.*\.native$|^new$|^new\..*|.*\.new\..*|.*\.new$|^package$|^package\..*|.*\.package\..*|.*\.package$|^private$|^private\..*|.*\.private\..*|.*\.private$|^protected$|^protected\..*|.*\.protected\..*|.*\.protected$|^public$|^public\..*|.*\.public\..*|.*\.public$|^return$|^return\..*|.*\.return\..*|.*\.return$|^short$|^short\..*|.*\.short\..*|.*\.short$|^static$|^static\..*|.*\.static\..*|.*\.static$|^strictfp$|^strictfp\..*|.*\.strictfp\..*|.*\.strictfp$|^super$|^super\..*|.*\.super\..*|.*\.super$|^switch$|^switch\..*|.*\.switch\..*|.*\.switch$|^synchronized$|^synchronized\..*|.*\.synchronized\..*|.*\.synchronized$|^this$|^this\..*|.*\.this\..*|.*\.this$|^throw$|^throw\..*|.*\.throw\..*|.*\.throw$|^throws$|^throws\..*|.*\.throws\..*|.*\.throws$|^transient$|^transient\..*|.*\.transient\..*|.*\.transient$|^try$|^try\..*|.*\.try\..*|.*\.try$|^void$|^void\..*|.*\.void\..*|.*\.void$|^volatile$|^volatile\..*|.*\.volatile\..*|.*\.volatile$|^while$|^while\..*|.*\.while\..*|.*\.while$)(^(?:[a-z_]+(?:\d*[a-zA-Z_]*)*)(?:\.[a-z_]+(?:\d*[a-zA-Z_]*)*)*$)/.test(
value
)
) {
return false;
}
return true;
}

const cachedBundleIdResults: Record<string, string> = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { validatePackage } from '../ConfigValidation';

describe(validatePackage, () => {
it(`must have at least two components split by a dot`, () => {
expect(validatePackage('somn')).toBe(false);
expect(validatePackage('somn.other')).toBe(true);
expect(validatePackage('a.a.a.a')).toBe(true);
expect(validatePackage('a_.a.a.a')).toBe(true);
});
it(`requires each segment to start with a letter.`, () => {
expect(validatePackage('1a.2b.c3')).toBe(false);
expect(validatePackage('a.2b.c')).toBe(false);
expect(validatePackage('a.b2.c')).toBe(true);
});
it(`doesn't allow dashes`, () => {
expect(validatePackage('a.b-c')).toBe(false);
});
it(`doesn't allow special character`, () => {
expect(validatePackage('com.bacon.©')).toBe(false);
});
it(`doesn't allow capitalized`, () => {
// simple
expect(validatePackage('a.Catch')).toBe(false);
// test components
expect(validatePackage('foo.BAR.int')).toBe(false);
});
it(`doesn't allow android reserved words`, () => {
// simple
expect(validatePackage('a.b.catch')).toBe(false);
// test components
expect(validatePackage('foo.bar.int')).toBe(false);
// reserved word not exact match.
expect(validatePackage('somn.foo.falser')).toBe(true);
expect(validatePackage('while1.package2.void3.transient4')).toBe(true);

// more reserved works
expect(validatePackage('foo.bar.abstract')).toBe(false);
expect(validatePackage('foo.bar.assert')).toBe(false);
});
});