mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
72 lines
7.9 KiB
Go
72 lines
7.9 KiB
Go
//
|
|
// 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/
|
|
|
|
package sanitize ;import (_dg "github.com/unidoc/unipdf/v3/common";_a "github.com/unidoc/unipdf/v3/core";);
|
|
|
|
// 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.
|
|
Launch bool ;};
|
|
|
|
// New returns a new sanitizer object.
|
|
func New (opts SanitizationOpts )*Sanitizer {return &Sanitizer {_g :opts }};
|
|
|
|
// Sanitizer represents a sanitizer object.
|
|
// It implements the Optimizer interface to access the objects field from the writer.
|
|
type Sanitizer struct{_g SanitizationOpts ;_f map[string ]int ;};
|
|
|
|
// Optimize optimizes `objects` and returns updated list of objects.
|
|
func (_fb *Sanitizer )Optimize (objects []_a .PdfObject )([]_a .PdfObject ,error ){return _fb .processObjects (objects );};func (_fc *Sanitizer )processObjects (_ab []_a .PdfObject )([]_a .PdfObject ,error ){_de :=[]_a .PdfObject {};_dgd :=_fc ._g ;for _ ,_dec :=range _ab {switch _aa :=_dec .(type ){case *_a .PdfIndirectObject :_aac ,_af :=_a .GetDict (_aa );
|
|
if _af {if _b ,_bg :=_a .GetName (_aac .Get ("\u0054\u0079\u0070\u0065"));_bg &&*_b =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_da :=_a .GetIndirect (_aac .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_da &&_dgd .OpenAction {_aac .Remove ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e");
|
|
};}else if _e ,_daf :=_a .GetName (_aac .Get ("\u0053"));_daf {switch *_e {case "\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074":if _dgd .JavaScript {if _dc ,_gd :=_a .GetStream (_aac .Get ("\u004a\u0053"));_gd {_df :=[]byte {};_eg ,_fd :=_a .MakeStream (_df ,nil );
|
|
if _fd ==nil {*_dc =*_eg ;};};_dg .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 _dgd .URI {_dg .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 _dgd .GoTo {_dg .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 _dgd .GoToR {_dg .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 _dgd .Launch {_dg .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 _bgf ,_ce :=_a .GetStream (_aac .Get ("\u004a\u0053"));_ce {_fcd :=[]byte {};_afd ,_egb :=_a .MakeStream (_fcd ,nil );if _egb ==nil {*_bgf =*_afd ;};};};}else if _dfc :=_aac .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");
|
|
_dfc !=nil &&_dgd .JavaScript {continue ;}else if _bf ,_gb :=_a .GetName (_aac .Get ("\u0054\u0079\u0070\u0065"));_gb &&*_bf =="\u0041\u006e\u006eo\u0074"&&_dgd .JavaScript {if _afc ,_afe :=_a .GetIndirect (_aac .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));
|
|
_afe {if _be ,_ac :=_a .GetDict (_afc .PdfObject );_ac {if _gee ,_deb :=_a .GetDict (_be .Get ("\u0041\u0041"));_deb {_fa ,_dea :=_a .GetIndirect (_gee .Get ("\u004b"));if _dea {if _gf ,_beb :=_a .GetDict (_fa .PdfObject );_beb {if _gfa ,_fbe :=_a .GetName (_gf .Get ("\u0053"));
|
|
_fbe &&*_gfa =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_gf .Clear ();}else if _afef :=_gee .Get ("\u0046");_afef !=nil {if _gbd ,_gea :=_a .GetIndirect (_afef );_gea {if _gbg ,_fdd :=_a .GetDict (_gbd .PdfObject );_fdd {if _cg ,_ag :=_a .GetName (_gbg .Get ("\u0053"));
|
|
_ag &&*_cg =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_gbg .Clear ();};};};};};};};};};};};case *_a .PdfObjectStream :_dg .Log .Debug ("\u0070d\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0073t\u0072e\u0061m\u0020\u0074\u0079\u0070\u0065\u0020\u0025T",_aa );
|
|
case *_a .PdfObjectStreams :_dg .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",_aa );default:_dg .Log .Debug ("u\u006e\u006b\u006e\u006fwn\u0020p\u0064\u0066\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0025\u0054",_aa );
|
|
};_de =append (_de ,_dec );};_fc .analyze (_de );return _de ,nil ;};
|
|
|
|
// GetSuspiciousObjects returns a count of each detected suspicious object.
|
|
func (_gg *Sanitizer )GetSuspiciousObjects ()map[string ]int {return _gg ._f };func (_db *Sanitizer )analyze (_ef []_a .PdfObject ){_aad :=map[string ]int {};for _ ,_agb :=range _ef {switch _agg :=_agb .(type ){case *_a .PdfIndirectObject :_aaf ,_gc :=_a .GetDict (_agg .PdfObject );
|
|
if _gc {if _bgc ,_cc :=_a .GetName (_aaf .Get ("\u0054\u0079\u0070\u0065"));_cc &&*_bgc =="\u0043a\u0074\u0061\u006c\u006f\u0067"{if _ ,_agc :=_a .GetIndirect (_aaf .Get ("\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"));_agc {_aad ["\u004f\u0070\u0065\u006e\u0041\u0063\u0074\u0069\u006f\u006e"]++;
|
|
};}else if _eb ,_cf :=_a .GetName (_aaf .Get ("\u0053"));_cf {_abe :=_eb .String ();if _abe =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"||_abe =="\u0055\u0052\u0049"||_abe =="\u0047\u006f\u0054\u006f"||_abe =="\u0047\u006f\u0054o\u0052"||_abe =="\u004c\u0061\u0075\u006e\u0063\u0068"{_aad [_abe ]++;
|
|
}else if _abe =="\u0052e\u006e\u0064\u0069\u0074\u0069\u006fn"{if _ ,_geb :=_a .GetStream (_aaf .Get ("\u004a\u0053"));_geb {_aad [_abe ]++;};};}else if _ccf :=_aaf .Get ("\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074");_ccf !=nil {_aad ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
|
|
}else if _bd ,_gda :=_a .GetIndirect (_aaf .Get ("\u0050\u0061\u0072\u0065\u006e\u0074"));_gda {if _ae ,_ccc :=_a .GetDict (_bd .PdfObject );_ccc {if _cfg ,_ga :=_a .GetDict (_ae .Get ("\u0041\u0041"));_ga {_fe :=_cfg .Get ("\u004b");_fdf ,_cge :=_a .GetIndirect (_fe );
|
|
if _cge {if _dbc ,_gfg :=_a .GetDict (_fdf .PdfObject );_gfg {if _aef ,_efc :=_a .GetName (_dbc .Get ("\u0053"));_efc &&*_aef =="\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"{_aad ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;
|
|
}else if _ ,_efe :=_a .GetString (_dbc .Get ("\u004a\u0053"));_efe {_aad ["\u004a\u0061\u0076\u0061\u0053\u0063\u0072\u0069\u0070\u0074"]++;}else {_aadc :=_cfg .Get ("\u0046");if _aadc !=nil {_ed ,_fcg :=_a .GetIndirect (_aadc );if _fcg {if _cda ,_ba :=_a .GetDict (_ed .PdfObject );
|
|
_ba {if _cff ,_bga :=_a .GetName (_cda .Get ("\u0053"));_bga {_afa :=_cff .String ();_aad [_afa ]++;};};};};};};};};};};};};};_db ._f =_aad ;}; |