Skip to content

Commit

Permalink
fix: only consider files to be asar archives
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 27, 2019
1 parent 26d9ef9 commit fe7ab65
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 170 deletions.
21 changes: 13 additions & 8 deletions lib/common/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,26 @@
}
if (typeof archivePath !== 'string') return { isAsar: false }

if (archivePath.endsWith(ASAR_EXTENSION)) {
if (archivePath.endsWith(ASAR_EXTENSION) && asar.isArchive(archivePath)) {
return { isAsar: true, asarPath: archivePath, filePath: '' }
}

archivePath = path.normalize(archivePath)

// E.g. for "//some/path/to/archive.asar/then/internal.file"...
const index = archivePath.lastIndexOf(`${ASAR_EXTENSION}${path.sep}`)
if (index === -1) return { isAsar: false }

// E.g. for "//some/path/to/archive.asar/then/internal.file"...
return {
isAsar: true,
// "//some/path/to/archive.asar"
asarPath: archivePath.substr(0, index + ASAR_EXTENSION.length),
// "then/internal.file" (with a path separator excluded)
filePath: archivePath.substr(index + ASAR_EXTENSION.length + 1)
// "//some/path/to/archive.asar"
const asarPath = archivePath.substr(0, index + ASAR_EXTENSION.length)

// "then/internal.file" (with a path separator excluded)
const filePath = archivePath.substr(index + ASAR_EXTENSION.length + 1)

if (asar.isArchive(asarPath)) {
return { isAsar: true, asarPath, filePath }
} else {
return { isAsar: false }
}
}

Expand Down
2 changes: 2 additions & 0 deletions shell/common/api/atom_api_asar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "native_mate/object_template_builder_deprecated.h"
#include "native_mate/wrappable.h"
#include "shell/common/asar/archive.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/native_mate_converters/file_path_converter.h"
Expand Down Expand Up @@ -133,6 +134,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createArchive", &Archive::Create);
dict.SetMethod("isArchive", &asar::IsArchive);
dict.SetMethod("initAsarSupport", &InitAsarSupport);
}

Expand Down
18 changes: 17 additions & 1 deletion shell/common/asar/asar_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,24 @@ base::LazyInstance<base::ThreadLocalPointer<ArchiveMap>>::Leaky

const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");

std::map<base::FilePath, bool> g_is_archive_cache;

bool IsDirectory(const base::FilePath& path) {
base::File::Info file_info;
return base::GetFileInfo(path, &file_info) ? file_info.is_directory
: path.EndsWithSeparator();
}

} // namespace

bool IsArchive(const base::FilePath& path) {
auto it = g_is_archive_cache.find(path);
if (it != g_is_archive_cache.end()) {
return it->second;
}
return g_is_archive_cache[path] = !IsDirectory(path);
}

std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
if (!g_archive_map_tls.Pointer()->Get())
g_archive_map_tls.Pointer()->Set(new ArchiveMap);
Expand Down Expand Up @@ -59,7 +75,7 @@ bool GetAsarArchivePath(const base::FilePath& full_path,
base::FilePath iter = full_path;
while (true) {
base::FilePath dirname = iter.DirName();
if (iter.MatchesExtension(kAsarExtension))
if (iter.MatchesExtension(kAsarExtension) && asar::IsArchive(iter))
break;
else if (iter == dirname)
return false;
Expand Down
3 changes: 3 additions & 0 deletions shell/common/asar/asar_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace asar {

class Archive;

// Check if the path is an archive (not a directory).
bool IsArchive(const base::FilePath& path);

// Gets or creates a new Archive from the path.
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path);

Expand Down
4 changes: 2 additions & 2 deletions spec-main/api-protocol-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('protocol module', () => {
})

describe('protocol.registerFileProtocol', () => {
const filePath = path.join(fixturesPath, 'asar', 'a.asar', 'file1')
const filePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'file1')
const fileContent = fs.readFileSync(filePath)
const normalPath = path.join(fixturesPath, 'pages', 'a.html')
const normalContent = fs.readFileSync(normalPath)
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('protocol module', () => {
})

it('fails when sending unexist-file', async () => {
const fakeFilePath = path.join(fixturesPath, 'asar', 'a.asar', 'not-exist')
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist')
await registerFileProtocol(protocolName, (request, callback) => callback(fakeFilePath))
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
})
Expand Down

0 comments on commit fe7ab65

Please sign in to comment.