unipdf/sanitize/sanitize.go

71 lines
7.9 KiB
Go
Raw Normal View History

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-30 12:24:05 +00:00
package sanitize ;import (_ce "github.com/unidoc/unipdf/v3/common";_e "github.com/unidoc/unipdf/v3/core";);
// GetSuspiciousObjects returns a count of each detected suspicious object.
func (_gb *Sanitizer )GetSuspiciousObjects ()map[string ]int {return _gb ._ced };
2024-04-16 11:40:43 +00:00
// Optimize optimizes `objects` and returns updated list of objects.
2024-04-30 12:24:05 +00:00
func (_d *Sanitizer )Optimize (objects []_e .PdfObject )([]_e .PdfObject ,error ){return _d .processObjects (objects );};
2024-04-16 11:40:43 +00:00
// Sanitizer represents a sanitizer object.
// It implements the Optimizer interface to access the objects field from the writer.
2024-04-30 12:24:05 +00:00
type Sanitizer struct{_b SanitizationOpts ;_ced map[string ]int ;};func (_ab *Sanitizer )analyze (_ec []_e .PdfObject ){_db :=map[string ]int {};for _ ,_ecf :=range _ec {switch _cga :=_ecf .(type ){case *_e .PdfIndirectObject :_dc ,_ga :=_e .GetDict (_cga .PdfObject );
if _ga {if _dgc ,_eg :=_e .GetName (_dc .Get ("\u0054\u0079\u0070\u0065"));_eg &&*_dgc =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_fce :=_e .GetIndirect (_dc .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_fce {_db ["\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"]++;
};}else if _cdf ,_eca :=_e .GetName (_dc .Get ("\u0053"));_eca {_ef :=_cdf .String ();if _ef =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"||_ef =="\u0055\u0052\u0049"||_ef =="\u0047\u006f\u0054\u006f"||_ef =="\u0047\u006f\u0054o\u0052"||_ef =="\u004c\u0061\u0075\u006e\u0063\u0068"{_db [_ef ]++;
}else if _ef =="\u0052e\u006e\u0064\u0069\u0074\u0069\u006fn"{if _ ,_cfg :=_e .GetStream (_dc .Get ("\u004a\u0053"));_cfg {_db [_ef ]++;};};}else if _fcec :=_dc .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");_fcec !=nil {_db ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
}else if _dbf ,_bg :=_e .GetIndirect (_dc .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));_bg {if _ff ,_ea :=_e .GetDict (_dbf .PdfObject );_ea {if _bdc ,_bdg :=_e .GetDict (_ff .Get ("\u0041\u0041"));_bdg {_dfg :=_bdc .Get ("\u004b");_cb ,_fddb :=_e .GetIndirect (_dfg );
if _fddb {if _afa ,_gcfc :=_e .GetDict (_cb .PdfObject );_gcfc {if _fb ,_beg :=_e .GetName (_afa .Get ("\u0053"));_beg &&*_fb =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_db ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
}else if _ ,_de :=_e .GetString (_afa .Get ("\u004a\u0053"));_de {_db ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;}else {_bge :=_bdc .Get ("\u0046");if _bge !=nil {_geb ,_fbd :=_e .GetIndirect (_bge );if _fbd {if _bag ,_cba :=_e .GetDict (_geb .PdfObject );
_cba {if _ccg ,_gf :=_e .GetName (_bag .Get ("\u0053"));_gf {_gdcg :=_ccg .String ();_db [_gdcg ]++;};};};};};};};};};};};};};_ab ._ced =_db ;};
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-30 12:24:05 +00:00
Launch bool ;};func (_a *Sanitizer )processObjects (_g []_e .PdfObject )([]_e .PdfObject ,error ){_gc :=[]_e .PdfObject {};_ca :=_a ._b ;for _ ,_cd :=range _g {switch _bd :=_cd .(type ){case *_e .PdfIndirectObject :_cea ,_ee :=_e .GetDict (_bd );if _ee {if _eeb ,_be :=_e .GetName (_cea .Get ("\u0054\u0079\u0070\u0065"));
_be &&*_eeb =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_cg :=_e .GetIndirect (_cea .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_cg &&_ca .OpenAction {_cea .Remove ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e");
};}else if _cf ,_cc :=_e .GetName (_cea .Get ("\u0053"));_cc {switch *_cf {case "\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074":if _ca .JavaScript {if _ad ,_gd :=_e .GetStream (_cea .Get ("\u004a\u0053"));_gd {_f :=[]byte {};_cfd ,_caf :=_e .MakeStream (_f ,nil );
if _caf ==nil {*_ad =*_cfd ;};};_ce .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 _ca .URI {_ce .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 _ca .GoTo {_ce .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 _ca .GoToR {_ce .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 _ca .Launch {_ce .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 _ae ,_aed :=_e .GetStream (_cea .Get ("\u004a\u0053"));
_aed {_acb :=[]byte {};_cec ,_dg :=_e .MakeStream (_acb ,nil );if _dg ==nil {*_ae =*_cec ;};};};}else if _fc :=_cea .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");_fc !=nil &&_ca .JavaScript {continue ;}else if _fd ,_fg :=_e .GetName (_cea .Get ("\u0054\u0079\u0070\u0065"));
_fg &&*_fd =="\u0041\u006e\u006eo\u0074"&&_ca .JavaScript {if _gdc ,_fdd :=_e .GetIndirect (_cea .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));_fdd {if _ag ,_eb :=_e .GetDict (_gdc .PdfObject );_eb {if _gcf ,_aa :=_e .GetDict (_ag .Get ("\u0041\u0041"));
_aa {_gcc ,_gg :=_e .GetIndirect (_gcf .Get ("\u004b"));if _gg {if _df ,_ba :=_e .GetDict (_gcc .PdfObject );_ba {if _fa ,_af :=_e .GetName (_df .Get ("\u0053"));_af &&*_fa =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_df .Clear ();
}else if _ge :=_gcf .Get ("\u0046");_ge !=nil {if _bb ,_acg :=_e .GetIndirect (_ge );_acg {if _bdb ,_fab :=_e .GetDict (_bb .PdfObject );_fab {if _bf ,_dfa :=_e .GetName (_bdb .Get ("\u0053"));_dfa &&*_bf =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_bdb .Clear ();
};};};};};};};};};};};case *_e .PdfObjectStream :_ce .Log .Debug ("\u0070d\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0073t\u0072e\u0061m\u0020\u0074\u0079\u0070\u0065\u0020\u0025T",_bd );case *_e .PdfObjectStreams :_ce .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",_bd );
default:_ce .Log .Debug ("u\u006e\u006b\u006e\u006fwn\u0020p\u0064\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0025\u0054",_bd );};_gc =append (_gc ,_cd );};_a .analyze (_gc );return _gc ,nil ;};
// New returns a new sanitizer object.
func New (opts SanitizationOpts )*Sanitizer {return &Sanitizer {_b :opts }};