Nội dung bài viết Decode và encode là một trong những cách thường được sử dụng để tránh các cuộc tấn công tập lệnh trên nhiều trang web [XSS] bằng cách mã hóa các ký tự đặc biệt trong một URL. Bài viết này cung cấp cách triển khai cũng như giải thích rõ ràng hơn. Sử dụng Video học lập trình mỗi ngày
0 chuyển đổi một chuỗi thành một định dạng URL hợp lệ để làm cho dữ liệu được truyền đi đáng tin cậy và an toàn hơn. Và sử dụng const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 ngược lại với quá trình mã hóa. Nó chuyển đổi các chuỗi URL được mã hóa và các tham số truy vấn trở lại định dạng bình thường của chúng.const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
Trong bài viết này, bạn sẽ học cách
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 hoặc const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 String URL và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 trong ứng dụng Node.js.Xem thêm: Mẹo viết code javascript và nodejs
URL encode
Trong Node.js xây dựng trên công cụ JavaScript V8 của Chrome hỗ trợ hai phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 đó là const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 để mã hoá một URL. Vậy nó dùng như thế nào?encodeURI javascript
Nếu bạn muốn mã hoá một URL hoàn chỉnh thì có thể sử dụng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6, phương thức này không hỗ trợ các ký tự sau: const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9. Ví dụ sau cho thấy điều đó:const url = '//example.com/!Learn Node$/ Example';
// Encode complete URL
const encodedUrl = encodeURI[url];
// Print encoded URL
console.log[encodedUrl];
// //example.com/!Learn%20Node$/%20Example
encodeURIComponent
Nếu như dùng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 để mã hoá một URL thì việc sử dụng const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 thì chỉ mã hoá const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 hay còn gọi là tham số chuỗi truy vấn chứ không phải một URL hoàn chỉnh. Xem tiếp ví dụ sau:const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
Ngoài phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 thì chúng tôi cung câp thêm cho bạn một module tương tự nhưng nó được tích hợp sẵn trong Node.js để mã hoá URL đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4.querystring module
Bạn cũng có thể sử dụng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 được tích hợp Node.js để mã hóa URL. Module này cung cấp các phương thức tiện ích để phân tích cú pháp và định dạng chuỗi truy vấn URL:const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
decode URL
Trên đó là việc hướng dẫn cũng như giải thích về việc mã hoá
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6. Đương nhiên, khi mã hoá thì phải có giải mã, tương tự khi encode xong thi phải decode.Tương tự thôi
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 cũng cung cấp cho hai methods đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9 để thực hiện giải mã URL.decodeURI javascript
Phương pháp
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 được sử dụng để giải mã một URL đầy đủ:const encodedUrl = '//example.com/!Learn%20Node$/%20Example';
// Decode URL
const url = decodeURI[encodedUrl];
// Print URL
console.log[url];
// //example.com/!Learn Node$/ Example
decodeURIComponent
Chức năng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9 được sử dụng để các thành phần URL giải mã:try {
var a = decodeURIComponent['%E0%A4%A'];
} catch[e] {
console.error[e];
}
// URIError: malformed URI sequence
Lời kết
Bài viết đã quá rõ ràng cho việc sử dụng
const encodedUrl = '//example.com/!Learn%20Node$/%20Example';
// Decode URL
const url = decodeURI[encodedUrl];
// Print URL
console.log[url];
// //example.com/!Learn Node$/ Example
2 rồi, cho nên việc tóm tắt là việc làm dư thừa. Nhưng các bạn cũng nên để ý việc khác nhau giữa const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7, cũng như const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9 trong Node.js. Tránh nhầm lẫn để sử dụng không đúng mục đích của việc làm của mình.Xem thêm: Mẹo viết code javascript và nodejs
URL encode
Trong Node.js xây dựng trên công cụ JavaScript V8 của Chrome hỗ trợ hai phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 đó là const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 để mã hoá một URL. Vậy nó dùng như thế nào?const url = require['url'];
const oldUrl = "//tut.by/ad=%24%7Baccount.domain%7D";
const newUrl = url.parse[oldUrl, true].format[];
encodeURI javascript
{
auth: null
hash: null
host: "tut.by"
hostname: "tut.by"
href: "//tut.by/?ad=%24%7Baccount.domain%7D"
path: "/?ad=%24%7Baccount.domain%7D"
pathname: "/"
port: null
protocol: ""
query: {ad: "${account.domain}"}
search: "?ad=%24%7Baccount.domain%7D"
slashes: true
}
Nếu bạn muốn mã hoá một URL hoàn chỉnh thì có thể sử dụng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6, phương thức này không hỗ trợ các ký tự sau: const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9. Ví dụ sau cho thấy điều đó:const formattedUrl = newUrl.format[];
encodeURIComponent
//tut.by/?ad=%24%7Baccount.domain%7D
Nếu như dùng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 để mã hoá một URL thì việc sử dụng const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 thì chỉ mã hoá const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 hay còn gọi là tham số chuỗi truy vấn chứ không phải một URL hoàn chỉnh. Xem tiếp ví dụ sau://tut.by/?ad=${account.domain}
Ngoài phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 thì chúng tôi cung câp thêm cho bạn một module tương tự nhưng nó được tích hợp sẵn trong Node.js để mã hoá URL đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4.