2023-07-28 12:14:31 +00:00
|
|
|
//
|
|
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
|
|
//
|
|
|
|
// This is a commercial product and requires a license to operate.
|
|
|
|
// A trial license can be obtained at https://unidoc.io
|
|
|
|
//
|
|
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
|
2024-04-16 11:40:43 +00:00
|
|
|
package sanitize ;import (_f "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/core";);
|
|
|
|
|
|
|
|
// Optimize optimizes `objects` and returns updated list of objects.
|
|
|
|
func (_fc *Sanitizer )Optimize (objects []_d .PdfObject )([]_d .PdfObject ,error ){return _fc .processObjects (objects );};
|
|
|
|
|
|
|
|
// Sanitizer represents a sanitizer object.
|
|
|
|
// It implements the Optimizer interface to access the objects field from the writer.
|
|
|
|
type Sanitizer struct{_dg SanitizationOpts ;_a map[string ]int ;};func (_fb *Sanitizer )analyze (_dcg []_d .PdfObject ){_geg :=map[string ]int {};for _ ,_bcf :=range _dcg {switch _bcc :=_bcf .(type ){case *_d .PdfIndirectObject :_bdb ,_eba :=_d .GetDict (_bcc .PdfObject );
|
|
|
|
if _eba {if _efe ,_gc :=_d .GetName (_bdb .Get ("\u0054\u0079\u0070\u0065"));_gc &&*_efe =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_ea :=_d .GetIndirect (_bdb .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_ea {_geg ["\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"]++;
|
|
|
|
};}else if _bb ,_ebg :=_d .GetName (_bdb .Get ("\u0053"));_ebg {_fcf :=_bb .String ();if _fcf =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"||_fcf =="\u0055\u0052\u0049"||_fcf =="\u0047\u006f\u0054\u006f"||_fcf =="\u0047\u006f\u0054o\u0052"||_fcf =="\u004c\u0061\u0075\u006e\u0063\u0068"{_geg [_fcf ]++;
|
|
|
|
}else if _fcf =="\u0052e\u006e\u0064\u0069\u0074\u0069\u006fn"{if _ ,_ggc :=_d .GetStream (_bdb .Get ("\u004a\u0053"));_ggc {_geg [_fcf ]++;};};}else if _bdda :=_bdb .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");_bdda !=nil {_geg ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
|
|
|
|
}else if _eaf ,_abe :=_d .GetIndirect (_bdb .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));_abe {if _gge ,_dea :=_d .GetDict (_eaf .PdfObject );_dea {if _dfg ,_cb :=_d .GetDict (_gge .Get ("\u0041\u0041"));_cb {_dfc :=_dfg .Get ("\u004b");_ede ,_ded :=_d .GetIndirect (_dfc );
|
|
|
|
if _ded {if _ffd ,_fce :=_d .GetDict (_ede .PdfObject );_fce {if _cgg ,_bbb :=_d .GetName (_ffd .Get ("\u0053"));_bbb &&*_cgg =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_geg ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
|
|
|
|
}else if _ ,_dff :=_d .GetString (_ffd .Get ("\u004a\u0053"));_dff {_geg ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;}else {_dd :=_dfg .Get ("\u0046");if _dd !=nil {_eg ,_abc :=_d .GetIndirect (_dd );if _abc {if _fbag ,_egc :=_d .GetDict (_eg .PdfObject );
|
|
|
|
_egc {if _bcde ,_ebf :=_d .GetName (_fbag .Get ("\u0053"));_ebf {_gbe :=_bcde .String ();_geg [_gbe ]++;};};};};};};};};};};};};};_fb ._a =_geg ;};
|
|
|
|
|
|
|
|
// GetSuspiciousObjects returns a count of each detected suspicious object.
|
|
|
|
func (_cd *Sanitizer )GetSuspiciousObjects ()map[string ]int {return _cd ._a };
|
|
|
|
|
|
|
|
// New returns a new sanitizer object.
|
|
|
|
func New (opts SanitizationOpts )*Sanitizer {return &Sanitizer {_dg :opts }};func (_g *Sanitizer )processObjects (_ge []_d .PdfObject )([]_d .PdfObject ,error ){_c :=[]_d .PdfObject {};_ce :=_g ._dg ;for _ ,_aa :=range _ge {switch _ee :=_aa .(type ){case *_d .PdfIndirectObject :_fg ,_da :=_d .GetDict (_ee );
|
|
|
|
if _da {if _ga ,_gg :=_d .GetName (_fg .Get ("\u0054\u0079\u0070\u0065"));_gg &&*_ga =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_fcc :=_d .GetIndirect (_fg .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_fcc &&_ce .OpenAction {_fg .Remove ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e");
|
|
|
|
};}else if _cg ,_eb :=_d .GetName (_fg .Get ("\u0053"));_eb {switch *_cg {case "\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074":if _ce .JavaScript {if _de ,_b :=_d .GetStream (_fg .Get ("\u004a\u0053"));_b {_aac :=[]byte {};_dgf ,_ef :=_d .MakeStream (_aac ,nil );
|
|
|
|
if _ef ==nil {*_de =*_dgf ;};};_f .Log .Debug ("\u004a\u0061\u0076\u0061\u0073\u0063\u0072\u0069\u0070\u0074\u0020a\u0063\u0074\u0069\u006f\u006e\u0020\u0073\u006b\u0069\u0070p\u0065\u0064\u002e");continue ;};case "\u0055\u0052\u0049":if _ce .URI {_f .Log .Debug ("\u0055\u0052\u0049\u0020ac\u0074\u0069\u006f\u006e\u0020\u0073\u006b\u0069\u0070\u0070\u0065\u0064\u002e");
|
|
|
|
continue ;};case "\u0047\u006f\u0054\u006f":if _ce .GoTo {_f .Log .Debug ("G\u004fT\u004f\u0020\u0061\u0063\u0074\u0069\u006f\u006e \u0073\u006b\u0069\u0070pe\u0064\u002e");continue ;};case "\u0047\u006f\u0054o\u0052":if _ce .GoToR {_f .Log .Debug ("R\u0065\u006d\u006f\u0074\u0065\u0020G\u006f\u0054\u004f\u0020\u0061\u0063\u0074\u0069\u006fn\u0020\u0073\u006bi\u0070p\u0065\u0064\u002e");
|
|
|
|
continue ;};case "\u004c\u0061\u0075\u006e\u0063\u0068":if _ce .Launch {_f .Log .Debug ("\u004a\u0061\u0076\u0061\u0073\u0063\u0072\u0069\u0070\u0074\u0020a\u0063\u0074\u0069\u006f\u006e\u0020\u0073\u006b\u0069\u0070p\u0065\u0064\u002e");continue ;};case "\u0052e\u006e\u0064\u0069\u0074\u0069\u006fn":if _cee ,_bc :=_d .GetStream (_fg .Get ("\u004a\u0053"));
|
|
|
|
_bc {_ff :=[]byte {};_gb ,_ab :=_d .MakeStream (_ff ,nil );if _ab ==nil {*_cee =*_gb ;};};};}else if _bd :=_fg .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");_bd !=nil &&_ce .JavaScript {continue ;}else if _bdd ,_gf :=_d .GetName (_fg .Get ("\u0054\u0079\u0070\u0065"));
|
|
|
|
_gf &&*_bdd =="\u0041\u006e\u006eo\u0074"&&_ce .JavaScript {if _ffg ,_dc :=_d .GetIndirect (_fg .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));_dc {if _aad ,_ag :=_d .GetDict (_ffg .PdfObject );_ag {if _cf ,_gfe :=_d .GetDict (_aad .Get ("\u0041\u0041"));
|
|
|
|
_gfe {_ba ,_ad :=_d .GetIndirect (_cf .Get ("\u004b"));if _ad {if _df ,_cc :=_d .GetDict (_ba .PdfObject );_cc {if _cgd ,_ffb :=_d .GetName (_df .Get ("\u0053"));_ffb &&*_cgd =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_df .Clear ();
|
|
|
|
}else if _ae :=_cf .Get ("\u0046");_ae !=nil {if _ed ,_ffgb :=_d .GetIndirect (_ae );_ffgb {if _gfd ,_gea :=_d .GetDict (_ed .PdfObject );_gea {if _ec ,_afa :=_d .GetName (_gfd .Get ("\u0053"));_afa &&*_ec =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_gfd .Clear ();
|
|
|
|
};};};};};};};};};};};case *_d .PdfObjectStream :_f .Log .Debug ("\u0070d\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0073t\u0072e\u0061m\u0020\u0074\u0079\u0070\u0065\u0020\u0025T",_ee );case *_d .PdfObjectStreams :_f .Log .Debug ("\u0070\u0064\u0066\u0020\u006f\u0062\u006a\u0065\u0063\u0074\u0020s\u0074\u0072\u0065\u0061\u006d\u0073\u0020\u0074\u0079\u0070e\u0020\u0025\u0054",_ee );
|
|
|
|
default:_f .Log .Debug ("u\u006e\u006b\u006e\u006fwn\u0020p\u0064\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0025\u0054",_ee );};_c =append (_c ,_aa );};_g .analyze (_c );return _c ,nil ;};
|
2023-07-28 12:14:31 +00:00
|
|
|
|
|
|
|
// SanitizationOpts specifies the objects to be removed during sanitization.
|
|
|
|
type SanitizationOpts struct{
|
|
|
|
|
|
|
|
// JavaScript specifies wether JavaScript action should be removed. JavaScript Actions, section 12.6.4.16 of PDF32000_2008
|
|
|
|
JavaScript bool ;
|
|
|
|
|
|
|
|
// URI specifies if URI actions should be removed. 12.6.4.7 URI Actions, PDF32000_2008.
|
|
|
|
URI bool ;
|
|
|
|
|
|
|
|
// GoToR removes remote GoTo actions. 12.6.4.3 Remote Go-To Actions, PDF32000_2008.
|
|
|
|
GoToR bool ;
|
|
|
|
|
|
|
|
// GoTo specifies wether GoTo actions should be removed. 12.6.4.2 Go-To Actions, PDF32000_2008.
|
|
|
|
GoTo bool ;
|
|
|
|
|
|
|
|
// RenditionJS enables removing of `JS` entry from a Rendition Action.
|
|
|
|
// The `JS` entry has a value of text string or stream containing a JavaScript script that shall be executed when the action is triggered.
|
|
|
|
// 12.6.4.13 Rendition Actions Table 214, PDF32000_2008.
|
|
|
|
RenditionJS bool ;
|
|
|
|
|
|
|
|
// OpenAction removes OpenAction entry from the document catalog.
|
|
|
|
OpenAction bool ;
|
|
|
|
|
|
|
|
// Launch specifies wether Launch Action should be removed.
|
|
|
|
// A launch action launches an application or opens or prints a document.
|
|
|
|
// 12.6.4.5 Launch Actions, PDF32000_2008.
|
2024-04-16 11:40:43 +00:00
|
|
|
Launch bool ;};
|