-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (48 loc) · 1.79 KB
/
index.js
File metadata and controls
64 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://cloud.google.com/storage/docs/naming#requirements
const validBucketCharacters = /([a-z]|[0-9]|-|_|\.)*/;
const minBucketLength = 3;
const maxBucketLength = 63
const minObjectLength = 1;
const maxObjectLength = 1024;
const ipNotation = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
const googleSpellings = [
"google", "g00gle", "g0ogle", "go0gle", "goog1e", "g00g1e", "g0og1e", "go0g1e"
];
module.exports = {
validateBucket(bucket) {
if (!bucket) {return false;}
if (typeof bucket !== "string") {return false;}
if (bucket.length < minBucketLength) {return false;}
if (bucket.length > maxBucketLength) {return false;}
if (bucket.match(validBucketCharacters)[0] !== bucket) {return false;}
if (!bucket.match(/^[0-9a-z]/)) {return false;}
if (!bucket.match(/[0-9a-z]$/)) {return false;}
if (bucket.match(ipNotation)) {return false;}
if (bucket.startsWith("goog")) {return false;}
if (googleSpellings.some(google=>bucket.includes(google))) {return false;}
return true;
},
validateObject(object) {
if (!object) {return false;}
if (object.length < minObjectLength) {return false;}
if (object.length > maxObjectLength) {return false;}
if (object.includes("\n")) {return false;}
return true;
},
validateFilepath(filepath) {
let bucket = null;
let object = null;
if (typeof filepath === "object") {
bucket = filepath.bucket;
object = filepath.object;
} else {
if (typeof filepath !== "string") {return false;}
if (!filepath.includes("/")) {return false;}
bucket = filepath.split("/")[0];
object = filepath.split("/").slice(1).join("/");
}
if (!bucket || !object) {return false;}
return module.exports.validateObject(object) &&
module.exports.validateBucket(bucket);
}
};