Ruby restriction on string substitutions in a return expression?

Ok, so I'm trying to execute the following code:

`#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}` sleep(10) #give war time to error out and die if its going to `#{@daemon_path} --running --name=#{@app_name}` 

Variable Values:

  • @daemon_path = path / to / daemon (correct for my system)
  • @app_name = foobarbazquux
  • @command = path / to / java (also correct for my system)
  • jetty_jar = method that returns the path to the user bank that gets off the pier (checked, works)
  • @war_path = / path / to / helloworld / war (checked in stand-alone berth container, works)
  • random_port = method that returns a random port number between 10000 and 65535 (temporarily changed it to return 8000 or 22 depending on whether I want to start a war or not)

I get this error (tested commands in bash, they work fine):

Invalid arguments: command not specified

usage: daemon [options] [-] [cmd arg ...]

I fixed the above error by putting quotes around the above commands as follows:

 "`#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}`" "`#{@daemon_path} --running --name=#{@app_name}`" 

Ok, so after executing the code, I check the output with $? and pay attention to the return code 0. It should be 1. I ran it in bash and I will get 1. If I manually placed all the correct values ​​for each variable, it works correctly.

Also, if I execute a script, passing all such values:

 `./daemon_script_file #{@daemon_path} #{@app_name} #{@java_path} #{jetty_jar} #{@war_path} #{random_port}` 

to daemon script file:

 #!/bin/bash set -x d_bin=$1 name=$2 cmd=$3 jar=$4 war=$5 port=$6 $d_bin --name=$name --command=$cmd -- -jar $jar $war $port sleep 10 $d_bin --name=$name --running result=$? exit $result 

I get the following debug trace output:

 + d_bin=/usr/bin/daemon + name= + cmd= + jar= + war= + port= + /usr/bin/daemon --name= --command= -- -jar + sleep 10 + /usr/bin/daemon --name= --running + result=1 + exit 1 sh: 2: foobarbazquux: not found invalid file (bad magic number): Exec format error 

Are there any clues as to why? Am I doing something incredibly stupid here?

as a note, line:

 "#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}" 

permits:

 "/usr/bin/daemon --name=foobarbazquux --command=/usr/java/jdk1.7.0_21/bin/java -- -jar /home/nterry/JettyContainer-1.0.b4-jar-with-dependencies.jar /home/nterry/helloworld.war 8080" 

What exactly right

+4
source share
2 answers

@ Casper: You were right. @daemon_path ended up with an invalid character. Thank you very much.

+1
source

This might be a good idea when interpolating strings into commands to use system() with separate arguments instead of backlinks. Add a new argument for each unclaimed space that you had on the backticks team. For instance:

 system(@daemon_path, "--name=#{@app_name}", "--command=#{@java_path}", '--', '-jar', jetty_jar, @war_path, random_port) sleep(10) #give war time to error out and die if its going to system(@daemon_path, '--running', "--name=#{@app_name}") 

By sending individual arguments to the system (which you cannot do with reverse loops), it ensures that all special shell characters, such as ; , # , " , etc., will be transferred directly to the command being run, and not interpreted by the shell. This is extremely important for safety when any part of the command comes from user input.

It’s also good that @Casper suggested checking for invalid characters in these variables because you probably didn’t want to pass these characters along with your daemon.

+3
source

Source: https://habr.com/ru/post/1486340/


All Articles