传递“("和“)"通过 URI 导致 403 错误,我该如何对其进行编码?

2023-05-16前端开发问题
8

本文介绍了传递“("和“)"通过 URI 导致 403 错误,我该如何对其进行编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

(用于 XML HTTP 请求的 JavaScript 和用于执行 SQL 查询的 PHP.)

我正在构建一个执行查询的网络应用程序.它使用 XMLHTTP 请求 GET 方法并将查询传递给执行它的 PHP 脚本.在我在其中引入括号 ( ) 之前,它工作正常.

这是一个如何工作的例子:

函数执行Qry(){qry = document.getElementByID('textarea').value;qryHTTPRequest(encodeURI(qry));//我也试过 encodeURIComponent(qry);}函数 xmlHTTPRequest(qry){//获取urlFetch = "http://my.url.com/script.php?qry=" + qry;}

这是一个快速参考,我知道我的 xmlhttp 请求可以正常工作,因为它可以完成在传递其他查询时需要执行的操作,例如:

SELECT * FROM `tableName`

工作正常,但是当你尝试做类似的事情时

创建表`new_table`AS(选择 * FROM `old_table`)

然后这是它无法执行的时候,我收到 403 错误,所以我认为它与 () 相关,因为我什至在 PHP 本身上尝试了相同的代码,而不必通过它并且它起作用了,所以URL编码过程一定有问题吗?如果这是问题,是否有编码这些字符的方法?我假设还有其他字符没有使用 encodeURI() 方法以及 encodeURIComponent() 进行编码.提前致谢!

解决方案

下面应该这样做:

urlFetch = "http://my.url.com/script.php?qry=" + encodeURIComponent(qry).replace(/(/g, "%28").replace(/)/g, "%29");

括号在 URI 语法中很奇怪.许多编码器将它们视为特殊的,即使它们出现在过时标记"产生中.使用常见的 Web 协议(httphttpsmailto)可以安全地将它们编码为 %28%29 虽然允许 Web 服务器为它们分配特殊含义.您已经在使用 encodeURIencodeURIComponent 所以您已经假设 URL 转义序列是 UTF-8.

来自 RFC 3986:

<块引用>

子分隔符!"/$"/&"/'"/("/)"/*"/+"/,"/;"/="

...

过时的规则翻译标记              "-"/"_"/"."/!"/~"/*"/'"/"("/")"

(JavaScript for the XML HTTP request and PHP for the execution SQL query.)

I'm building a web app that executes queries. It uses the XMLHTTP request GET method and passes a query to a PHP script that executes it. It works fine until I introduce parentheses ( ) in it.

Here is an example of how works:

function executeQry(){
qry = document.getElementByID('textarea').value;
qryHTTPRequest(encodeURI(qry));
//I've also tried encodeURIComponent(qry);
}


function xmlHTTPRequest(qry){
//fetches 
urlFetch = "http://my.url.com/script.php?qry=" + qry;
 }

this is a quick reference, I know that my xmlhttp request works fine because it does what it needs to do when other queries are passed through for example:

SELECT * FROM `tableName`

works fine, but when you try to do something like

CREATE TABLE `new_table`
AS (SELECT * FROM `old_table`)

Then this is when it won't execute, I get the 403 error so I figured that it's an with the () because I even tried this same code on the PHP itself, without having to pass it through and it worked, so there must be an issue with the URL encoding process right? If this is the issue, is there a method for encoding these characters? I assume there are other characters that don't get encoded with encodeURI() method as well as the encodeURIComponent(). Thanks in advance!

解决方案

The below should do it:

urlFetch = "http://my.url.com/script.php?qry=" + encodeURIComponent(qry)
    .replace(/(/g, "%28").replace(/)/g, "%29");

Parentheses are oddballs in the URI grammar. Many encoders treat them as special even though they only appear in the obsolete "mark" production. With common web protocols (http, https, mailto) it is safe to encode them to %28 and %29 though web servers are allowed to assign special meanings to them. You are already using encodeURI or encodeURIComponent so you are already assuming that URL escape sequences are UTF-8.

From RFC 3986:

sub-delims    "!" / "$" / "&" / "'" / "(" / ")"
            / "*" / "+" / "," / ";" / "="

...

obsolete rule     translation
mark              "-" / "_" / "." / "!" / "~" / "*" / "'"
                / "(" / ")"

这篇关于传递“("和“)"通过 URI 导致 403 错误,我该如何对其进行编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13