Ruby Project - Prevent a ruby file from directly being called from OS command line

Spandan

I am doing a demo command line project in Ruby. The structure is like this:

/ROOT_DIR
   init.rb
   /SCRIPT_DIR
      (other scripts and files)

I want users to only go into the application using init.rb, but as it stands, anyone can go into the sub-folder and call other ruby scripts directly.

Questions:

  1. What ways can above scenario be prevented?
  2. If I was to use directory permissions, would it get reset when running the code from a Windows machine to on Linux machine?
  3. Is there anything that can be included in Ruby files itself to prevent it from being directly called from OS command line?
Martin Tournoij

You can't do this with file permissions, since the user needs to read the files; removing the read permission means you can't include it either. Removing the execute permission is useful to signal that these file aren't intended to be executed, but won't prevent people from typing ruby incl.rb.


The easiest way is probably to set a global variable in the init.rb script:

#!/usr/bin/env ruby

FROM_INIT = true
require './incl.rb'

puts 'This is init!'

And then check if this variable is defined in the included incl.rb file:

unless defined? FROM_INIT
    puts 'Must be called from init.rb'
    exit 0
end

puts 'This is incl!'

A second method might be checking the value of $PROGRAM_NAME in incl.rb; this stores the current program name (like argv[0] in many other languages):

unless $PROGRAM_NAME.end_with? 'init.rb'
    puts 'Must be called from init.rb'
    exit 0
end

I don't recommend this though, as it's not very future-proof; what if you want to rename init.rb or make a second script?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby Project - Prevent a ruby file from directly being called from OS command line

From Dev

How can I run a ruby file in command line from Java?

From Dev

list of arguments for executing ruby from command line

From Dev

running command line from ruby rspec

From Dev

Prevent function from being called

From Dev

Powershell command not being properly called by Ruby

From Dev

How to 'disable' a certain method in ruby from being called in the console

From Dev

Require a file to be called from the command line

From Dev

How can I run a command line command from within a ruby file?

From Dev

ruby tweet line from text file

From Dev

include file in project from command line

From Dev

How to find out from which line number the method was called in Ruby?

From Dev

Remove " " from line Ruby

From Dev

How to use Ruby's command line in-place-edit mode to remove lines from a text file

From Dev

Printing pdf file directly from dos command line

From Dev

Incorrect ctags called from OS X command line

From Dev

Prevent on('click') from being called twice

From Dev

Prevent destructor from being called manually

From Dev

Prevent templated copy constructor from being called

From Dev

Ruby script - does not print 'Hello world!' from the command line

From Dev

WPF Command not being called from DataTemplate ContextMenu

From Dev

Prevent a command output from being displayed in a pager

From Dev

Prevent line numbers from being copied to clipboard

From Dev

Reading a JSON file from Ruby (on Mac OS X)

From Dev

Ruby setter= method not being called

From Java

Javascript Check in the function, if it being called directly or from an object

From Dev

How to know if a git hook is being called from not within the terminal/command line

From Dev

How can interpreter detect being called from a script as opposed to command line?

From Dev

Create WAR file from command line for Eclipse Dynamic Web Project

Related Related

  1. 1

    Ruby Project - Prevent a ruby file from directly being called from OS command line

  2. 2

    How can I run a ruby file in command line from Java?

  3. 3

    list of arguments for executing ruby from command line

  4. 4

    running command line from ruby rspec

  5. 5

    Prevent function from being called

  6. 6

    Powershell command not being properly called by Ruby

  7. 7

    How to 'disable' a certain method in ruby from being called in the console

  8. 8

    Require a file to be called from the command line

  9. 9

    How can I run a command line command from within a ruby file?

  10. 10

    ruby tweet line from text file

  11. 11

    include file in project from command line

  12. 12

    How to find out from which line number the method was called in Ruby?

  13. 13

    Remove " " from line Ruby

  14. 14

    How to use Ruby's command line in-place-edit mode to remove lines from a text file

  15. 15

    Printing pdf file directly from dos command line

  16. 16

    Incorrect ctags called from OS X command line

  17. 17

    Prevent on('click') from being called twice

  18. 18

    Prevent destructor from being called manually

  19. 19

    Prevent templated copy constructor from being called

  20. 20

    Ruby script - does not print 'Hello world!' from the command line

  21. 21

    WPF Command not being called from DataTemplate ContextMenu

  22. 22

    Prevent a command output from being displayed in a pager

  23. 23

    Prevent line numbers from being copied to clipboard

  24. 24

    Reading a JSON file from Ruby (on Mac OS X)

  25. 25

    Ruby setter= method not being called

  26. 26

    Javascript Check in the function, if it being called directly or from an object

  27. 27

    How to know if a git hook is being called from not within the terminal/command line

  28. 28

    How can interpreter detect being called from a script as opposed to command line?

  29. 29

    Create WAR file from command line for Eclipse Dynamic Web Project

HotTag

Archive