Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:35:24+00:00 2026-05-26T03:35:24+00:00

What do I need to do to properly sanitize/escape a parameter that is being

  • 0

What do I need to do to properly sanitize/escape a parameter that is being entered into a programmatic SSH command?

For example, the path parameter –

public boolean exists(String path) {

    try {
        ChannelExec c = (ChannelExec) session.openChannel("exec");

        //Here *** would like to be sure that the path is completely valid
        c.setCommand("[ -f " + path + " ] && echo \"File exists\" || echo \"File does not exists\"");

        InputStream in = c.getInputStream();

        c.connect();

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        IOUtils.copy(in, out);

        in.close();
        out.close();

        System.out.println(out.toString("UTF-8"));
        c.disconnect();

    } catch (JSchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // TODO Auto-generated method stub
    return false;
}

The reason it is unsafe is that the path parameter can come from a user uploaded file. A malicious user could technically upload a file w/ an invalid filename. Although I can check for this beforehand (which I’m doing) I’d also like to check for it here too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:35:25+00:00Added an answer on May 26, 2026 at 3:35 am

    I think a good idea here would be to make sure it is passed as a single parameter to [, and not multiple ones (or even multiple commands). So simply wrap it in ', and replace any ' inside the string by '\''.

    private String escape(String s) {
        return "'" + s.replace("'", "'\\''") + "'";
    }
    

    You also can use ' instead of \" for the echo part of the command, as long as you don’t need variable expansion on the server side (and there are no variables in these strings):

    c.setCommand("[ -f " + escape(path) + " ] && " +
                  "echo 'File exists' || echo 'File does not exist'");
    

    (Note that I also did a tiny grammar fix.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are developing an ASP.Net web application and need that application to work properly
I am in need of a GUI-Toolkit that properly supports OpenGL3 and allow creating
I've read on this page that dynamic arrays need ShareMem unit to work properly.
Overview I'm in need of a way to properly sanitize my MSSQL data. We
For this I think to properly solve it I need to show that sigma(logn)
I need a Guid property in some attribute class like this: public class SomeAttribute
I need to bind width property of the GridView column that is created dynamically
For our application to work properly we need to execute a SQL Statement on
I have some signed .ps1 script, I need to verify they are properly signed
I need to convert minutes (defined as Integer) into the following String format hh:mm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.