Skip to content

Commit 2b9b45e

Browse files
nivedinCopilotjamesgeorge007
authored
fix: prevent syncing secret variable initial values (hoppscotch#5434)
Co-authored-by: Copilot <[email protected]> Co-authored-by: jamesgeorge007 <[email protected]>
1 parent 11b07db commit 2b9b45e

File tree

9 files changed

+187
-54
lines changed

9 files changed

+187
-54
lines changed

packages/hoppscotch-common/src/components/environments/my/Details.vue

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,23 @@ const workingEnvID = computed(() => {
436436
437437
const getCurrentValue = (id: string | "Global", varIndex: number) => {
438438
const env = workingEnv.value?.variables[varIndex]
439-
if (env && env.secret) {
439+
if (env?.secret) {
440440
return secretEnvironmentService.getSecretEnvironmentVariable(id, varIndex)
441441
?.value
442442
}
443443
return currentEnvironmentValueService.getEnvironmentVariable(id, varIndex)
444444
?.currentValue
445445
}
446446
447+
const getInitialValue = (id: string | "Global", varIndex: number) => {
448+
const env = workingEnv.value?.variables[varIndex]
449+
if (env?.secret) {
450+
return secretEnvironmentService.getSecretEnvironmentVariable(id, varIndex)
451+
?.initialValue
452+
}
453+
return env?.initialValue
454+
}
455+
447456
watch(
448457
() => props.show,
449458
(show) => {
@@ -469,7 +478,13 @@ watch(
469478
: workingEnvID.value,
470479
index
471480
) ?? e.currentValue,
472-
initialValue: e.initialValue,
481+
initialValue:
482+
getInitialValue(
483+
props.editingEnvironmentIndex === "Global"
484+
? "Global"
485+
: workingEnvID.value,
486+
index
487+
) ?? e.initialValue,
473488
secret: e.secret,
474489
},
475490
}))
@@ -535,6 +550,7 @@ const saveEnvironment = () => {
535550
key: e.key,
536551
value: e.currentValue,
537552
varIndex: i,
553+
initialValue: e.initialValue,
538554
})
539555
: O.none
540556
)
@@ -583,7 +599,7 @@ const saveEnvironment = () => {
583599
A.map((e) => ({
584600
key: e.key,
585601
secret: e.secret,
586-
initialValue: e.initialValue || "",
602+
initialValue: e.secret ? "" : e.initialValue,
587603
currentValue: "",
588604
}))
589605
)

packages/hoppscotch-common/src/components/environments/teams/Details.vue

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ const getCurrentValue = (
419419
)?.currentValue
420420
}
421421
422+
const getInitialValue = (editingID: string, varIndex: number) => {
423+
return secretEnvironmentService.getSecretEnvironmentVariable(
424+
editingID,
425+
varIndex
426+
)?.initialValue
427+
}
428+
422429
watch(
423430
() => props.show,
424431
(show) => {
@@ -449,7 +456,11 @@ watch(
449456
index,
450457
e.secret
451458
) ?? e.currentValue,
452-
initialValue: e.initialValue,
459+
initialValue: e.secret
460+
? (getInitialValue(props.editingEnvironment?.id ?? "", index) ??
461+
e.initialValue ??
462+
"")
463+
: e.initialValue,
453464
secret: e.secret,
454465
},
455466
}))
@@ -516,7 +527,12 @@ const saveEnvironment = async () => {
516527
filteredVariables,
517528
A.filterMapWithIndex((i, e) =>
518529
e.secret
519-
? O.some({ key: e.key, value: e.currentValue, varIndex: i })
530+
? O.some({
531+
key: e.key,
532+
value: e.currentValue,
533+
varIndex: i,
534+
initialValue: e.initialValue,
535+
})
520536
: O.none
521537
)
522538
)
@@ -540,7 +556,7 @@ const saveEnvironment = async () => {
540556
A.map((e) => ({
541557
key: e.key,
542558
secret: e.secret,
543-
initialValue: e.initialValue || "",
559+
initialValue: e.secret ? "" : e.initialValue,
544560
currentValue: "",
545561
}))
546562
)

packages/hoppscotch-common/src/helpers/RequestRunner.ts

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ const updateEnvironments = (
167167
key: e.key,
168168
value: e.currentValue ?? "",
169169
varIndex: index,
170+
initialValue: e.initialValue ?? "",
170171
})
171172

172-
// delete the value from the environment
173-
// so that it doesn't get saved in the environment
174-
173+
// create a new object with cleared values for secret variables
174+
// so that these values don't get saved in the environment
175175
return {
176176
key: e.key,
177177
secret: e.secret,
178-
initialValue: e.initialValue ?? "",
178+
initialValue: e.secret ? "" : (e.initialValue ?? ""),
179179
currentValue: "",
180180
}
181181
}
@@ -210,24 +210,36 @@ const updateEnvironments = (
210210
return updatedEnv
211211
}
212212

