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

Commit

Permalink
Improved package name validation (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored Sep 23, 2020
1 parent c702d09 commit a1b565e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
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
*
* @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);
});
});

8 comments on commit a1b565e

@bnussman
Copy link

Choose a reason for hiding this comment

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

Hey guys. This commit broke my android package name! I understand I should not have used a capital A, but this is valid on google's end so expo should allow capitals as I show below...

"android": { "package": "app.ridebeep.App", "versionCode": 12, "googleServicesFile": "./google-services.json" },

I made a post on the Expo forums because I suspected an issue.

@bnussman
Copy link

Choose a reason for hiding this comment

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

Maybe Expo should implement some warning for capital letters instead of not allowing them.

@brentvatne
Copy link
Member

@brentvatne brentvatne commented on a1b565e Sep 25, 2020

Choose a reason for hiding this comment

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

@bnussman - thanks for the heads up. the problem here i think is that we treat applicationId and package as the same thing. we need to investigate what we should do here, i suspect we likely want to let you specify applicationId separately from package name

@bnussman
Copy link

Choose a reason for hiding this comment

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

@brentvatne thank you so much for the quick response and action

@beDenz
Copy link

@beDenz beDenz commented on a1b565e Sep 26, 2020

Choose a reason for hiding this comment

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

How now build application with old style package name, with uppercase symbols? Have any solutions? Because google play console sensitive to this one.

@Ryxar
Copy link

@Ryxar Ryxar commented on a1b565e Sep 28, 2020

Choose a reason for hiding this comment

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

Same issue, Capital Letters in my package name, I'm not able to push in play store anymore. I'll try to downgrade expo-cli to 3.27.8

@bnussman
Copy link

Choose a reason for hiding this comment

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

If you guys need a temporary work around. Just find the file packages/expo-cli/src/commands/eject/ConfigValidation.js and manually put the old code for validatePackage back in

@brentvatne
Copy link
Member

Choose a reason for hiding this comment

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

i just rolled out expo-cli@3.27.11 as the latest release, this reverts this commit so we go back to the old style of validation. we will need to apply these validation rules to the package name in the future, but we will only add them back once we support applicationId config as separate from package

Please sign in to comment.