feat(codegen): validate __TAURI_ISOLATION_HOOK__ is referenced (#4631)

This commit is contained in:
Lucas Fernandes Nogueira 2022-07-11 20:41:34 -03:00 committed by GitHub
parent d5e6f7fa5c
commit 3b4ed970e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-codegen": patch
---
Validate `__TAURI_ISOLATION_HOOK__` being set by a file in the isolation application.

View File

@ -330,13 +330,28 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
let dir = config_parent.join(dir);
if !dir.exists() {
panic!(
"The isolation dir configuration is set to `{:?}` but this path doesn't exist",
"The isolation application path is set to `{:?}` but it does not exist",
dir
)
}
let mut sets_isolation_hook = false;
let key = uuid::Uuid::new_v4().to_string();
let assets = EmbeddedAssets::new(dir.clone(), &options, map_isolation(&options, dir))?;
let map_isolation = map_isolation(&options, dir.clone());
let assets = EmbeddedAssets::new(dir, &options, |key, path, input, csp_hashes| {
// we check if `__TAURI_ISOLATION_HOOK__` exists in the isolation code
// before modifying the files since we inject our own `__TAURI_ISOLATION_HOOK__` reference in HTML files
if String::from_utf8_lossy(input).contains("__TAURI_ISOLATION_HOOK__") {
sets_isolation_hook = true;
}
map_isolation(key, path, input, csp_hashes)
})?;
if !sets_isolation_hook {
panic!("The isolation application does not contain a file setting the `window.__TAURI_ISOLATION_HOOK__` value.");
}
let schema = options.isolation_schema;
quote!(#root::Pattern::Isolation {

View File

@ -246,7 +246,12 @@ impl EmbeddedAssets {
pub fn new(
input: impl Into<EmbeddedAssetsInput>,
options: &AssetOptions,
map: impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError>,
mut map: impl FnMut(
&AssetKey,
&Path,
&mut Vec<u8>,
&mut CspHashes,
) -> Result<(), EmbeddedAssetsError>,
) -> Result<Self, EmbeddedAssetsError> {
// we need to pre-compute all files now, so that we can inject data from all files into a few
let RawEmbeddedAssets { paths, csp_hashes } = RawEmbeddedAssets::new(input.into(), options)?;
@ -262,7 +267,8 @@ impl EmbeddedAssets {
assets: HashMap::new(),
},
move |mut state, (prefix, entry)| {
let (key, asset) = Self::compress_file(&prefix, entry.path(), &map, &mut state.csp_hashes)?;
let (key, asset) =
Self::compress_file(&prefix, entry.path(), &mut map, &mut state.csp_hashes)?;
state.assets.insert(key, asset);
Result::<_, EmbeddedAssetsError>::Ok(state)
},
@ -292,7 +298,12 @@ impl EmbeddedAssets {
fn compress_file(
prefix: &Path,
path: &Path,
map: &impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError>,
map: &mut impl FnMut(
&AssetKey,
&Path,
&mut Vec<u8>,
&mut CspHashes,
) -> Result<(), EmbeddedAssetsError>,
csp_hashes: &mut CspHashes,
) -> Result<Asset, EmbeddedAssetsError> {
let mut input = std::fs::read(path).map_err(|error| EmbeddedAssetsError::AssetRead {