Skip to content
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

Remove JS Input Validation #3939

Merged
merged 4 commits into from
Oct 20, 2020
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Oct 13, 2020

This PR removes all input validation that should be caught by any JavaScript environments that supports strict type checking, such as TypeScript or Google Closure. It is merely meant to reduce binary size.

Validations that remain:

  • Empty strings are still rejected as collection ID/document IDs (note that Android & iOS don't seem to do this
  • Limits have to be positive
  • All query constraints and restrictions that are not expressed via the API
  • The firestore-exp API still calls cast to verify that the passed in objects are the right Firestore objects. This should catch almost all cases where a user forgets to change the arguments as they transition to the functional API.

@changeset-bot
Copy link

changeset-bot bot commented Oct 13, 2020

🦋 Changeset detected

Latest commit: 60d46aa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
firebase Major
@firebase/firestore Major
@firebase/rules-unit-testing Patch
rxfire Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 14, 2020

Binary Size Report

Affected SDKs

  • @firebase/app

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 11.1 kB 11.0 kB -97 B (-0.9%)
  • @firebase/auth

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 179 kB 179 kB -10 B (-0.0%)
  • @firebase/component

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 5.30 kB 5.18 kB -115 B (-2.2%)
  • @firebase/database

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 270 kB 269 kB -1.60 kB (-0.6%)
  • @firebase/firestore

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 249 kB 239 kB -10.2 kB (-4.1%)
    esm2017 198 kB 190 kB -7.59 kB (-3.8%)
    main 484 kB 471 kB -13.2 kB (-2.7%)
    module 247 kB 239 kB -7.61 kB (-3.1%)
    react-native 198 kB 190 kB -7.60 kB (-3.8%)
  • @firebase/firestore/exp

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 190 kB 188 kB -1.40 kB (-0.7%)
    main 478 kB 476 kB -2.51 kB (-0.5%)
    module 190 kB 188 kB -1.40 kB (-0.7%)
    react-native 190 kB 189 kB -1.40 kB (-0.7%)
  • @firebase/firestore/lite

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 63.5 kB 62.2 kB -1.33 kB (-2.1%)
    main 140 kB 137 kB -2.54 kB (-1.8%)
    module 63.5 kB 62.2 kB -1.33 kB (-2.1%)
    react-native 63.7 kB 62.4 kB -1.33 kB (-2.1%)
  • @firebase/firestore/memory

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 186 kB 177 kB -9.55 kB (-5.1%)
    esm2017 148 kB 140 kB -7.57 kB (-5.1%)
    main 356 kB 343 kB -13.2 kB (-3.7%)
    module 184 kB 177 kB -7.60 kB (-4.1%)
    react-native 148 kB 140 kB -7.57 kB (-5.1%)
  • @firebase/functions

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 10.1 kB 9.87 kB -268 B (-2.6%)
  • @firebase/performance

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 27.8 kB 27.6 kB -269 B (-1.0%)
  • @firebase/remote-config

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 22.8 kB 22.4 kB -421 B (-1.8%)
  • @firebase/rules-unit-testing

    Type Base (0204abf) Head (f6ff48c) Diff
    main 7.86 kB 8.00 kB +137 B (+1.7%)
  • @firebase/util

    Type Base (0204abf) Head (f6ff48c) Diff
    browser 21.0 kB 20.1 kB -824 B (-3.9%)
    esm2017 18.6 kB 18.9 kB +269 B (+1.4%)
    main 21.0 kB 21.3 kB +279 B (+1.3%)
    module 19.9 kB 20.1 kB +263 B (+1.3%)
  • firebase

    Type Base (0204abf) Head (f6ff48c) Diff
    firebase-firestore.js 286 kB 278 kB -7.69 kB (-2.7%)
    firebase-firestore.memory.js 225 kB 217 kB -7.69 kB (-3.4%)
    firebase.js 831 kB 823 kB -7.70 kB (-0.9%)

Test Logs

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 14, 2020

Size Analysis Report

Affected Products

No changes between base commit (0204abf) and head commit (f6ff48c).

Test Logs

@schmidt-sebastian
Copy link
Contributor Author

I sat on this for too long since I was hoping this would shave off even more bytes, but I was just informed that code freeze for Fireconf is tomorrow.

@@ -189,6 +190,7 @@ export class Transaction
options?: legacy.SetOptions
): Transaction {
if (options) {
validateSetOptions('Transaction.set', options);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still need to validate that merge and mergeFields isn't used together.

@@ -69,7 +68,6 @@ export function serverTimestamp(): FieldValue {
* `updateDoc()`.
*/
export function arrayUnion(...elements: unknown[]): FieldValue {
validateAtLeastNumberOfArgs('arrayUnion()', arguments, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While this make a little bit of sense, it is absent on Android.

Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

Basically LGTM, though with some questions.

'boolean',
'experimentalAutoDetectLongPolling',
settings.experimentalAutoDetectLongPolling
);
this.experimentalAutoDetectLongPolling =
settings.experimentalAutoDetectLongPolling ??
Copy link
Contributor

Choose a reason for hiding this comment

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

So given that we're not validating that settings.experimentalAutoDetectLongPolling is actually a boolean value, should we be accessing it with a forced coercion (i.e. as !!settings.experimentalAutoDetectLongPolling)?

Or is the idea that we've declared the type in typescript and all bets are off for users who aren't using our d.ts files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally decided against this approach, because it makes it impossible to turn a feature off it its default value is true. Since we have only ever been using opt-in features, this has not been a problem yet. I updated this code, which ensures that we have a boolean value once we exit this function (it also removes some code).

@@ -1478,10 +1346,8 @@ export class DocumentSnapshot<T = DocumentData>

get(
fieldPath: string | ExternalFieldPath,
options?: PublicSnapshotOptions
options: PublicSnapshotOptions = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this require an API change to the d.ts file, or is this rendered by tsc as a local assignment if not defined? Stated alternatively: does the caller supply the default argument or does the callee?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default argument is provided by the callee. An example in ES3:

function foo(bar = {}) {
}

transpiles to:

"use strict";
function foo(bar) {
    if (bar === void 0) { bar = {}; }
}

ES5+JavaScript supports default arguments, but the semantics match the code snippet above.

@@ -2487,7 +2274,7 @@ export class CollectionReference<T = DocumentData>
}
}

function validateSetOptions(
export function validateSetOptions(
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this potentially create a cycle? Better to move this into input_validation.ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Oct 19, 2020
Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Oct 20, 2020
@schmidt-sebastian schmidt-sebastian merged commit 4b540f9 into master Oct 20, 2020
@google-oss-bot google-oss-bot mentioned this pull request Oct 22, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/no-validation branch November 9, 2020 22:36
@firebase firebase locked and limited conversation to collaborators Nov 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants