已合并
fix: url encode check cannot cover encodeURIComponent type #749
fix: url encode check cannot cover encodeURIComponent type #749
已合并
zichenyang21创建于 2025年4月3日
zichenyang21
zichenyang21
2025年4月3日

Why are these changes being made?

Currently only decodeURI is used to decode and check if input url has been encoded. However, in some cases, the url is encoded by encodeURIComponent, by using encodeURI, special symbol like: /, and : would not be encoded, while encodeURIComponent would translate them to %252F and %252F. Same process would happen in decode.
For example, when input url is: https://api.example.org/v1/qr-generator?url=https%3A%2F%2Fexample.org%2Fuser%2F12345%3Fref%3Dabc%252Fdef%253A
decodeURI has output: "https://api.example.org/v1/qr-generator?url=https%3A%2F%2Fexample.org%2Fuser%2F12345%3Fref%3Dabc%252Fdef%253A", which is the same as input.
decodeURIComponent has output: "https://api.example.org/v1/qr-generator?url=https://example.org/user/12345?ref=abc%2Fdef:", which is different.
if decodeURI has the same output as input for now, double encoding would happen.

At the same time, we do encoding on hostname and pathname by encodeURI, and encode params value by encodeURIComponent, so all special symbols could be encoded formally.

Changelog

change isEncodedURI, check decodeURI and decodeURIComponent at the same time.
change getEncodedURI, parse URL and encode host and pathname using encodeURI, encode params value using encodeURIComponent.
Test Plan
Run the following demo, log the output uri to see it is not double encoded, and all special symbols are encoded correctly.
`
import React, { useState, useEffect } from 'react';
import { View, Text, Button, ActivityIndicator, StyleSheet } from 'react-native';

const App = () => {
const [responseData, setResponseData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

// The URL with encoded parameters
const apiUrl = "https://api.example.org/v1/qr-generator?url=https%3A%2F%2Fexample.org%2Fuser%2F12345%3Fref%3Dabc%252Fdef%253A";
// const apiUrl = "https://api.example.com/search?query={"name"%3A"John"%2C"age"%3A30}"

const fetchData = async () => {
setLoading(true);
setError(null);
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(HTTP error! Status: ${response.status});
}
const data = await response.json(); // Assuming JSON response
setResponseData(data);
} catch (err) {
setError(err.message);
console.error("Request failed:", err);
} finally {
setLoading(false);
}
};

return (

HTTP Request Demo

likedislike
Pull Request已成功合入, 合并人@openharmony_ci
(感谢 zichenyang21 的贡献)
openharmony_ci
openharmony_ci成员
2025年4月3日 评论:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
openharmony_ciopenharmony_ci成员
2025年4月3日 删除了label:dco检查失败
openharmony_ciopenharmony_ci成员
2025年4月3日 添加了label:dco检查成功
zichenyang21
zichenyang21
2025年4月3日 评论:

start build

likedislike
openharmony_ci
openharmony_ci成员
2025年4月3日 评论:

该提交没有关联任何Issue,请先关联一个Issue后再触发构建
The PR is not associated with any Issue, Please associate the PR with one Issue before trigger build.

likedislike
此处折叠了57条消息 查看更多
JackPanJackPan
2025年4月3日 通过了评审
huangyouhuahuangyouhua
2025年4月3日 通过审查
openharmony_ciopenharmony_ci成员
2025年4月3日 关闭了关联的issue
openharmony_ciopenharmony_ci成员
2025年4月3日 合入了pull request
openharmony_devops
openharmony_devops成员
2025年4月3日 评论:

提交次数已达到或超过3次,为了保证上库效率和减少构建资源浪费,请点击链接观看学习视频,谢谢!https://dcp.openharmony.cn/workbench/video/videoDisplay

likedislike