Skip to content

Commit f768886

Browse files
committed
feat(webpack): migrate from deprecated url.parse() to WHATWG URL API
Replace the deprecated url.parse() and url.format() methods with the modern WHATWG URL API in PostCSS CLI resources plugins to resolve security and maintainability concerns. - Replace url.parse(inputUrl) with new URL(normalizedUrl, 'file:///') - Replace url.format({ pathname, hash, search }) with simple string concatenation - Replace url.resolve(deployUrl, outputUrl) with new URL(outputUrl, deployUrl).href - Apply changes consistently across webpack, rspack, and angular-rspack packages The deprecated url.parse() method is prone to security vulnerabilities and is not standardized. WHATWG URL API provides a modern, secure, and standardized way to parse URLs while maintaining all existing functionality. Resolves deprecation warnings about url.parse() security implications.
1 parent 731e037 commit f768886

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

packages/angular-rspack/src/lib/utils/postcss-cli-resources.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export default function (options?: PostcssCliResourcesOptions): Plugin {
9696
inputUrl = inputUrl.slice(1);
9797
}
9898

99-
const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
99+
const normalizedUrl = inputUrl.replace(/\\/g, '/');
100+
const parsedUrl = new URL(normalizedUrl, 'file:///');
101+
const { pathname, hash, search } = parsedUrl;
100102
const resolver = (file: string, base: string) =>
101103
new Promise<string>((resolve, reject) => {
102104
loader.resolve(base, decodeURI(file), (err, result) => {
@@ -144,11 +146,11 @@ export default function (options?: PostcssCliResourcesOptions): Plugin {
144146

145147
let outputUrl = outputPath.replace(/\\/g, '/');
146148
if (hash || search) {
147-
outputUrl = url.format({ pathname: outputUrl, hash, search });
149+
outputUrl = outputUrl + (search || '') + (hash || '');
148150
}
149151

150152
if (deployUrl && !extracted) {
151-
outputUrl = url.resolve(deployUrl, outputUrl);
153+
outputUrl = new URL(outputUrl, deployUrl).href;
152154
}
153155

154156
resourceCache.set(cacheKey, outputUrl);

packages/rspack/src/plugins/utils/plugins/postcss-cli-resources.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
9494
resourceCache.set(cacheKey, outputUrl);
9595
return outputUrl;
9696
}
97-
const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
97+
const normalizedUrl = inputUrl.replace(/\\/g, '/');
98+
const parsedUrl = new URL(normalizedUrl, 'file:///');
99+
const { pathname, hash, search } = parsedUrl;
98100
const resolver = (file: string, base: string) =>
99101
new Promise<boolean | string>((resolve, reject) => {
100102
loader.resolve(base, decodeURI(file), (err, result) => {
@@ -125,11 +127,11 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
125127
loader.emitFile(outputPath, content, undefined);
126128
let outputUrl = outputPath.replace(/\\/g, '/');
127129
if (hash || search) {
128-
outputUrl = url.format({ pathname: outputUrl, hash, search });
130+
outputUrl = outputUrl + (search || '') + (hash || '');
129131
}
130132
const loaderOptions: any = loader.loaders[loader.loaderIndex].options;
131133
if (deployUrl && loaderOptions.ident !== 'extracted') {
132-
outputUrl = url.resolve(deployUrl, outputUrl);
134+
outputUrl = new URL(outputUrl, deployUrl).href;
133135
}
134136
resourceCache.set(cacheKey, outputUrl);
135137
resolve(outputUrl);

packages/webpack/src/utils/webpack/plugins/postcss-cli-resources.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
9494
resourceCache.set(cacheKey, outputUrl);
9595
return outputUrl;
9696
}
97-
const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
97+
const normalizedUrl = inputUrl.replace(/\\/g, '/');
98+
const parsedUrl = new URL(normalizedUrl, 'file:///');
99+
const { pathname, hash, search } = parsedUrl;
98100
const resolver = (file: string, base: string) =>
99101
new Promise<boolean | string>((resolve, reject) => {
100102
loader.resolve(base, decodeURI(file), (err, result) => {
@@ -125,11 +127,11 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
125127
loader.emitFile(outputPath, content, undefined);
126128
let outputUrl = outputPath.replace(/\\/g, '/');
127129
if (hash || search) {
128-
outputUrl = url.format({ pathname: outputUrl, hash, search });
130+
outputUrl = outputUrl + (search || '') + (hash || '');
129131
}
130132
const loaderOptions: any = loader.loaders[loader.loaderIndex].options;
131133
if (deployUrl && loaderOptions.ident !== 'extracted') {
132-
outputUrl = url.resolve(deployUrl, outputUrl);
134+
outputUrl = new URL(outputUrl, deployUrl).href;
133135
}
134136
resourceCache.set(cacheKey, outputUrl);
135137
resolve(outputUrl);

0 commit comments

Comments
 (0)