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

  • Home
  • SEARCH
  • 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 6690275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:37:04+00:00 2026-05-26T05:37:04+00:00

We know that using string concatenation to form SQL queries renders a program vulnerable

  • 0

We know that using string concatenation to form SQL queries renders a program vulnerable to SQL injection. I usually get around that by using parameter features provided by the API of whatever database software I’m using.

But I haven’t heard of this being a problem in regular system programming. Consider the following code as part of a program that allows a user to write to files in his private directory only.

Scanner scanner = new Scanner(System.in);
String directoryName = "Bob";
String filePath = null;
String text = "some text";

System.out.print("Enter a file to write to: ");
filePath = scanner.nextLine();

// Write to the file in Bob's personal directory for this program (i.e. Bob/textfile.txt)
FileOutputStream file = new FileOutputStream(directoryName + "/" + filePath);
file.write(text.getBytes());

Is the second-last line a vulnerability? If so, how can the program be made more secure (particularly in Java, C++ and C#)? One way is to validate input for escape characters. Anything else?

  • 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-26T05:37:04+00:00Added an answer on May 26, 2026 at 5:37 am

    The simplest solution here is to have a whitelist of acceptable characters. Modifying your original code (to include Java conventions, since you said you’re new…)

    package javawhitelist;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class JavaWhiteListExample {
    
        public static void main(String[] args) throws IOException {
    
            Scanner scanner = new Scanner(System.in); 
            String directoryName = "Bob"; 
            String filePath = null; 
            FileWriter stream = null;
            String text = "some text";  
            System.out.print("Enter a file to write to: "); 
            filePath = scanner.nextLine();  
            String WHITELIST = "[^0-9A-Za-z]+";
            Pattern p = Pattern.compile(WHITELIST);
            Matcher m = p.matcher(filePath);
    
            //You need to do m.find() because m.matches() looks for COMPLETE match
            if(m.find()){ 
                //reject input.
                System.out.println("Invalid input.");
            }else{
                // Write to the file in Bob's home directory (i.e. Bob/textfile.txt) 
                try{
                    File toWrite = new File(directoryName + File.separator + filePath);
    
                    if(toWrite.canWrite()){
                        stream = new FileWriter(toWrite);
                        stream.write(text);
                    }   
                }catch(FileNotFoundException e){
                    e.printStackTrace();
                }catch(IOException e){
                    e.printStackTrace();
                }finally{
                    if(stream != null){
                        stream.close();
                    }
                }
    
            }
        }
    }
    

    The default implementation of any JVM runs with all the access permissions of the user. Using the File.canWrite() method will help ensure that the user won’t write over a file he/she has no permission to. The MOST secure solution (specifying EXACTLY where the file will go) would be to use
    com.sun.security.auth.module.UnixSystem.getName()
    and use that to build the
    /home/$USER
    part of the directory name. Some solutions might tell you to use
    System.getProperty("user.home"):
    or some such, but those rely upon easily changeable environment variables.

    I tried to be thorough, I hope this helps.

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

Sidebar

Related Questions

I know that using single quotes around a string in PHP is faster than
I know that using Dim currUser As String = Request.ServerVariables(LOGON_USER) returns the Domain\Username, but
I know how to replace text in a string. But that's using keyboard (ASCII)
In PHP, I know that using parameterized queries is the best way to prevent
I'm using Spring, Spring Security, BlazeDS, Flex and spring-flex. I know that I can
I know that buffer overruns are one potential hazard to using C-style strings (char
I know that using continuous integration improves the quality of my code base, and
I know that using SharePoint internally is free, but what if I create an
I know that just using rand() is predictable, if you know what you're doing,
I know that systems are using their own time zone databases at different levels.

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.