improve file type detection

This commit is contained in:
tdurieux committed 2021-04-22 09:30:28 +02:00
1 parent 06b3d0e8f1
commit 4d256c5464
1 file changed
+29 -54
+29 -54
View File
@@ -269,15 +269,20 @@ module.exports.additionalExtensions = [
"in", "in",
]; ];
module.exports.isText = (p) => { module.exports.isText = (p) => {
if (isText(p)) {
return true;
}
const filename = path.basename(p); const filename = path.basename(p);
const extensions = filename.split(".").reverse(); const extensions = filename.split(".").reverse();
const extension = extensions[0].toLowerCase(); const extension = extensions[0].toLowerCase();
if (module.exports.additionalExtensions.includes(extension)) { if (module.exports.additionalExtensions.includes(extension)) {
return true; return true;
} }
if (isText(p)) {
return true;
}
if (ofs.existsSync(p)) {
if (isText(p, ofs.readFileSync(p))) {
return true;
}
}
return false; return false;
}; };
module.exports.isFileSupported = (repoConfig, p) => { module.exports.isFileSupported = (repoConfig, p) => {
@@ -295,6 +300,7 @@ module.exports.isFileSupported = (repoConfig, p) => {
if ( if (
repoConfig.options.image && repoConfig.options.image &&
(extension == "png" || (extension == "png" ||
extension == "ico" ||
extension == "jpg" || extension == "jpg" ||
extension == "jpeg" || extension == "jpeg" ||
extension == "gif") extension == "gif")
@@ -330,15 +336,17 @@ module.exports.isFilePathValid = async (options) => {
options.path options.path
); );
if (!module.exports.isFileSupported(repoConfig, anonymizedFilePath)) { if (ofs.existsSync(anonymizedFilePath)) {
throw "file_not_supported"; return true;
} }
let anonymizePath = options.path; let unanonymizePath = options.path;
if (anonymizePath.indexOf(config.ANONYMIZATION_MASK) > -1) {
const files = await module.exports.getFileList({ repoConfig }); const files = await module.exports.getFileList({ repoConfig });
const file = getFile(files, options.path); const file = getFile(files, options.path);
if (file == null) {
throw "file_not_found";
}
if (file) { if (file) {
const r = await db const r = await db
.get("anonymized_repositories") .get("anonymized_repositories")
@@ -349,19 +357,17 @@ module.exports.isFilePathValid = async (options) => {
const shatree = tree2sha(r.originalFiles); const shatree = tree2sha(r.originalFiles);
if (shatree[file.sha]) { if (shatree[file.sha]) {
anonymizePath = shatree[file.sha]; unanonymizePath = shatree[file.sha];
}
} }
} }
const originalFilePath = path.join( const originalFilePath = path.join(
repoUtils.getOriginalPath(repoConfig.repoId), repoUtils.getOriginalPath(repoConfig.repoId),
anonymizePath unanonymizePath
); );
if (ofs.existsSync(anonymizedFilePath)) {
return true;
}
if (ofs.existsSync(originalFilePath)) { if (ofs.existsSync(originalFilePath)) {
if (!module.exports.isFileSupported(repoConfig, anonymizedFilePath)) { if (!module.exports.isFileSupported(repoConfig, originalFilePath)) {
throw "file_not_supported"; throw "file_not_supported";
} }
await anonymizeUtils.anonymizeFile( await anonymizeUtils.anonymizeFile(
@@ -373,12 +379,6 @@ module.exports.isFilePathValid = async (options) => {
} }
// if stream mode check download the file // if stream mode check download the file
if (repoConfig.options.mode == "stream") { if (repoConfig.options.mode == "stream") {
const repo = gh(repoConfig.fullName);
const files = await module.exports.getFileList({ repoConfig });
let file = getFile(files, options.path);
if (file == null) {
throw "file_not_found";
}
if (!file.sha) { if (!file.sha) {
throw "is_folder"; throw "is_folder";
} }
@@ -391,11 +391,8 @@ module.exports.isFilePathValid = async (options) => {
}); });
let ghRes = null; let ghRes = null;
if (file) {
if (!module.exports.isFileSupported(repoConfig, anonymizedFilePath)) {
throw "file_not_supported";
}
try { try {
const repo = gh(repoConfig.fullName);
ghRes = await octokit.request( ghRes = await octokit.request(
"GET /repos/{owner}/{repo}/git/blobs/{file_sha}", "GET /repos/{owner}/{repo}/git/blobs/{file_sha}",
{ {
@@ -405,37 +402,12 @@ module.exports.isFilePathValid = async (options) => {
} }
); );
} catch (error) { } catch (error) {
if (error.status == 401 && config.GITHUB_TOKEN) { if (error.status == 403) {
try {
response = await getZip(config.GITHUB_TOKEN);
} catch (error) {
throw "repo_not_accessible";
}
} else if (error.status == 403) {
throw "file_too_big"; throw "file_too_big";
} }
console.error(error); console.error(error);
throw "file_not_accessible"; throw "file_not_accessible";
} }
} else {
try {
ghRes = await octokit.repos.getContents({
owner: repo.owner,
repo: repo.name,
path: options.path,
ref: repoConfig.commit ? repoConfig.commit : "HEAD",
});
} catch (error) {
if (error.status == 404) {
return false;
}
if (error.status == 403) {
console.log(error);
throw "file_too_big";
}
throw error;
}
}
if (!ghRes.data.content && ghRes.data.size != 0) { if (!ghRes.data.content && ghRes.data.size != 0) {
throw "file_not_accessible"; throw "file_not_accessible";
} }
@@ -452,15 +424,18 @@ module.exports.isFilePathValid = async (options) => {
} }
try { try {
await fs.writeFile(originalFilePath, content, { encoding: "utf-8" }); await fs.writeFile(originalFilePath, content, { encoding: "utf-8" });
} catch (error) {
console.error(error);
throw "unable_to_write_file";
}
if (!module.exports.isFileSupported(repoConfig, originalFilePath)) {
throw "file_not_supported";
}
await anonymizeUtils.anonymizeFile( await anonymizeUtils.anonymizeFile(
originalFilePath, originalFilePath,
anonymizedFilePath, anonymizedFilePath,
repoConfig repoConfig
); );
} catch (error) {
console.error(error);
throw "unable_to_write_file";
}
return true; return true;
} }
return false; return false;