douduan3203 2014-11-07 14:16
浏览 48
已采纳

如何隐藏对我的javascript文件的直接访问?

I'm running a LAMP web server.

I'd like to include my script files on my page with:

<script src="http://domain.com/script.js"></script>

I would like visiting http://domain.com/script.js to display either an error, or a blank page.

I've seen other similar questions, of which the answer was "just obfuscate it", or "security by obfuscation is bad".

This isn't for the sake of security. I'm wanting to stop bots from pulling my code automatically. I'm ok with human users getting the code. I would simply like this as an alternative to obfuscation.

I've already attempted this with the use of base64_encoded $_GET and $_SESSION parameters. I'm wondering if there's a more elegant solution out there.


CLARIFICATION:

I am aware that Javascript is still available to the user. I am perfectly fine with the code being accessible via Firebug, Chrome's developer tools, etc. I simply want the code accessible via my tags, and inaccessible directly. This is not for security, and not to "hide" my code.


Clarification 2:

The reason I need this is because our company recently found a competitor running scripts to scrape data off of our site. I would like to be able to prevent the data from being scraped via their script, and force them to do it manually.

  • 写回答

5条回答 默认 最新

  • duanqia9034 2014-11-07 20:02
    关注

    I opted to just pursue the $_SESSION/$_GET/$_POST-gated script I had started before visiting StackOverflow.

    The solution's not perfect, but it suits my needs, in that the scripts are accessible via my tags, but inaccessible directly. This is a simplified version of what I am doing:

    File 1 is the PHP file generating the HTML page the user sees. This file creates a random value, and sets the value to the session. The script file File 2 is included using this random value as a GET parameter.

    File 1:

    <?php
    session_start();
    $gate['first_gate'] = crypt((time() * md_rand()) . 'salt');
    $gate['second_gate'] = null;
    $_SESSION['gate'] = json_encode($gate);
    ?>
    <html>
        ...
        <!--this is just the HTML page including the script-->
        <script src="file_2.php?gate=<?=base64_encode(json_encode($gate))?>"></script>
        ...
    </html>
    

    File 2 is the PHP file functioning as a gate for the actual JavaScript code. It verifies that the randomized session variable is equal to the GET parameter, then grabs the code from File 3 using a POST request.

    File 2:

     <?php
     $session_gate = json_decode($_SESSION['gate']);
     $get_gate = json_decode(base64_decode($_GET['gate']));
     //Exit if the session value != the get value
     if($get_gate->first_gate != $session_gate->first_gate) exit;
    
     //Set first gate to null to prevent re-visit
     $session_gate->first_gate = null;
     $session_gate->second_gate = crypt((time() * md_rand()) . 'salt');
     $_SESSION['gate'] = json_encode($session_gate);
     header('Content-Type: application/javascript');
     ?>
     //This is visible via "view source" (then clicking on the script's URL)
     //Grab the actual JS file, hidden behind a POST "wall"
     $.post("file_3.php", { gate: '<?=base64_encode($_SESSION['gate'])?>' });
    

    File 3 is inaccessible when directly viewing the page, as it exits without the POST data from File 2. Bots will still be able to ping it with a POST request, so some additional safety measures should be added here.

    File 3:

     <?php
     $session_gate = json_decode($_SESSION['gate']);
     $post_gate = json_decode(base64_decode($_POST['gate']));
     //Exit without a POST request. Use a more specific value, other than
     //the $_POST superglobal by itself (just using $_POST for illustrative purposes)
     if(!$_POST) exit; //or print an error message
     //Exit if the session value != the get value
     if($get_gate->second_gate != $session_gate->second_gate) exit;
    
     //Set both gates to null to prevent re-visit
     $session_gate->first_gate = null;
     $session_gate->second_gate = null;
     $_SESSION['gate'] = json_encode($session_gate);
     //Additional safety measures (such as IP address/HOST check) here, if desired
     header('Content-Type: application/javascript');
     ?>
     //Javascript code here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)