public void TestRemoveMetadata() { using (TempFileForActions inputFile = new TempFileForActions(CopyFile(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\TestDocuments\TestSimpleSpreadsheet.xls")))) { LightSpeedExcelCleaningStrategy strategy = new LightSpeedExcelCleaningStrategy(); Type type = typeof(LightSpeedExcelCleaningStrategy); MethodInfo mi = type.GetMethod("CleanFile", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke( strategy, new object[3] { inputFile.TempFile, new List<ContentType>() { ContentType.ContentRule, ContentType.Comment, ContentType.Header, ContentType.Footer, ContentType.TrackChange, ContentType.Field, ContentType.RoutingSlip, ContentType.Links }, new List<Exclusion>() } ); Assert.IsTrue(strategy.VerifyFile(inputFile)); } }
private void ExecuteUsingOfficeOpenXml(TempFileForActions tempFile, CleanActionPropertySet elementsToClean) { try { m_elementsToClean = GetElementsNotRemovedByDomClean(GetListOfEnabledElementsToClean(elementsToClean)); using (BinaryData bData = new BinaryData(tempFile.GetMemoryStream())) { using (XlsxDocumentReader reader = new XlsxDocumentReader(bData)) { using (Stream str = GetOutputStream()) { reader.CleanTo(str, m_elementsToClean); File.Copy(InterimTempFileName, tempFile.TempFile, true); } } } } catch (System.Exception ex) { Logger.LogError(ex); throw; } finally { CleanUp(); } }
public override void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { try { m_elementsToClean = GetListOfEnabledElementsToClean(cleanProperties); List<Exclusion> listExclusion = GetListOfExcludedElements(cleanProperties); using (BinaryData bData = new BinaryData(tempFile.GetMemoryStream())) { using (PptxDocumentReader reader = new PptxDocumentReader(bData)) { using (Stream str = GetOutputStream()) { reader.CleanTo(str, m_elementsToClean, listExclusion); File.Copy(InterimTempFileName, tempFile.TempFile, true); } } } } catch (System.Exception ex) { Logger.LogError("PowerpointX cleaning failed"); Logger.LogError(ex); throw; } finally { CleanUp(); } }
public override void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet elementsToClean) { if (!(bool)elementsToClean[CleanOption.SkipDomCleaning].Value) { ExecuteWithDisabledOptions(() => DoDomRemoval(tempFile.TempFile, elementsToClean), elementsToClean, CleanOption.Macros); } // For elements not removed by DOM clean ExecuteUsingOfficeOpenXml(tempFile, elementsToClean); }
private Stream CallCleanThread(BinaryCleanActionPropertySet cleanProperties, Stream input, ref Dictionary<string, string> streamProperties, CleanPropertiesDisplayTranslator strings) { string filename = streamProperties[strings.StrmProp_DisplayName]; using (TempFileForActions tempFile = new TempFileForActions(System.IO.Path.GetFileName(filename), input)) { CleanData cleanData = new CleanData(cleanProperties, tempFile); Thread staThread = new Thread(new ParameterizedThreadStart(DoClean)); staThread.SetApartmentState(ApartmentState.STA); staThread.Start(cleanData); staThread.Join(); if (cleanData.ex != null) throw cleanData.ex; input.Dispose(); // Clean it out of memory return tempFile.GetMemoryStream(); } }
public override bool VerifyFile(TempFileForActions tempFile) { try { using (PptDocumentReader reader = new PptDocumentReader(tempFile.TempFile, true)) { reader.Read(); return true; } } catch (System.Exception ex) { Logger.LogError("PowerPoint Verification failed"); Logger.LogError(ex); } return false; }
public override bool VerifyFile(TempFileForActions tempFile) { try { using (BinaryData bData = new BinaryData(tempFile.GetMemoryStream())) { PptxDocumentReader reader = new PptxDocumentReader(bData); reader.Read(); } return true; } catch (System.Exception ex) { Logger.LogError("PowerpointX Verification failed"); Logger.LogError(ex); } return false; }
public override void RemoveMacros(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { if ((bool)cleanProperties[CleanOption.Macros].Value == true) { try { using (CWordBinaryMacroCleaner wordMacroCleaner = new CWordBinaryMacroCleaner()) { wordMacroCleaner.Clean(tempFile.TempFile); } } catch (System.Exception ex) { Logger.LogError("Word Binary macro cleaning failed"); Logger.LogError(ex); throw; } } }
public void RemoveMetadata(TempFileForActions tempFile) { if (tempFile == null) { Logger.LogError("Invalid temp file passed to RemoveMetadata"); } FileType fileType = OfficeApplicationCache.GetFileTypeBasedOnFilename(tempFile.TempFile, false); LightSpeedCleanStrategyBase strategy = CreateCleaningStrategy(fileType); if (null == strategy) return; strategy.RemoveMetadata(tempFile, m_cleanProperties); if (!Office2007(fileType)) { strategy.RemoveMacros(tempFile, m_cleanProperties); Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Workshare\ProtectEnterprise\VerifyClean"); bool bVerified = false; String office = "False"; if (key != null) { office = key.GetValue("Office") as String; key.Close(); } if (office.ToLowerInvariant() == "true") bVerified = strategy.VerifyFileWithOffice(tempFile); else bVerified = strategy.VerifyFile(tempFile); if (!bVerified) throw new CleanUserActionException("Cleaned file is corrupt."); } SetFileTimesToCurrentSystemTime(tempFile.TempFile); }
public void TestDocWithUnicodeProps() { using (TempFileForActions inputFile = new TempFileForActions(CopyFile(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\TestDocuments\UnicodeCustomProps.doc")))) { LightSpeedWordCleaningStrategy strategy = new LightSpeedWordCleaningStrategy(); Type type = typeof(LightSpeedWordCleaningStrategy); MethodInfo mi = type.GetMethod("CleanFile", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke( strategy, new object[3] { inputFile.TempFile, new List<ContentType>() { ContentType.ContentRule, ContentType.Comment, ContentType.TrackChange, ContentType.SmallText, ContentType.WhiteText, ContentType.HiddenText, ContentType.SmartTag, ContentType.Field, ContentType.RoutingSlip, ContentType.AttachedTemplate, ContentType.AutoVersion, ContentType.Version, ContentType.BuiltInProperty, ContentType.CustomProperty, ContentType.Variable }, new List<Exclusion>() } ); Assert.IsTrue(strategy.VerifyFileWithOffice(inputFile)); } }
string CleanFile(string file) { var tffa = new TempFileForActions(file); var caps = new CleanActionPropertySet(); caps[CleanOption.SkipDomCleaning].Value = true; var strategy = new WordXCleaningStrategy(); strategy.RemoveMetadata(tffa, caps); return file; }
public override void RemoveMetadata(TempFileForActions tempFile, BinaryCleanActionPropertySet cleanProperties) { List<ContentType> listContentTypes = GetListOfEnabledElementsToClean(cleanProperties); CleanFile(tempFile.TempFile, listContentTypes); }
public override void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { CMetadataElements elementsToClean = GetMetaDataElementsToClean(cleanProperties); CExcludedMetadataElements excludedElements = GetMetaDataElementsToExclude(cleanProperties); CMetadataElements reviewersOnlyElements = new CMetadataElements(); reviewersOnlyElements.SelectNoElements(); reviewersOnlyElements["Reviewers"].Enabled = elementsToClean["Reviewers"].Enabled; int lDttm = 0; bool bExcludeSaveDate = FieldIsExcluded(excludedElements, "savedate"); if (elementsToClean["DocumentStatistics"].Enabled && !bExcludeSaveDate) // if zeroed, do not re-zero { lDttm = -1; // don't bother with dttm Revision } // We clean reviews on the binary level first to get ride of them correctly. if (!(bool) cleanProperties[CleanOption.SkipDomCleaning].Value) { lDttm = ExecuteStrategy3(tempFile.TempFile, lDttm, reviewersOnlyElements, excludedElements, "Metawall.WordBinaryStripper"); DoDomRemoval(tempFile.TempFile, cleanProperties); } // Clean all binary metadata again, the DOM clean would have inserted some metadata. ExecuteStrategy3(tempFile.TempFile, lDttm, elementsToClean, excludedElements, "Metawall.WordBinaryStripper"); }
public virtual void RemoveMacros(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { }
public bool VerifyFileWithOffice(TempFileForActions tempFile) { OfficeApplicationCache officeApplicationCache = OfficeApplicationCache.Instance; int attempts = 10; object document = null; try { MessageFilter.Register(); document = TryToOpenDocument(tempFile.TempFile, ref attempts); officeApplicationCache.CloseDocument(document, true); return true; } catch(Exception ex) { Logger.LogError(ex); return false; } finally { if (null != document) System.Runtime.InteropServices.Marshal.ReleaseComObject(document); MessageFilter.Revoke(); } }
public virtual bool VerifyFile(TempFileForActions tempFile) { return false; }
private Stream CallCleanThread(CleanActionPropertySet cleanProperties, Stream input, ref Dictionary<string, string> streamProperties, CleanPropertiesDisplayTranslator strings, bool callingFallbackThread, bool convertFile) { string originalDisplayName = streamProperties[strings.StrmProp_DisplayName]; string originalFileName = streamProperties[strings.StrmProp_FileName]; convertFile = (InstalledProduct.IsProtectEnterpriseClientInstalled() || InstalledProduct.IsProfessionalInstalled()) && FcsFile.IsRtfFile(input); if (convertFile) { input = ConvertToDoc(input, streamProperties, strings); } string displayName = streamProperties[strings.StrmProp_DisplayName]; using (TempFileForActions tempFile = new TempFileForActions(Path.GetFileName(displayName), input)) { CleanData cleanData = new CleanData(cleanProperties, tempFile); Thread staThread; if (callingFallbackThread) { staThread = new Thread(DoFallbackClean); staThread.Name = MethodBase.GetCurrentMethod().DeclaringType.Name + ".DoFallbackClean"; } else { staThread = new Thread(DoClean); staThread.Name = MethodBase.GetCurrentMethod().DeclaringType.Name + ".DoClean"; } staThread.SetApartmentState(ApartmentState.STA); staThread.Start(cleanData); staThread.Join(); if (cleanData.ex != null) { //IActionProperty iap = null; if (callingFallbackThread || RunningInServerCtxt(cleanProperties)) { m_fallbackTried = false; input.Dispose(); throw cleanData.ex; } cleanData.ex = null; return CallCleanThread(cleanProperties, input, ref streamProperties, strings, true, convertFile); } input.Dispose(); if (!convertFile) { return tempFile.GetMemoryStream(); } return ConvertFromDoc(tempFile, originalFileName, originalDisplayName, streamProperties, strings); } }
public virtual void RemoveMetadata(TempFileForActions tempFile, BinaryCleanActionPropertySet cleanProperties) { }
public void TestVerifyFileWithOfficeWithCorruptFile() { using (TempFileForActions inputFile = new TempFileForActions(CopyFile(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\TestDocuments\Corrupt.doc")))) { LightSpeedWordCleaningStrategy strategy = new LightSpeedWordCleaningStrategy(); Assert.IsFalse(strategy.VerifyFileWithOffice(inputFile), "Verification should fail"); } }
public void TestRemoveMetadataX() { using (TempFileForActions inputFile = new TempFileForActions(CopyFile(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\TestDocuments\TestSimpleDocument.docx")))) { LightSpeedWordXCleaningStrategy strategy = new LightSpeedWordXCleaningStrategy(); strategy.RemoveMetadata(inputFile, new CleanActionPropertySet()); } }
/// <summary> /// Converts a stream from a .doc file back to its original format /// </summary> private static Stream ConvertFromDoc(TempFileForActions tempFile, string originalFileName, string originalDisplayName, IDictionary<string, string> streamProperties, CleanPropertiesDisplayTranslator strings) { string dstPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(originalFileName)); Stream convertedStream; try { using (FileStream fs = new FileStream(tempFile.TempFile, FileMode.Open, FileAccess.Read)) { DocConverterCache.ConvertToRtf(fs, tempFile.TempFile, dstPath); } convertedStream = new MemoryStream(File.ReadAllBytes(dstPath)); streamProperties[strings.StrmProp_FileName] = originalFileName; streamProperties[strings.StrmProp_DisplayName] = originalDisplayName; } finally { File.Delete(dstPath); } return convertedStream; }
public override void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { List<ContentType> listContentTypes = GetListOfEnabledElementsToClean(cleanProperties); List<Exclusion> listExclusion = GetListOfExcludedElements(cleanProperties); CleanFile(tempFile.TempFile, listContentTypes, listExclusion); }
public virtual void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { DoDomRemoval(tempFile.TempFile, cleanProperties); CMetadataElements elementsToClean = GetMetaDataElementsToClean(cleanProperties); CExcludedMetadataElements excludedElements = GetMetaDataElementsToExclude(cleanProperties); ExecuteStrategy2(tempFile.TempFile, elementsToClean, excludedElements, "Workshare.OfficeBinaryStripperStrategy"); }
public CleanData(BinaryCleanActionPropertySet cleanProperties, TempFileForActions tempFile) { this.cleanProperties = cleanProperties; this.tempFile = tempFile; }
public override void RemoveMetadata(TempFileForActions tempFile, CleanActionPropertySet cleanProperties) { Workshare.Protect.Api.CMetadataElements elementsToClean = GetMetaDataElementsToClean(cleanProperties); DoDomRemoval(tempFile.TempFile, cleanProperties); }