"Save As" Button not working anymore  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

fcl
User
Posts: 2
Joined: Tue Jul 11, 2023 12:27 pm

"Save As" Button not working anymore

Post by fcl »

Good day everyone,

I hope you can help me with my problem.

For my company I have created a PDF form which is used to order material. The form has a button with JavaScript. The Javascript "collects" the values of the fields of the form and puts them together as a string. When clicked the PDF file gets automatically saved in the same location with its file name.

The following code worked in the past. Currently V10.0.1 build 371 is installed.

// splice supplier (get first word in field)
var supplier = this.getField("3").value;

// always cut after 10
var supplierNew = supplier.slice(0, 9);

// if space found, cut:
if (supplierNew.indexOf(" ")){
var supplierNewNew = supplierNew.split(" ", 1);
} else {
var supplierNewNew = supplierNew;
};

// make a file name from the field values
var fileName = this.getField("1").value + "_" + this.getField("Group2").value + "_" + this.getField("Dropdown4").value + "_" + supplierNewNew + "_" + this.getField("54").value;

fileName = fileName.replace("\\","");
fileName = fileName.replace("/","");
fileName = fileName.replace(":","");
fileName = fileName.replace("*","");
fileName = fileName.replace("?","");
fileName = fileName.replace("\"","");
fileName = fileName.replace("<","");
fileName = fileName.replace(">","");
fileName = fileName.replace("|","");

// get the path where the file is currently located
var filePath = this.path.replace(this.documentFileName, "");

// create the new full path
var newFullFilePath = filePath + fileName + ".pdf";

if ((this.getField("1").value.length < 1) || (this.getField("Group2").value.length < 1) || (this.getField("Dropdown4").value.length < 1) || (this.getField("3").value.length < 1) || (this.getField("54").value.length < 1) || (this.getField("56").value.length < 1)){
app.alert("Es sind nicht alle Pflichtfelder (*) ausgefüllt!");
} else {
try {
this.saveAs(newFullFilePath);
} catch (e) {
app.alert("Error! Could not save as: " + newFullFilePath + e);
}
};


I have read something about privileged access or an external JS file!?
image.png
Any help appreciated.

Cheers,
Finn
You do not have the required permissions to view the files attached to this post.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3556
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada

Re: "Save As" Button not working anymore

Post by Ivan - Tracker Software »

In V10 we improved security handling in JavaScript.
The doc.saveAs function works only in privileged contexts as stated in its description.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
User avatar
Roman - Tracker Supp
Site Admin
Posts: 306
Joined: Sun Nov 21, 2004 3:19 pm

Re: "Save As" Button not working anymore  SOLVED

Post by Roman - Tracker Supp »

Hello, Finn,

According to the Adobe Acrobat JS API specification, Doc.saveAs method can only be called in a privileged context - i.e. either from the JS console or an application initialization script, or by using privilege elevation functions app.beginPriv/endPriv, which, it their turn, can only be used in a trusted or trust propagator function.
To fix your script you need to create a trusted function that will call Doc.saveAs in a privileged context.

Here is an example.
Add this code to a .js file in %APPDATA%\Tracker Software\PDFXEditor\3.0\JavaScripts\ directory, so that it will be executed as an application initialization script:

Code: Select all

const trustedSaveAs = app.trustedFunction(
	(doc, path) => {
		app.beginPriv();
		doc.saveAs(path);
		app.endPriv(); // this is actually unnecessary - elevated privileges have function scope
	}
);
Then you can use trustedSaveAs function in your regular scripts, i.e. instead of doc.saveAs(path) use trustedSaveAs(doc, path)

However, other scripts will be able to use your trustedSaveAs() as well, thus compromising security. So it is recommended that trusted function should not implement generic tasks (like saving a document to an arbitrary path). In your case you can make the trusted function more specific by moving there some of your script logic related to saving the document. And add rigorous validation of the function arguments. This will make that trusted function difficult to exploit by a malicious script.
fcl
User
Posts: 2
Joined: Tue Jul 11, 2023 12:27 pm

Re: "Save As" Button not working anymore

Post by fcl »

Thank you guys, that worked and helped me alot.
Roman - Tracker Supp wrote: Tue Jul 11, 2023 6:47 pm Hello, Finn,

According to the Adobe Acrobat JS API specification, [url=https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#saveas] Doc.saveAs[/url] method can only be called in a privileged context - i.e. either from the JS console or an application initialization script, or by using privilege elevation functions app.beginPriv/endPriv, which, it their turn, can only be used in a trusted or trust propagator function.
To fix your script you need to create a trusted function that will call Doc.saveAs in a privileged context.

Here is an example.
Add this code to a .js file in %APPDATA%\Tracker Software\PDFXEditor\3.0\JavaScripts\ directory, so that it will be executed as an application initialization script:

Code: Select all

const trustedSaveAs = app.trustedFunction(
	(doc, path) => {
		app.beginPriv();
		doc.saveAs(path);
		app.endPriv(); // this is actually unnecessary - elevated privileges have function scope
	}
);
Then you can use trustedSaveAs function in your regular scripts, i.e. instead of doc.saveAs(path) use trustedSaveAs(doc, path)

However, other scripts will be able to use your trustedSaveAs() as well, thus compromising security. So it is recommended that trusted function should not implement generic tasks (like saving a document to an arbitrary path). In your case you can make the trusted function more specific by moving there some of your script logic related to saving the document. And add rigorous validation of the function arguments. This will make that trusted function difficult to exploit by a malicious script.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: "Save As" Button not working anymore

Post by Tracker Supp-Stefan »

Hello fcl,

Happy to hear that! :)

Kind regards,
Stefan