Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bumpy/fix-url-prepend-https.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
varlock: patch
---

Do not prependHttps when a URL already has a protocol
6 changes: 5 additions & 1 deletion packages/varlock/src/env-graph/lib/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ const UrlDataType = createEnvGraphDataType(
generatePlaceholder: (seed) => `https://${seed}.invalid/`,
coerce(rawVal) {
const val = coerceToString(rawVal);
if (settings?.prependHttps && !val.startsWith('https://')) return `https://${val}`;
// Only prepend when no URI scheme is present (docs: "if no protocol is specified").
// Checking only https:// used to turn http://example.com into https://http://example.com.
if (settings?.prependHttps && !/^[a-zA-Z][a-zA-Z\d+.-]*:\/\//.test(val)) {
return `https://${val}`;
}
return val;
},
validate(val) {
Expand Down
9 changes: 9 additions & 0 deletions packages/varlock/src/env-graph/test/data-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ describe('url data type', () => {
expect(g.configSchema.MY_URL.isValid).toBe(true);
expect(g.configSchema.MY_URL.resolvedValue).toBe('https://example.com');
});

it('does not prepend when another protocol is already present', async () => {
const g = await loadAndResolve(outdent`
# @type=url(prependHttps=true)
MY_URL=http://example.com
`);
expect(g.configSchema.MY_URL.isValid).toBe(true);
expect(g.configSchema.MY_URL.resolvedValue).toBe('http://example.com');
});
});

describe('noTrailingSlash', () => {
Expand Down
Loading