1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
|
//! ref: composer/src/Composer/Config.php
mod config_source_interface;
mod json_config_source;
pub use config_source_interface::*;
pub use json_config_source::*;
use crate::io::io_interface;
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
E_USER_DEPRECATED, FILTER_VALIDATE_URL, PHP_URL_HOST, PHP_URL_SCHEME, PhpMixed,
RuntimeException, array_key_exists, array_merge_recursive, array_reverse, array_search_mixed,
array_unique, current, empty, filter_var, implode, in_array, is_array, is_int, is_string, key,
max, parse_url, reset, rtrim, strtolower, strtoupper, strtr, substr, trigger_error,
};
use std::cell::RefCell;
use crate::advisory::Auditor;
use crate::downloader::TransportException;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::util::Platform;
use crate::util::ProcessExecutor;
#[derive(Debug)]
pub struct Config {
/// @var array<string, mixed>
config: IndexMap<String, PhpMixed>,
/// @var ?non-empty-string
base_dir: Option<String>,
/// @var array<int|string, mixed>
repositories: IndexMap<String, PhpMixed>,
config_source: Option<Box<dyn ConfigSourceInterface>>,
auth_config_source: Option<Box<dyn ConfigSourceInterface>>,
local_auth_config_source: Option<Box<dyn ConfigSourceInterface>>,
use_environment: bool,
/// @var array<string, true>
warned_hosts: IndexMap<String, bool>,
/// @var array<string, true>
ssl_verify_warned_hosts: IndexMap<String, bool>,
/// @var array<string, string>
// TODO(phase-b): RefCell to allow `&self` access from Config::get / get_with_flags.
source_of_config_value: RefCell<IndexMap<String, String>>,
}
impl Config {
pub const SOURCE_DEFAULT: &'static str = "default";
pub const SOURCE_COMMAND: &'static str = "command";
pub const SOURCE_UNKNOWN: &'static str = "unknown";
pub const RELATIVE_PATHS: i64 = 1;
/// @var array<string, mixed>
pub fn default_config() -> IndexMap<String, PhpMixed> {
let mut c: IndexMap<String, PhpMixed> = IndexMap::new();
c.insert("process-timeout".to_string(), PhpMixed::Int(300));
c.insert("use-include-path".to_string(), PhpMixed::Bool(false));
c.insert(
"allow-plugins".to_string(),
PhpMixed::Array(IndexMap::new()),
);
c.insert(
"use-parent-dir".to_string(),
PhpMixed::String("prompt".to_string()),
);
c.insert(
"preferred-install".to_string(),
PhpMixed::String("dist".to_string()),
);
let mut audit: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
audit.insert(
"ignore".to_string(),
Box::new(PhpMixed::Array(IndexMap::new())),
);
audit.insert(
"abandoned".to_string(),
Box::new(PhpMixed::String(Auditor::ABANDONED_FAIL.to_string())),
);
c.insert("audit".to_string(), PhpMixed::Array(audit));
c.insert("notify-on-install".to_string(), PhpMixed::Bool(true));
c.insert(
"github-protocols".to_string(),
PhpMixed::List(vec![
Box::new(PhpMixed::String("https".to_string())),
Box::new(PhpMixed::String("ssh".to_string())),
Box::new(PhpMixed::String("git".to_string())),
]),
);
c.insert("gitlab-protocol".to_string(), PhpMixed::Null);
c.insert(
"vendor-dir".to_string(),
PhpMixed::String("vendor".to_string()),
);
c.insert(
"bin-dir".to_string(),
PhpMixed::String("{$vendor-dir}/bin".to_string()),
);
c.insert(
"cache-dir".to_string(),
PhpMixed::String("{$home}/cache".to_string()),
);
c.insert(
"data-dir".to_string(),
PhpMixed::String("{$home}".to_string()),
);
c.insert(
"cache-files-dir".to_string(),
PhpMixed::String("{$cache-dir}/files".to_string()),
);
c.insert(
"cache-repo-dir".to_string(),
PhpMixed::String("{$cache-dir}/repo".to_string()),
);
c.insert(
"cache-vcs-dir".to_string(),
PhpMixed::String("{$cache-dir}/vcs".to_string()),
);
c.insert("cache-ttl".to_string(), PhpMixed::Int(15552000)); // 6 months
c.insert("cache-files-ttl".to_string(), PhpMixed::Null); // fallback to cache-ttl
c.insert(
"cache-files-maxsize".to_string(),
PhpMixed::String("300MiB".to_string()),
);
c.insert("cache-read-only".to_string(), PhpMixed::Bool(false));
c.insert(
"bin-compat".to_string(),
PhpMixed::String("auto".to_string()),
);
c.insert("discard-changes".to_string(), PhpMixed::Bool(false));
c.insert("autoloader-suffix".to_string(), PhpMixed::Null);
c.insert("sort-packages".to_string(), PhpMixed::Bool(false));
c.insert("optimize-autoloader".to_string(), PhpMixed::Bool(false));
c.insert("classmap-authoritative".to_string(), PhpMixed::Bool(false));
c.insert("apcu-autoloader".to_string(), PhpMixed::Bool(false));
c.insert("prepend-autoloader".to_string(), PhpMixed::Bool(true));
c.insert(
"update-with-minimal-changes".to_string(),
PhpMixed::Bool(false),
);
c.insert(
"github-domains".to_string(),
PhpMixed::List(vec![Box::new(PhpMixed::String("github.com".to_string()))]),
);
c.insert(
"bitbucket-expose-hostname".to_string(),
PhpMixed::Bool(true),
);
c.insert("disable-tls".to_string(), PhpMixed::Bool(false));
c.insert("secure-http".to_string(), PhpMixed::Bool(true));
c.insert("secure-svn-domains".to_string(), PhpMixed::List(vec![]));
c.insert("cafile".to_string(), PhpMixed::Null);
c.insert("capath".to_string(), PhpMixed::Null);
c.insert("github-expose-hostname".to_string(), PhpMixed::Bool(true));
c.insert(
"gitlab-domains".to_string(),
PhpMixed::List(vec![Box::new(PhpMixed::String("gitlab.com".to_string()))]),
);
c.insert(
"store-auths".to_string(),
PhpMixed::String("prompt".to_string()),
);
c.insert("platform".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert(
"archive-format".to_string(),
PhpMixed::String("tar".to_string()),
);
c.insert("archive-dir".to_string(), PhpMixed::String(".".to_string()));
c.insert("htaccess-protect".to_string(), PhpMixed::Bool(true));
c.insert("use-github-api".to_string(), PhpMixed::Bool(true));
c.insert("lock".to_string(), PhpMixed::Bool(true));
c.insert(
"platform-check".to_string(),
PhpMixed::String("php-only".to_string()),
);
c.insert(
"bitbucket-oauth".to_string(),
PhpMixed::Array(IndexMap::new()),
);
c.insert("github-oauth".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert("gitlab-oauth".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert("gitlab-token".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert("http-basic".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert("bearer".to_string(), PhpMixed::Array(IndexMap::new()));
c.insert(
"custom-headers".to_string(),
PhpMixed::Array(IndexMap::new()),
);
c.insert("bump-after-update".to_string(), PhpMixed::Bool(false));
c.insert(
"allow-missing-requirements".to_string(),
PhpMixed::Bool(false),
);
c.insert(
"client-certificate".to_string(),
PhpMixed::Array(IndexMap::new()),
);
c.insert(
"forgejo-domains".to_string(),
PhpMixed::List(vec![Box::new(PhpMixed::String("codeberg.org".to_string()))]),
);
c.insert(
"forgejo-token".to_string(),
PhpMixed::Array(IndexMap::new()),
);
c
}
/// @var array<string, mixed>
pub fn default_repositories() -> IndexMap<String, PhpMixed> {
let mut r: IndexMap<String, PhpMixed> = IndexMap::new();
let mut packagist: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
packagist.insert(
"type".to_string(),
Box::new(PhpMixed::String("composer".to_string())),
);
packagist.insert(
"url".to_string(),
Box::new(PhpMixed::String("https://repo.packagist.org".to_string())),
);
r.insert("packagist.org".to_string(), PhpMixed::Array(packagist));
r
}
/// @param bool $useEnvironment Use COMPOSER_ environment variables to replace config settings
/// @param ?string $baseDir Optional base directory of the config
pub fn new(use_environment: bool, base_dir: Option<String>) -> Self {
let mut this = Self {
// load defaults
config: Self::default_config(),
repositories: Self::default_repositories(),
use_environment,
base_dir: base_dir.filter(|s| is_string(&PhpMixed::String(s.clone())) && !s.is_empty()),
config_source: None,
auth_config_source: None,
local_auth_config_source: None,
warned_hosts: IndexMap::new(),
ssl_verify_warned_hosts: IndexMap::new(),
source_of_config_value: RefCell::new(IndexMap::new()),
};
let config_clone = this.config.clone();
for (config_key, config_value) in &config_clone {
this.set_source_of_config_value(config_value, config_key, Self::SOURCE_DEFAULT);
}
let repositories_clone = this.repositories.clone();
for (config_key, config_value) in &repositories_clone {
this.set_source_of_config_value(
config_value,
&format!("repositories.{}", config_key),
Self::SOURCE_DEFAULT,
);
}
this
}
/// Changing this can break path resolution for relative config paths so do not call this without knowing what you are doing
///
/// The $baseDir should be an absolute path and without trailing slash
pub fn set_base_dir(&mut self, base_dir: Option<String>) {
self.base_dir = base_dir;
}
pub fn set_config_source(&mut self, source: Box<dyn ConfigSourceInterface>) {
self.config_source = Some(source);
}
pub fn get_config_source(&self) -> &dyn ConfigSourceInterface {
self.config_source.as_ref().unwrap().as_ref()
}
pub fn get_config_source_mut(&mut self) -> &mut dyn ConfigSourceInterface {
self.config_source.as_mut().unwrap().as_mut()
}
pub fn set_auth_config_source(&mut self, source: Box<dyn ConfigSourceInterface>) {
self.auth_config_source = Some(source);
}
pub fn get_auth_config_source(&self) -> &dyn ConfigSourceInterface {
self.auth_config_source.as_ref().unwrap().as_ref()
}
pub fn get_auth_config_source_mut(&mut self) -> &mut dyn ConfigSourceInterface {
self.auth_config_source.as_mut().unwrap().as_mut()
}
pub fn set_local_auth_config_source(&mut self, source: Box<dyn ConfigSourceInterface>) {
self.local_auth_config_source = Some(source);
}
pub fn get_local_auth_config_source(&self) -> Option<&dyn ConfigSourceInterface> {
self.local_auth_config_source.as_deref()
}
pub fn get_local_auth_config_source_mut(
&mut self,
) -> Option<&mut (dyn ConfigSourceInterface + 'static)> {
self.local_auth_config_source
.as_mut()
.map(|b| &mut **b as &mut dyn ConfigSourceInterface)
}
/// Merges new config values with the existing ones (overriding)
///
/// @param array{config?: array<string, mixed>, repositories?: array<mixed>} $config
pub fn merge(&mut self, config: &IndexMap<String, PhpMixed>, source: &str) {
// override defaults with given config
let config_section = config.get("config").cloned().unwrap_or(PhpMixed::Null);
if !empty(&config_section) && is_array(&config_section) {
let config_section_map = match config_section {
PhpMixed::Array(m) => m,
_ => IndexMap::new(),
};
for (key, val_box) in &config_section_map {
let val = (**val_box).clone();
if in_array(
PhpMixed::String(key.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("bitbucket-oauth".to_string())),
Box::new(PhpMixed::String("github-oauth".to_string())),
Box::new(PhpMixed::String("gitlab-oauth".to_string())),
Box::new(PhpMixed::String("gitlab-token".to_string())),
Box::new(PhpMixed::String("http-basic".to_string())),
Box::new(PhpMixed::String("bearer".to_string())),
Box::new(PhpMixed::String("client-certificate".to_string())),
Box::new(PhpMixed::String("forgejo-token".to_string())),
]),
true,
) && self.config.contains_key(key)
{
let existing = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
self.config.insert(
key.clone(),
array_merge_recursive(vec![existing, val.clone()]),
);
self.set_source_of_config_value(&val, key, source);
} else if in_array(
PhpMixed::String(key.clone()),
&PhpMixed::List(vec![Box::new(PhpMixed::String(
"allow-plugins".to_string(),
))]),
true,
) && self.config.contains_key(key)
&& is_array(self.config.get(key).unwrap_or(&PhpMixed::Null))
&& is_array(&val)
{
// merging $val first to get the local config on top of the global one, then appending the global config,
// then merging local one again to make sure the values from local win over global ones for keys present in both
let existing = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
self.config.insert(
key.clone(),
array_merge_recursive(vec![val.clone(), existing, val.clone()]),
);
self.set_source_of_config_value(&val, key, source);
} else if in_array(
PhpMixed::String(key.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("gitlab-domains".to_string())),
Box::new(PhpMixed::String("github-domains".to_string())),
]),
true,
) && self.config.contains_key(key)
{
let existing = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
let merged = array_merge_recursive(vec![existing, val.clone()]);
let unique_list: Vec<String> = match &merged {
PhpMixed::List(l) => l
.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect(),
_ => vec![],
};
let deduped = array_unique(&unique_list);
self.config.insert(
key.clone(),
PhpMixed::List(
deduped
.into_iter()
.map(|s| Box::new(PhpMixed::String(s)))
.collect(),
),
);
self.set_source_of_config_value(&val, key, source);
} else if key == "preferred-install" && self.config.contains_key(key) {
let mut val = val.clone();
let existing = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
if is_array(&val) || is_array(&existing) {
if is_string(&val) {
let mut m = IndexMap::new();
m.insert("*".to_string(), Box::new(val.clone()));
val = PhpMixed::Array(m);
}
let existing = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
if is_string(&existing) {
let mut m = IndexMap::new();
m.insert("*".to_string(), Box::new(existing));
self.config.insert(key.clone(), PhpMixed::Array(m));
self.source_of_config_value
.borrow_mut()
.insert(format!("{}*", key), source.to_string());
}
let cur = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
self.config
.insert(key.clone(), array_merge_recursive(vec![cur, val.clone()]));
self.set_source_of_config_value(&val, key, source);
// the full match pattern needs to be last
let has_wildcard = matches!(
self.config.get(key),
Some(PhpMixed::Array(m)) if m.contains_key("*")
);
if has_wildcard {
if let Some(PhpMixed::Array(m)) = self.config.get_mut(key) {
if let Some(wildcard) = m.shift_remove("*") {
m.insert("*".to_string(), wildcard);
}
}
}
} else {
self.config.insert(key.clone(), val.clone());
self.set_source_of_config_value(&val, key, source);
}
} else if key == "audit" {
let current_ignores = self
.config
.get("audit")
.and_then(|v| v.as_array())
.and_then(|m| m.get("ignore"))
.cloned()
.map(|b| *b)
.unwrap_or(PhpMixed::List(vec![]));
let merged = array_merge_recursive(vec![
self.config.get("audit").cloned().unwrap_or(PhpMixed::Null),
val.clone(),
]);
self.config.insert(key.clone(), merged);
self.set_source_of_config_value(&val, key, source);
let val_ignore = match &val {
PhpMixed::Array(m) => m
.get("ignore")
.cloned()
.map(|b| *b)
.unwrap_or(PhpMixed::List(vec![])),
_ => PhpMixed::List(vec![]),
};
let new_ignores = array_merge_recursive(vec![current_ignores, val_ignore]);
if let Some(PhpMixed::Array(audit)) = self.config.get_mut("audit") {
audit.insert("ignore".to_string(), Box::new(new_ignores));
}
} else {
self.config.insert(key.clone(), val.clone());
self.set_source_of_config_value(&val, key, source);
}
}
}
let repositories_section = config
.get("repositories")
.cloned()
.unwrap_or(PhpMixed::Null);
if !empty(&repositories_section) && is_array(&repositories_section) {
// PHP: array_reverse on IndexMap preserves keys (preserve_keys=true)
self.repositories = self
.repositories
.iter()
.rev()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
let new_repos_map: IndexMap<String, PhpMixed> = match &repositories_section {
PhpMixed::Array(m) => m.iter().map(|(k, v)| (k.clone(), (**v).clone())).collect(),
_ => IndexMap::new(),
};
let new_repos: IndexMap<String, PhpMixed> = new_repos_map.into_iter().rev().collect();
for (name, repository) in &new_repos {
// disable a repository by name
// this is a code path, that will be used less as the next check will be preferred
if matches!(repository, PhpMixed::Bool(false)) {
self.disable_repo_by_name(&name.to_string());
continue;
}
// disable a repository with an anonymous {"name": false} repo
if is_array(&repository)
&& repository.as_array().map(|m| m.len()).unwrap_or(0) == 1
&& matches!(current(repository.clone()), PhpMixed::Bool(false))
{
self.disable_repo_by_name(&key(repository.clone()).unwrap_or_default());
continue;
}
// auto-deactivate the default packagist.org repo if it gets redefined
let is_composer = repository
.as_array()
.and_then(|m| m.get("type"))
.and_then(|v| v.as_string())
== Some("composer");
let repo_url = repository
.as_array()
.and_then(|m| m.get("url"))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
if is_composer
&& Preg::is_match(
r"{^https?://(?:[a-z0-9-.]+\.)?packagist.org(/|$)}",
&repo_url,
)
.unwrap_or(false)
{
self.disable_repo_by_name("packagist.org");
}
// store repo
// TODO(phase-b): is_int($name) where $name is an IndexMap key (PHP string-or-int)
let is_numeric_name = name.parse::<i64>().is_ok();
if is_numeric_name {
if !self.repositories.contains_key(name) {
self.repositories.insert(name.clone(), repository.clone());
} else {
// PHP: $this->repositories[] = $repository
// appending to numeric-keyed map
let next_idx = self.repositories.len();
self.repositories
.insert(next_idx.to_string(), repository.clone());
}
let found_key = array_search_mixed(
repository,
&PhpMixed::Array(
self.repositories
.iter()
.map(|(k, v)| (k.clone(), Box::new(v.clone())))
.collect(),
),
true,
)
.and_then(|v| v.as_string().map(|s| s.to_string()))
.unwrap_or_default();
self.set_source_of_config_value(
repository,
&format!("repositories.{}", found_key),
source,
);
} else if name == "packagist" {
// BC support for default "packagist" named repo
self.repositories
.insert(format!("{}.org", name), repository.clone());
self.set_source_of_config_value(
repository,
&format!("repositories.{}.org", name),
source,
);
} else {
self.repositories.insert(name.clone(), repository.clone());
self.set_source_of_config_value(
repository,
&format!("repositories.{}", name),
source,
);
}
}
self.repositories = self
.repositories
.iter()
.rev()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
}
}
/// @return array<int|string, mixed>
pub fn get_repositories(&self) -> IndexMap<String, PhpMixed> {
self.repositories.clone()
}
/// Returns a setting
///
/// @param int $flags Options (see class constants)
/// @throws \RuntimeException
///
/// @return mixed
pub fn get(&self, key: &str) -> PhpMixed {
self.get_with_flags(key, 0).unwrap_or(PhpMixed::Null)
}
// TODO(phase-b): typed convenience; PHP's Config::get() returns mixed.
pub fn get_str(&self, key: &str) -> Result<String> {
Ok(self
.get_with_flags(key, 0)?
.as_string()
.unwrap_or_default()
.to_string())
}
pub fn get_with_flags(&self, key: &str, flags: i64) -> Result<PhpMixed> {
match key {
// strings/paths with env var and {$refs} support
"vendor-dir" | "bin-dir" | "process-timeout" | "data-dir" | "cache-dir"
| "cache-files-dir" | "cache-repo-dir" | "cache-vcs-dir" | "cafile" | "capath" => {
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
let env = format!("COMPOSER_{}", strtoupper(&strtr(key, "-", "_")));
let val = self.get_composer_env(&env);
if !matches!(val, PhpMixed::Bool(false)) {
self.set_source_of_config_value(&val, key, &env);
}
if key == "process-timeout" {
let raw = if matches!(val, PhpMixed::Bool(false)) {
self.config.get(key).cloned().unwrap_or(PhpMixed::Null)
} else {
val.clone()
};
return Ok(PhpMixed::Int(max(0, raw.as_int().unwrap_or(0))));
}
let raw_val = if matches!(val, PhpMixed::Bool(false)) {
self.config.get(key).cloned().unwrap_or(PhpMixed::Null)
} else {
val
};
let processed = self.process(raw_val, flags);
let mut val_str = rtrim(processed.as_string().unwrap_or(""), Some("/\\"));
val_str = Platform::expand_path(&val_str);
if substr(key, -4, None) != "-dir" {
return Ok(PhpMixed::String(val_str));
}
Ok(PhpMixed::String(
if (flags & Self::RELATIVE_PATHS) == Self::RELATIVE_PATHS {
val_str
} else {
self.realpath(&val_str)
},
))
}
// booleans with env var support
"cache-read-only" | "htaccess-protect" => {
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
let env = format!("COMPOSER_{}", strtoupper(&strtr(key, "-", "_")));
let val = self.get_composer_env(&env);
let val = if matches!(val, PhpMixed::Bool(false)) {
self.config.get(key).cloned().unwrap_or(PhpMixed::Null)
} else {
self.set_source_of_config_value(&val, key, &env);
val
};
Ok(PhpMixed::Bool(
val.as_string() != Some("false")
&& val.as_bool().unwrap_or_else(|| !val.is_null()),
))
}
// booleans without env var support
"disable-tls" | "secure-http" | "use-github-api" | "lock" => {
// special case for secure-http
if key == "secure-http"
&& self.get_with_flags("disable-tls", 0)?.as_bool() == Some(true)
{
return Ok(PhpMixed::Bool(false));
}
let v = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
Ok(PhpMixed::Bool(
v.as_string() != Some("false") && v.as_bool().unwrap_or(false),
))
}
// ints without env var support
"cache-ttl" => Ok(PhpMixed::Int(max(
0,
self.config.get(key).and_then(|v| v.as_int()).unwrap_or(0),
))),
// numbers with kb/mb/gb support, without env var support
"cache-files-maxsize" => {
let raw = self
.config
.get(key)
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
let mut matches: IndexMap<CaptureKey, String> = IndexMap::new();
if !Preg::is_match3(
r"/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i",
&raw,
Some(&mut matches),
)
.unwrap_or(false)
{
return Err(RuntimeException {
message: format!("Could not parse the value of '{}': {}", key, raw),
code: 0,
}
.into());
}
let mut size = matches
.get(&CaptureKey::ByIndex(1))
.cloned()
.unwrap_or_default()
.parse::<f64>()
.unwrap_or(0.0);
let unit = matches.get(&CaptureKey::ByIndex(2)).cloned();
if let Some(unit) = unit {
match strtolower(&unit).as_str() {
"g" => {
size *= 1024.0;
size *= 1024.0;
size *= 1024.0;
}
"m" => {
size *= 1024.0;
size *= 1024.0;
}
"k" => {
size *= 1024.0;
}
_ => {}
}
}
Ok(PhpMixed::Int(max(0, size as i64)))
}
// special cases below
"cache-files-ttl" => {
let v = self.config.get(key).cloned();
if let Some(v) = v {
if !v.is_null() {
return Ok(PhpMixed::Int(max(0, v.as_int().unwrap_or(0))));
}
}
self.get_with_flags("cache-ttl", 0)
}
"home" => {
let v = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
let expanded = Platform::expand_path(v.as_string().unwrap_or(""));
let processed = self.process(PhpMixed::String(expanded), flags);
Ok(PhpMixed::String(rtrim(
processed.as_string().unwrap_or(""),
Some("/\\"),
)))
}
"bin-compat" => {
let env_val = self.get_composer_env("COMPOSER_BIN_COMPAT");
let value = match env_val {
PhpMixed::Bool(false) | PhpMixed::Null => self
.config
.get(key)
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string(),
other => other.as_string().unwrap_or("").to_string(),
};
if !in_array(
PhpMixed::String(value.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("auto".to_string())),
Box::new(PhpMixed::String("full".to_string())),
Box::new(PhpMixed::String("proxy".to_string())),
Box::new(PhpMixed::String("symlink".to_string())),
]),
false,
) {
return Err(RuntimeException {
message: format!(
"Invalid value for 'bin-compat': {}. Expected auto, full or proxy",
value
),
code: 0,
}
.into());
}
if value == "symlink" {
trigger_error(
"config.bin-compat \"symlink\" is deprecated since Composer 2.2, use auto, full (for Windows compatibility) or proxy instead.",
E_USER_DEPRECATED,
);
}
Ok(PhpMixed::String(value))
}
"discard-changes" => {
let env = self.get_composer_env("COMPOSER_DISCARD_CHANGES");
if !matches!(env, PhpMixed::Bool(false)) {
let env_str = env.as_string().unwrap_or("").to_string();
if !in_array(
PhpMixed::String(env_str.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("stash".to_string())),
Box::new(PhpMixed::String("true".to_string())),
Box::new(PhpMixed::String("false".to_string())),
Box::new(PhpMixed::String("1".to_string())),
Box::new(PhpMixed::String("0".to_string())),
]),
true,
) {
return Err(RuntimeException {
message: format!(
"Invalid value for COMPOSER_DISCARD_CHANGES: {}. Expected 1, 0, true, false or stash",
env_str
),
code: 0,
}
.into());
}
if env_str == "stash" {
return Ok(PhpMixed::String("stash".to_string()));
}
// convert string value to bool
return Ok(PhpMixed::Bool(
env_str != "false" && !env_str.is_empty() && env_str != "0",
));
}
let val = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
let allowed = matches!(&val, PhpMixed::Bool(_)) || val.as_string() == Some("stash");
if !allowed {
return Err(RuntimeException {
message: format!(
"Invalid value for 'discard-changes': {:?}. Expected true, false or stash",
val
),
code: 0,
}
.into());
}
Ok(val)
}
"github-protocols" => {
let mut protos: Vec<String> = self
.config
.get("github-protocols")
.and_then(|v| v.as_list())
.map(|l| {
l.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default();
let secure_http = self
.config
.get("secure-http")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if secure_http {
let map: IndexMap<String, String> = protos
.iter()
.enumerate()
.map(|(i, s)| (i.to_string(), s.clone()))
.collect();
let found = array_search_mixed(
&PhpMixed::String("git".to_string()),
&PhpMixed::Array(
map.into_iter()
.map(|(k, v)| (k, Box::new(PhpMixed::String(v))))
.collect(),
),
false,
);
if let Some(idx_val) = found {
let idx = idx_val
.as_string()
.unwrap_or("")
.parse::<usize>()
.unwrap_or(usize::MAX);
if idx < protos.len() {
protos.remove(idx);
}
}
}
let first = reset(&protos);
if first.as_deref() == Some("http") {
return Err(RuntimeException {
message: "The http protocol for github is not available anymore, update your config's github-protocols to use \"https\", \"git\" or \"ssh\"".to_string(),
code: 0,
}
.into());
}
Ok(PhpMixed::List(
protos
.into_iter()
.map(|s| Box::new(PhpMixed::String(s)))
.collect(),
))
}
"autoloader-suffix" => {
let v = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
if v.as_string() == Some("") {
// we need to guarantee null or non-empty-string
return Ok(PhpMixed::Null);
}
Ok(self.process(v, flags))
}
"audit" => {
let mut result = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
let abandoned_env = self.get_composer_env("COMPOSER_AUDIT_ABANDONED");
if !matches!(abandoned_env, PhpMixed::Bool(false)) {
let abandoned_env_str = abandoned_env.as_string().unwrap_or("").to_string();
let valid_choices: Vec<String> =
Auditor::ABANDONEDS.iter().map(|s| s.to_string()).collect();
if !in_array(
PhpMixed::String(abandoned_env_str.clone()),
&PhpMixed::List(
valid_choices
.iter()
.map(|s| Box::new(PhpMixed::String(s.clone())))
.collect(),
),
true,
) {
return Err(RuntimeException {
message: format!(
"Invalid value for COMPOSER_AUDIT_ABANDONED: {}. Expected one of {}.",
abandoned_env_str,
implode(", ", &valid_choices),
),
code: 0,
}
.into());
}
if let PhpMixed::Array(ref mut m) = result {
m.insert(
"abandoned".to_string(),
Box::new(PhpMixed::String(abandoned_env_str)),
);
}
}
let block_abandoned_env =
self.get_composer_env("COMPOSER_SECURITY_BLOCKING_ABANDONED");
if !matches!(block_abandoned_env, PhpMixed::Bool(false)) {
let env_str = block_abandoned_env.as_string().unwrap_or("").to_string();
if !in_array(
PhpMixed::String(env_str.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("0".to_string())),
Box::new(PhpMixed::String("1".to_string())),
]),
true,
) {
return Err(RuntimeException {
message: format!(
"Invalid value for COMPOSER_SECURITY_BLOCKING_ABANDONED: {}. Expected 0 or 1.",
env_str
),
code: 0,
}
.into());
}
if let PhpMixed::Array(ref mut m) = result {
m.insert(
"block-abandoned".to_string(),
Box::new(PhpMixed::Bool(env_str == "1")),
);
}
}
Ok(result)
}
_ => {
if !self.config.contains_key(key) {
return Ok(PhpMixed::Null);
}
let v = self.config.get(key).cloned().unwrap_or(PhpMixed::Null);
Ok(self.process(v, flags))
}
}
}
/// @return array<string, mixed[]>
pub fn all(&mut self, flags: i64) -> Result<IndexMap<String, PhpMixed>> {
let mut all: IndexMap<String, PhpMixed> = IndexMap::new();
all.insert(
"repositories".to_string(),
PhpMixed::Array(
self.get_repositories()
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
),
);
let keys: Vec<String> = self.config.keys().cloned().collect();
let mut config_section: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
for key in keys {
config_section.insert(key.clone(), Box::new(self.get_with_flags(&key, flags)?));
}
all.insert("config".to_string(), PhpMixed::Array(config_section));
Ok(all)
}
pub fn get_source_of_value(&mut self, key: &str) -> String {
let _ = self.get(key);
self.source_of_config_value
.borrow()
.get(key)
.cloned()
.unwrap_or_else(|| Self::SOURCE_UNKNOWN.to_string())
}
/// @param mixed $configValue
fn set_source_of_config_value(&self, config_value: &PhpMixed, path: &str, source: &str) {
self.source_of_config_value
.borrow_mut()
.insert(path.to_string(), source.to_string());
if is_array(config_value) {
let map = match config_value {
PhpMixed::Array(m) => m
.iter()
.map(|(k, v)| (k.clone(), (**v).clone()))
.collect::<Vec<_>>(),
_ => vec![],
};
for (key, value) in map {
self.set_source_of_config_value(&value, &format!("{}.{}", path, key), source);
}
}
}
/// @return array<string, mixed[]>
pub fn raw(&self) -> IndexMap<String, PhpMixed> {
let mut result: IndexMap<String, PhpMixed> = IndexMap::new();
result.insert(
"repositories".to_string(),
PhpMixed::Array(
self.get_repositories()
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
),
);
result.insert(
"config".to_string(),
PhpMixed::Array(
self.config
.iter()
.map(|(k, v)| (k.clone(), Box::new(v.clone())))
.collect(),
),
);
result
}
/// Checks whether a setting exists
pub fn has(&self, key: &str) -> bool {
array_key_exists(key, &self.config)
}
/// Replaces {$refs} inside a config string
///
/// @param string|mixed $value a config string that can contain {$refs-to-other-config}
/// @param int $flags Options (see class constants)
///
/// @return string|mixed
fn process(&self, value: PhpMixed, flags: i64) -> PhpMixed {
if !is_string(&value) {
return value;
}
let value_str = value.as_string().unwrap_or("").to_string();
// TODO(phase-b): Preg::replace_callback with a closure that calls &mut self.get_with_flags
let mut result = value_str.clone();
let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match_strict_groups3(r"#\{\$(.+)\}#", &value_str, Some(&mut m)).unwrap_or(false)
{
let key_match = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default();
let replacement = self
.get_with_flags(&key_match, flags)
.ok()
.and_then(|v| v.as_string().map(|s| s.to_string()))
.unwrap_or_default();
result = result.replace(&format!("{{${}}}", key_match), &replacement);
}
PhpMixed::String(result)
}
/// Turns relative paths in absolute paths without realpath()
///
/// Since the dirs might not exist yet we can not call realpath or it will fail.
fn realpath(&self, path: &str) -> String {
if Preg::is_match(r"{^(?:/|[a-z]:|[a-z0-9.]+://|\\\\\\\\)}i", path).unwrap_or(false) {
return path.to_string();
}
match &self.base_dir {
Some(base) => format!("{}/{}", base, path),
None => path.to_string(),
}
}
/// Reads the value of a Composer environment variable
///
/// This should be used to read COMPOSER_ environment variables
/// that overload config values.
///
/// @param non-empty-string $var
///
/// @return string|false
fn get_composer_env(&self, var: &str) -> PhpMixed {
if self.use_environment {
return match Platform::get_env(var) {
Some(v) => PhpMixed::String(v),
None => PhpMixed::Bool(false),
};
}
PhpMixed::Bool(false)
}
fn disable_repo_by_name(&mut self, name: &str) {
if self.repositories.contains_key(name) {
self.repositories.shift_remove(name);
} else if name == "packagist" {
// BC support for default "packagist" named repo
self.repositories.shift_remove("packagist.org");
}
}
/// Validates that the passed URL is allowed to be used by current config, or throws an exception.
pub fn prohibit_url_by_config(
&mut self,
url: &str,
io: Option<std::rc::Rc<std::cell::RefCell<dyn IOInterface>>>,
repo_options: &IndexMap<String, PhpMixed>,
) -> Result<()> {
// Return right away if the URL is malformed or custom (see issue #5173), but only for non-HTTP(S) URLs
if !filter_var(url, FILTER_VALIDATE_URL)
&& !Preg::is_match(r"{^https?://}", url).unwrap_or(false)
{
return Ok(());
}
// Extract scheme and throw exception on known insecure protocols
let scheme = parse_url(url, PHP_URL_SCHEME)
.as_string()
.map(|s| s.to_string());
let hostname = parse_url(url, PHP_URL_HOST)
.as_string()
.map(|s| s.to_string());
if in_array(
scheme
.clone()
.map(PhpMixed::String)
.unwrap_or(PhpMixed::Null),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("http".to_string())),
Box::new(PhpMixed::String("git".to_string())),
Box::new(PhpMixed::String("ftp".to_string())),
Box::new(PhpMixed::String("svn".to_string())),
]),
false,
) {
if self.get_with_flags("secure-http", 0)?.as_bool() == Some(true) {
if scheme.as_deref() == Some("svn") {
if in_array(
hostname
.clone()
.map(PhpMixed::String)
.unwrap_or(PhpMixed::Null),
&self.get_with_flags("secure-svn-domains", 0)?,
true,
) {
return Ok(());
}
return Err(TransportException::new(
format!(
"Your configuration does not allow connections to {}. See https://getcomposer.org/doc/06-config.md#secure-svn-domains for details.",
url
),
0,
)
.into());
}
return Err(TransportException::new(
format!(
"Your configuration does not allow connections to {}. See https://getcomposer.org/doc/06-config.md#secure-http for details.",
url
),
0,
)
.into());
}
if let Some(ref io) = io {
if let Some(ref hostname) = hostname {
if !self.warned_hosts.contains_key(hostname) {
io.write_error3(
&format!(
"<warning>Warning: Accessing {} over {} which is an insecure protocol.</warning>",
hostname,
scheme.as_deref().unwrap_or("")
),
true,
io_interface::NORMAL,
);
}
self.warned_hosts.insert(hostname.clone(), true);
}
}
}
if let Some(ref io) = io {
if let Some(ref hostname) = hostname {
if !self.ssl_verify_warned_hosts.contains_key(hostname) {
let mut warning: Option<String> = None;
let verify_peer = repo_options
.get("ssl")
.and_then(|v| v.as_array())
.and_then(|m| m.get("verify_peer"));
if let Some(v) = verify_peer {
if v.as_bool() == Some(false) {
warning = Some("verify_peer".to_string());
}
}
let verify_peer_name = repo_options
.get("ssl")
.and_then(|v| v.as_array())
.and_then(|m| m.get("verify_peer_name"));
if let Some(v) = verify_peer_name {
if v.as_bool() == Some(false) {
warning = match warning {
None => Some("verify_peer_name".to_string()),
Some(w) => Some(format!("{} and verify_peer_name", w)),
};
}
}
if let Some(w) = warning {
io.write_error3(
&format!(
"<warning>Warning: Accessing {} with {} disabled.</warning>",
hostname, w
),
true,
io_interface::NORMAL,
);
self.ssl_verify_warned_hosts.insert(hostname.clone(), true);
}
}
}
}
Ok(())
}
/// Used by long-running custom scripts in composer.json
pub fn disable_process_timeout() {
// Override global timeout set earlier by environment or config
ProcessExecutor::set_timeout(0);
}
}
|