Bug 1908144 - Add support for extracting Widevine plugins from Chrome component update service CRX3 archives. r=media-playback-reviewers,alwu

We already landed the necessary support for CRX3 in bug 1860397. The
difference with the compontent update service is that the Widevine
archives store the plugin files in a different folder within the archive
than before.

This patch makes it so that we extract all binaries/manifest files,
flattening the archive structure in the process. This should continue to
work for our present Widevine update mechanism, as well as the Chrome
component update service in the future.

Differential Revision: https://phabricator.services.mozilla.com/D216703
This commit is contained in:
Andrew Osmond 2024-08-07 20:44:18 +00:00
parent 9cb059b215
commit 4174446e75

View File

@ -6,70 +6,92 @@
const FILE_ENTRY = "201: ";
async function readJarDirectory(jarPath, installToDirPath) {
let extractedPaths = [];
// Construct a jar URI from the file URI so we can use the JAR URI scheme
// handling to navigate inside the zip file.
let jarResponse = await fetch(jarPath);
let dirListing = await jarResponse.text();
let lines = dirListing.split("\n");
let reader = new FileReader();
for (let line of lines) {
if (!line.startsWith(FILE_ENTRY)) {
// Not a file entry, skip.
continue;
}
// code: entry size modified-time type
let lineSplits = line.split(" ");
let jarEntry = lineSplits[1];
let jarType = lineSplits[4];
// Descend into and flatten any subfolders.
if (jarType === "DIRECTORY") {
extractedPaths.push(
...(await readJarDirectory(jarPath + jarEntry, installToDirPath))
);
continue;
}
let fileName = jarEntry.split("/").pop();
// Only keep the binaries and metadata files.
if (
!fileName.endsWith(".info") &&
!fileName.endsWith(".dll") &&
!fileName.endsWith(".dylib") &&
!fileName.endsWith(".sig") &&
!fileName.endsWith(".so") &&
!fileName.endsWith(".txt") &&
fileName !== "LICENSE" &&
fileName !== "manifest.json"
) {
continue;
}
let filePath = jarPath + jarEntry;
let filePathResponse = await fetch(filePath);
let fileContents = await filePathResponse.blob();
let fileData = await new Promise(resolve => {
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsArrayBuffer(fileContents);
});
await IOUtils.makeDirectory(installToDirPath);
// Do not extract into directories. Extract all files to the same
// directory.
let destPath = PathUtils.join(installToDirPath, fileName);
await IOUtils.write(destPath, new Uint8Array(fileData), {
tmpPath: destPath + ".tmp",
});
// Ensure files are writable and executable. Otherwise, we may be
// unable to execute or uninstall them.
await IOUtils.setPermissions(destPath, 0o700);
if (IOUtils.delMacXAttr) {
// If we're on MacOS Firefox will add the quarantine xattr to files it
// downloads. In this case we want to clear that xattr so we can load
// the CDM.
try {
await IOUtils.delMacXAttr(destPath, "com.apple.quarantine");
} catch (e) {
// Failed to remove the attribute. This could be because the profile
// exists on a file system without xattr support.
//
// Don't fail the extraction here, as in this case it's likely we
// didn't set quarantine on these files in the first place.
}
}
extractedPaths.push(destPath);
}
return extractedPaths;
}
onmessage = async function (msg) {
try {
let extractedPaths = [];
// Construct a jar URI from the file URI so we can use the JAR URI scheme
// handling to navigate inside the zip file.
let jarPath = "jar:" + msg.data.zipURI + "!/";
let jarResponse = await fetch(jarPath);
let dirListing = await jarResponse.text();
let lines = dirListing.split("\n");
let reader = new FileReader();
for (let line of lines) {
if (!line.startsWith(FILE_ENTRY)) {
// Not a file entry, skip.
continue;
}
let lineSplits = line.split(" ");
let fileName = lineSplits[1];
// We don't need these types of files.
if (
fileName == "verified_contents.json" ||
fileName == "icon-128x128.png" ||
fileName.startsWith("_")
) {
continue;
}
let filePath = jarPath + fileName;
let filePathResponse = await fetch(filePath);
let fileContents = await filePathResponse.blob();
let fileData = await new Promise(resolve => {
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsArrayBuffer(fileContents);
});
let installToDirPath = PathUtils.join(
await PathUtils.getProfileDir(),
...msg.data.relativeInstallPath
);
await IOUtils.makeDirectory(installToDirPath);
// Do not extract into directories. Extract all files to the same
// directory.
let destPath = PathUtils.join(installToDirPath, fileName);
await IOUtils.write(destPath, new Uint8Array(fileData), {
tmpPath: destPath + ".tmp",
});
// Ensure files are writable and executable. Otherwise, we may be
// unable to execute or uninstall them.
await IOUtils.setPermissions(destPath, 0o700);
if (IOUtils.delMacXAttr) {
// If we're on MacOS Firefox will add the quarantine xattr to files it
// downloads. In this case we want to clear that xattr so we can load
// the CDM.
try {
await IOUtils.delMacXAttr(destPath, "com.apple.quarantine");
} catch (e) {
// Failed to remove the attribute. This could be because the profile
// exists on a file system without xattr support.
//
// Don't fail the extraction here, as in this case it's likely we
// didn't set quarantine on these files in the first place.
}
}
extractedPaths.push(destPath);
}
let installToDirPath = PathUtils.join(
await PathUtils.getProfileDir(),
...msg.data.relativeInstallPath
);
let extractedPaths = await readJarDirectory(jarPath, installToDirPath);
postMessage({
result: "success",
extractedPaths,