Skip to content

Remove JS input validation for Storage #3967

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

Merged
merged 5 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions .changeset/tiny-hounds-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"firebase: major
"@firebase/storage": major
---

This releases removes all input validation. Please use our TypeScript types to validate API usage.
165 changes: 0 additions & 165 deletions packages/storage/src/implementation/args.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/storage/src/implementation/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
import { Location } from './location';
import * as json from './json';
import * as type from './type';
import { ListResult } from '../list';
import { StorageService } from '../service';

Expand All @@ -43,9 +42,6 @@ interface ListResultResponse {
nextPageToken?: string;
}

const MAX_RESULTS_KEY = 'maxResults';
const MAX_MAX_RESULTS = 1000;
const PAGE_TOKEN_KEY = 'pageToken';
const PREFIXES_KEY = 'prefixes';
const ITEMS_KEY = 'items';

Expand Down Expand Up @@ -92,28 +88,3 @@ export function fromResponseString(
const resource = (obj as unknown) as ListResultResponse;
return fromBackendResponse(service, bucket, resource);
}

export function listOptionsValidator(p: unknown): void {
if (!type.isObject(p) || !p) {
throw 'Expected ListOptions object.';
}
for (const key in p) {
if (key === MAX_RESULTS_KEY) {
if (
!type.isInteger(p[MAX_RESULTS_KEY]) ||
(p[MAX_RESULTS_KEY] as number) <= 0
) {
throw 'Expected maxResults to be a positive number.';
}
if ((p[MAX_RESULTS_KEY] as number) > 1000) {
throw `Expected maxResults to be less than or equal to ${MAX_MAX_RESULTS}.`;
}
} else if (key === PAGE_TOKEN_KEY) {
if (p[PAGE_TOKEN_KEY] && !type.isString(p[PAGE_TOKEN_KEY])) {
throw 'Expected pageToken to be string.';
}
} else {
throw 'Unknown option: ' + key;
}
}
}
16 changes: 10 additions & 6 deletions packages/storage/src/implementation/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as type from './type';
import * as UrlUtils from './url';
import { Reference } from '../reference';
import { StorageService } from '../service';
import { Code, FirebaseStorageError } from './error';

export function noXform_<T>(metadata: Metadata, value: T): T {
return value;
Expand Down Expand Up @@ -206,20 +207,23 @@ export function toResourceString(
return JSON.stringify(resource);
}

export function metadataValidator(p: unknown): void {
if (!type.isObject(p) || !p) {
throw 'Expected Metadata object.';
}
export function validateMetadata(p: { [key: string]: unknown }): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually got rid of this entirely in storage-exp because it's checking types of Metadata fields which should be caught by typescript check? Or did I make a mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I didn't realize this was fully typed. Removed.

for (const key in p) {
if (p.hasOwnProperty(key)) {
const val = p[key];
if (key === 'customMetadata') {
if (!type.isObject(val)) {
throw "Expected object for 'customMetadata' mapping.";
throw new FirebaseStorageError(
Code.INVALID_ARGUMENT,
"Expected object for 'customMetadata' mapping."
);
}
} else {
if (type.isNonNullObject(val)) {
throw "Mapping for '" + key + "' cannot be an object.";
throw new FirebaseStorageError(
Code.INVALID_ARGUMENT,
"Mapping for '" + key + "' cannot be an object."
);
}
}
}
Expand Down
22 changes: 0 additions & 22 deletions packages/storage/src/implementation/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,6 @@ export const StringFormat = {
DATA_URL: 'data_url'
};

export function formatValidator(stringFormat: unknown): void {
switch (stringFormat) {
case StringFormat.RAW:
case StringFormat.BASE64:
case StringFormat.BASE64URL:
case StringFormat.DATA_URL:
return;
default:
throw (
'Expected one of the event types: [' +
StringFormat.RAW +
', ' +
StringFormat.BASE64 +
', ' +
StringFormat.BASE64URL +
', ' +
StringFormat.DATA_URL +
'].'
);
}
}

/**
* @struct
*/
Expand Down
8 changes: 0 additions & 8 deletions packages/storage/src/implementation/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ export function isString(p: unknown): p is string {
return typeof p === 'string' || p instanceof String;
}

export function isInteger(p: unknown): p is number {
return isNumber(p) && Number.isInteger(p);
}

export function isNumber(p: unknown): p is number {
return typeof p === 'number' || p instanceof Number;
}

export function isNativeBlob(p: unknown): p is Blob {
return isNativeBlobDefined() && p instanceof Blob;
}
Expand Down
Loading