gpt4 book ai didi

javascript - 对 HTTPS 地址的 XMLHTTP POST 请求是否使用任何加密?

转载 作者:行者123 更新时间:2023-12-01 01:48:58 25 4
gpt4 key购买 nike

如果我们使用javascript的http请求函数:

var request = new XMLHttpRequest();

发送到 https 地址,这会使用任何类型的加密吗?还是 MITM 能够看到我们发送的所有数据?

示例:

function createAuthToken(baseRestURL, callback) {
var APIPath = "account/api/session";
var CORSPath = "https://cors-anywhere.herokuapp.com/";
var completeRestURL = CORSPath + baseRestURL + APIPath;
console.log("REST API URL: " + completeRestURL);

var method = "POST";
var postData = "{\"tokenId\": \"" + document.getElementById('api_key').value + "\",\"secret\": \"" + document.getElementById('secret').value + "\",\"loginMode\": 1,\"applicationType\": 35}";
var async = true;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && (request.status == 200 || request.status == 201)) {
console.log("ONLOAD");
var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
console.log(status);
var response = JSON.parse(request.responseText);
console.log(response.session_token);
return callback(response.session_token);

}

}
request.open(method, completRestURL, async);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.send(postData);

后续问题:如果没有,是否有办法在我们的客户端 JavaScript 中包含安全的加密?我的想法是在将请求发送到服务器之前使用网站的公钥来加密请求,但我找不到其他人尝试客户端加密。

粗略示例:

enter image description here

var CryptoJS = require("crypto-js");
var stackOverflowKey = "30 82 01 0a 02 82 01..."
var postData = "{\"tokenId\": \"" + document.getElementById('api_key').value + "\",\"secret\": \"" + document.getElementById('secret').value + "\",\"loginMode\": 1,\"applicationType\": 35}";
var encryptedPostData = cryptoJS.hmacSHA256(postData, stackOverflowKey)

//let's skip the callback and request headers as they are the same as above

var request = new XMLHttpRequest();
request.open();
request.send(encryptedPostData);

我没有学习计算机科学,也无法在网上找到任何与此相关的信息。普遍接受的方法是什么?

最佳答案

XMLHttpRequest 中的 HTTP 和 XML 部分一样,只是一个遗留的命名方案。因为所使用的请求不仅可以包含 http 协议(protocol) url,而且接收的也不仅仅是 XML 响应正文。

例如,最初的 W3C 工作草案介绍了 XMLHttpRequest 对象:

https://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/#introduction

The name of the object is XMLHttpRequest for compatibility with the web as it doesn't make much sense otherwise. It supports the transport of other data formats in addition to XML, some implementations support other protocols besides HTTP (that functionality is not covered in this specification though) and the API supports sending data as well.

请注意“一些实现”,因为这是 2006 年的工作草案,因此并非每个人都使用相同的实现。

当前 XMLHttpRequest 的 Whatwg 规范对此名称有这样的规定:

https://xhr.spec.whatwg.org/#introduction

The name XMLHttpRequest is historical and has no bearing on its functionality.

因此,只要所使用的浏览器根据规范实现 XMLHttpRequest,浏览器就会像平常一样处理请求/响应,即对 https 进行加密。

关于javascript - 对 HTTPS 地址的 XMLHTTP POST 请求是否使用任何加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com