213+
/**
214+
* Get the environment variable value from the secret environment service
215+
* @param envID The environment ID
216+
* @param index The index of the environment variable
217+
* @returns Current value and initial value of the environment variable
218+
*/
219+
const getSecretEnvironmentVariableValue = (
220+
envID: string,
221+
index: number
222+
): {
223+
value: string
224+
initialValue?: string
225+
} | null => {
226+
return secretEnvironmentService.getSecretEnvironmentVariableValue(
227+
envID,
228+
index
229+
)
230+
}
231+
213232
/**
214233
* Get the environment variable value from the current environment
215234
* @param envID The environment ID
216235
* @param index The index of the environment variable
217236
* @param isSecret Whether the environment variable is a secret
218-
* @returns The environment variable value
237+
* @returns Current value of the environment variable
219238
*/
220239
const getEnvironmentVariableValue = (
221240
envID: string,
222-
index: number,
223-
isSecret: boolean
241+
index: number
224242
): string | undefined => {
225-
if (isSecret) {
226-
return secretEnvironmentService.getSecretEnvironmentVariableValue(
227-
envID,
228-
index
229-
)
230-
}
231243
return currentEnvironmentValueService.getEnvironmentVariableValue(
232244
envID,
233245
index
@@ -823,6 +835,27 @@ const getUpdatedEnvVariables = (
823835
)
824836
)
825837

838+
// Helper to resolve currentValue & initialValue for (secret/non-secret) env vars
839+
const resolveEnvVars = (
840+
envID: string,
841+
vars: Environment["variables"]
842+
): Environment["variables"] =>
843+
vars.map((v, index) => {
844+
const secretMeta = v.secret
845+
? getSecretEnvironmentVariableValue(envID, index)
846+
: null
847+
return {
848+
...v,
849+
currentValue:
850+
(v.secret
851+
? secretMeta?.value
852+
: getEnvironmentVariableValue(envID, index)) ?? "",
853+
// fallback to var initialValue if secretMeta is not found
854+
initialValue:
855+
(v.secret ? secretMeta?.initialValue : "") ?? v.initialValue,
856+
}
857+
})
858+
826859
function translateToSandboxTestResults(
827860
testDesc: SandboxTestResult
828861
): HoppTestResult {
@@ -834,20 +867,10 @@ function translateToSandboxTestResults(
834867
}
835868
}
836869

837-
const globals = cloneDeep(getGlobalVariables()).map((g, index) => ({
838-
...g,
839-
currentValue: getEnvironmentVariableValue("Global", index, g.secret) ?? "",
840-
}))
841-
842-
const envVars = getCurrentEnvironment().variables.map((e, index) => ({
843-
...e,
844-
currentValue:
845-
getEnvironmentVariableValue(
846-
getCurrentEnvironment().id,
847-
index,
848-
e.secret
849-
) ?? "",
850-
}))
870+
const globals = resolveEnvVars("Global", cloneDeep(getGlobalVariables()))
871+
const { id: currentEnvID, variables: currentEnvVariables } =
872+
getCurrentEnvironment()
873+
const envVars = resolveEnvVars(currentEnvID, currentEnvVariables)
851874

852875
return {
853876
description: "",

packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,15 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
145145
? tooltipEnv.sourceEnvID!
146146
: currentSelectedEnvironment.id
147147

148-
const hasSecretStored = secretEnvironmentService.hasSecretValue(
148+
const hasSecretValueStored = secretEnvironmentService.hasSecretValue(
149149
tooltipSourceEnvID,
150150
tooltipEnv?.key ?? ""
151151
)
152+
const hasSecretInitialValueStored =
153+
secretEnvironmentService.hasSecretInitialValue(
154+
tooltipSourceEnvID,
155+
tooltipEnv?.key ?? ""
156+
)
152157

153158
// We need to check if the environment is a secret and if it has a secret value stored in the secret environment service
154159
// If it is a secret and has a secret value, we need to show "******" in the tooltip
@@ -158,12 +163,12 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
158163
// If the source environment is not found, we need to show "Not Found" in the tooltip, ie the the environment
159164
// is not defined in the selected environment or the global environment
160165
if (isSecret) {
161-
if (!hasSecretStored && envInitialValue) {
166+
if (hasSecretValueStored && hasSecretInitialValueStored) {
162167
envInitialValue = "******"
163-
} else if (hasSecretStored && !envInitialValue) {
164168
envCurrentValue = "******"
165-
} else if (hasSecretStored && envInitialValue) {
169+
} else if (!hasSecretValueStored && hasSecretInitialValueStored) {
166170
envInitialValue = "******"
171+
} else if (hasSecretValueStored && !hasSecretInitialValueStored) {
167172
envCurrentValue = "******"
168173
} else {
169174
envInitialValue = "Empty"

packages/hoppscotch-common/src/helpers/utils/environments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const unWrapEnvironments = (
3636
return {
3737
...globalVar,
3838
currentValue: secretVar.value,
39+
initialValue: secretVar.initialValue ?? "",
3940
}
4041
}
4142
return {
@@ -58,6 +59,7 @@ const unWrapEnvironments = (
5859
return {
5960
...selectedVar,
6061
currentValue: secretVar.value,
62+
initialValue: secretVar.initialValue ?? "",
6163
}
6264
}
6365
return {

packages/hoppscotch-common/src/newstore/environments.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,19 @@ export function getAggregateEnvsWithCurrentValue() {
556556

557557
...currentEnv.variables.map((x, index) => {
558558
let currentValue = x.currentValue
559+
let initialValue = x.initialValue
559560
if (x.secret) {
560561
currentValue =
561562
secretEnvironmentService.getSecretEnvironmentVariableValue(
562563
currentEnv.id,
563564
index
564-
) ?? ""
565+
)?.value ?? ""
566+
567+
initialValue =
568+
secretEnvironmentService.getSecretEnvironmentVariableValue(
569+
currentEnv.id,
570+
index
571+
)?.initialValue ?? ""
565572
}
566573

567574
return <AggregateEnvironment>{
@@ -571,19 +578,26 @@ export function getAggregateEnvsWithCurrentValue() {
571578
currentEnv.id,
572579
index
573580
) ?? currentValue,
574-
initialValue: x.initialValue,
581+
initialValue: x.initialValue ?? initialValue,
575582
secret: x.secret,
576583
sourceEnv: currentEnv.name,
577584
}
578585
}),
579586
...getGlobalVariables().map((x, index) => {
580587
let currentValue = x.currentValue
588+
let initialValue = x.initialValue
581589
if (x.secret) {
582590
currentValue =
583591
secretEnvironmentService.getSecretEnvironmentVariableValue(
584592
"Global",
585593
index
586-
) ?? ""
594+
)?.value ?? ""
595+
596+
initialValue =
597+
secretEnvironmentService.getSecretEnvironmentVariableValue(
598+
"Global",
599+
index
600+
)?.initialValue ?? ""
587601
}
588602
return <AggregateEnvironment>{
589603
key: x.key,
@@ -592,7 +606,7 @@ export function getAggregateEnvsWithCurrentValue() {
592606
"Global",
593607
index
594608
) ?? currentValue,
595-
initialValue: x.initialValue,
609+
initialValue: x.initialValue ?? initialValue,
596610
secret: x.secret,
597611
sourceEnv: "Global",
598612
}
@@ -623,12 +637,19 @@ export const aggregateEnvsWithCurrentValue$: Observable<
623637

624638
selectedEnv?.variables.map((x, index) => {
625639
let currentValue = x.currentValue
640+
let initialValue = x.initialValue
626641
if (x.secret) {
627642
currentValue =
628643
secretEnvironmentService.getSecretEnvironmentVariableValue(
629644
selectedEnv.id,
630645
index
631-
) ?? ""
646+
)?.value ?? ""
647+
648+
initialValue =
649+
secretEnvironmentService.getSecretEnvironmentVariableValue(
650+
selectedEnv.id,
651+
index
652+
)?.initialValue ?? ""
632653
}
633654
results.push({
634655
key: x.key,
@@ -637,20 +658,27 @@ export const aggregateEnvsWithCurrentValue$: Observable<
637658
selectedEnv.id,
638659
index
639660
) ?? currentValue,
640-
initialValue: x.initialValue,
661+
initialValue: x.initialValue ?? initialValue,
641662
secret: x.secret,
642663
sourceEnv: selectedEnv.name,
643664
})
644665
})
645666

646667
globalEnv.variables.map((x, index) => {
647668
let currentValue = x.currentValue
669+
let initialValue = x.initialValue
648670
if (x.secret) {
649671
currentValue =
650672
secretEnvironmentService.getSecretEnvironmentVariableValue(
651673
"Global",
652674
index
653-
) ?? ""
675+
)?.value ?? ""
676+
677+
initialValue =
678+
secretEnvironmentService.getSecretEnvironmentVariableValue(
679+
"Global",
680+
index
681+
)?.initialValue ?? ""
654682
}
655683
results.push({
656684
key: x.key,
@@ -659,7 +687,7 @@ export const aggregateEnvsWithCurrentValue$: Observable<
659687
"Global",
660688
index
661689
) ?? currentValue,
662-
initialValue: x.initialValue,
690+
initialValue: x.initialValue ?? initialValue,
663691
secret: x.secret,
664692
sourceEnv: "Global",
665693
})

0 commit comments

Comments
 (0)