ionic-team / ionic-team/capacitor

bug(core): buildUrlParams creates double ampersands for array parameters

Open
#8,270 0 comments 0 reactions 0 assignees View on GitHub
triage
Dominant language
TypeScript
Stars
16.7k
Forks
1.3k
Avg merge
3d 15h
Merged PRs (30d)
10

Description

## Bug Report

### Description
The `buildUrlParams` function in `core/src/core-plugins.ts` has two bugs:
1. Array parameters produce double ampersands (`&&`) in the output
2. Uses deprecated `substr()` method

### Root Cause

**Bug 1 - Missing assignment (line 336):**
```typescript
value.forEach((str) => {
encodedValue = shouldEncode ? encodeURIComponent(str) : str;
item += `${key}=${encodedValue}&`;
});
// last character will always be "&" so slice it off
item.slice(0, -1); // ❌ Result not assigned!
```

**Bug 2 - Deprecated method (line 346):**
```typescript
return output.substr(1); // ❌ substr is deprecated
```

### Expected Behavior
```typescript
buildUrlParams({ tags: ['javascript', 'typescript'], key: 'value' })
// Should return: "tags=javascript&tags=typescript&key=value"
```

### Actual Behavior
```typescript
buildUrlParams({ tags: ['javascript', 'typescript'], key: 'value' })
// Returns: "tags=javascript&tags=typescript&&key=value" // Note the &&
```

### Proposed Fix

**Fix 1 - Assign the slice result:**
```typescript
item = item.slice(0, -1);
```

**Fix 2 - Replace deprecated substr:**
```typescript
return output.substring(1);
```

### Impact
- **Severity**: Medium (malformed URL parameters)
- **Affected**: HTTP plugin when using array parameters
- **Breaking**: No

### Additional Context
I have a PR ready with both fixes and test cases.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.