-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsquid-config.test.ts
More file actions
1421 lines (1291 loc) · 50.1 KB
/
squid-config.test.ts
File metadata and controls
1421 lines (1291 loc) · 50.1 KB
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
import { generateSquidConfig } from './squid-config';
import { SquidConfig } from './types';
// Pattern constant for the safer domain character class (matches the implementation)
const DOMAIN_CHAR_PATTERN = '[a-zA-Z0-9.-]*';
describe('generateSquidConfig', () => {
const defaultPort = 3128;
describe('Protocol-Specific Domain Handling', () => {
it('should treat http:// prefix as HTTP-only domain', () => {
const config: SquidConfig = {
domains: ['http://github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_http_only dstdomain .github.com');
expect(result).toContain('http_access allow !CONNECT allowed_http_only');
expect(result).not.toContain('http://');
});
it('should treat https:// prefix as HTTPS-only domain', () => {
const config: SquidConfig = {
domains: ['https://api.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_https_only dstdomain .api.github.com');
expect(result).toContain('http_access allow CONNECT allowed_https_only');
expect(result).not.toContain('https://');
});
it('should treat domain without prefix as allowing both protocols', () => {
const config: SquidConfig = {
domains: ['github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).toContain('http_access deny !allowed_domains');
});
it('should handle mixed protocol domains', () => {
const config: SquidConfig = {
domains: ['http://api.httponly.com', 'https://secure.httpsonly.com', 'both.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// HTTP-only domain
expect(result).toContain('acl allowed_http_only dstdomain .api.httponly.com');
// HTTPS-only domain
expect(result).toContain('acl allowed_https_only dstdomain .secure.httpsonly.com');
// Both protocols domain
expect(result).toContain('acl allowed_domains dstdomain .both.com');
});
it('should remove trailing slash', () => {
const config: SquidConfig = {
domains: ['github.com/'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).not.toMatch(/github\.com\//);
});
it('should remove trailing slash with protocol prefix', () => {
const config: SquidConfig = {
domains: ['https://example.com/'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_https_only dstdomain .example.com');
expect(result).not.toContain('https://');
expect(result).not.toMatch(/example\.com\//);
});
it('should handle domain with port number', () => {
const config: SquidConfig = {
domains: ['example.com:8080'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Port should be preserved in the domain
expect(result).toContain('acl allowed_domains dstdomain .example.com:8080');
});
it('should handle domain with path', () => {
const config: SquidConfig = {
domains: ['https://api.github.com/v3/users'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Path should be preserved (Squid handles domain matching), as HTTPS-only
expect(result).toContain('acl allowed_https_only dstdomain .api.github.com/v3/users');
});
});
describe('Subdomain Handling', () => {
it('should add leading dot for subdomain matching', () => {
const config: SquidConfig = {
domains: ['github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .github.com');
});
it('should preserve existing leading dot', () => {
const config: SquidConfig = {
domains: ['.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should only have one leading dot, not two
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).not.toContain('..github.com');
});
it('should allow multiple independent domains', () => {
const config: SquidConfig = {
domains: ['github.com', 'gitlab.com', 'bitbucket.org'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).toContain('acl allowed_domains dstdomain .gitlab.com');
expect(result).toContain('acl allowed_domains dstdomain .bitbucket.org');
});
});
describe('Redundant Subdomain Removal', () => {
it('should remove subdomain when parent domain is present', () => {
const config: SquidConfig = {
domains: ['github.com', 'api.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should only contain github.com, not api.github.com
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).not.toContain('acl allowed_domains dstdomain .api.github.com');
// Should only have one ACL line for github.com
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(1);
});
it('should remove multiple subdomains when parent domain is present', () => {
const config: SquidConfig = {
domains: ['github.com', 'api.github.com', 'raw.github.com', 'gist.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should only contain github.com
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).not.toContain('api.github.com');
expect(result).not.toContain('raw.github.com');
expect(result).not.toContain('gist.github.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(1);
});
it('should keep nested subdomains when intermediate parent is not present', () => {
const config: SquidConfig = {
domains: ['api.v2.example.com', 'example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should only contain example.com since it's the parent
expect(result).toContain('acl allowed_domains dstdomain .example.com');
expect(result).not.toContain('api.v2.example.com');
});
it('should preserve subdomains when parent is not in the list', () => {
const config: SquidConfig = {
domains: ['api.github.com', 'raw.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should contain both subdomains since github.com is not in the list
expect(result).toContain('acl allowed_domains dstdomain .api.github.com');
expect(result).toContain('acl allowed_domains dstdomain .raw.github.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(2);
});
it('should handle mixed parent and subdomain correctly', () => {
const config: SquidConfig = {
domains: ['api.github.com', 'github.com', 'gitlab.com', 'api.gitlab.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should only contain github.com and gitlab.com (parents)
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).toContain('acl allowed_domains dstdomain .gitlab.com');
expect(result).not.toContain('api.github.com');
expect(result).not.toContain('api.gitlab.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(2);
});
it('should not remove domains that look similar but are not subdomains', () => {
const config: SquidConfig = {
domains: ['github.com', 'mygithub.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Both should be preserved as they are independent domains
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).toContain('acl allowed_domains dstdomain .mygithub.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(2);
});
});
describe('Edge Cases', () => {
it('should handle empty domain list', () => {
const config: SquidConfig = {
domains: [],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should not contain any ACL lines for allowed_domains
expect(result).not.toContain('acl allowed_domains dstdomain');
});
it('should handle single domain', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .example.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(1);
});
it('should handle domains with hyphens', () => {
const config: SquidConfig = {
domains: ['my-awesome-site.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .my-awesome-site.com');
});
it('should handle domains with numbers', () => {
const config: SquidConfig = {
domains: ['api123.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .api123.example.com');
});
it('should handle international domains', () => {
const config: SquidConfig = {
domains: ['münchen.de', '日本.jp'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .münchen.de');
expect(result).toContain('acl allowed_domains dstdomain .日本.jp');
});
it('should handle duplicate domains', () => {
const config: SquidConfig = {
domains: ['github.com', 'github.com', 'github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Duplicates should result in same number of ACL lines (not filtered at this level)
const aclLines = result.match(/acl allowed_domains dstdomain .github.com/g);
expect(aclLines).toHaveLength(3);
});
it('should handle mixed case domains', () => {
const config: SquidConfig = {
domains: ['GitHub.COM', 'Api.GitHub.COM'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Case should be preserved (DNS is case-insensitive but this is up to Squid)
expect(result).toContain('.GitHub.COM');
});
it('should handle very long subdomain chains', () => {
const config: SquidConfig = {
domains: ['a.b.c.d.e.f.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .a.b.c.d.e.f.example.com');
});
it('should handle TLD-only domain (edge case)', () => {
const config: SquidConfig = {
domains: ['com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .com');
});
});
describe('Configuration Structure', () => {
it('should use the specified port', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: 8080,
};
const result = generateSquidConfig(config);
expect(result).toContain('http_port 8080');
expect(result).not.toContain('http_port 3128');
});
it('should include all required Squid configuration sections', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Check for key configuration sections
expect(result).toContain('access_log');
expect(result).toContain('cache_log');
expect(result).toContain('cache deny all');
expect(result).toContain('http_port');
expect(result).toContain('acl localnet');
expect(result).toContain('acl SSL_ports');
expect(result).toContain('acl Safe_ports');
expect(result).toContain('acl CONNECT method CONNECT');
expect(result).toContain('http_access deny !allowed_domains');
expect(result).toContain('dns_nameservers');
// Check for custom log format
expect(result).toContain('logformat firewall_detailed');
});
it('should allow CONNECT to Safe_ports (80 and 443) for HTTP proxy compatibility', () => {
// See: https://github.com/github/gh-aw-firewall/issues/189
// Node.js fetch uses CONNECT method even for HTTP connections when proxied
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should deny CONNECT to non-Safe_ports (not just SSL_ports)
expect(result).toContain('http_access deny CONNECT !Safe_ports');
// Should NOT deny CONNECT to non-SSL_ports (would block port 80)
expect(result).not.toContain('http_access deny CONNECT !SSL_ports');
// Safe_ports should include both 80 and 443
expect(result).toContain('acl Safe_ports port 80');
expect(result).toContain('acl Safe_ports port 443');
});
it('should deny access to domains not in the allowlist', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('http_access deny !allowed_domains');
});
it('should disable caching', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('cache deny all');
});
});
describe('Domain Ordering', () => {
it('should preserve order of independent domains', () => {
const config: SquidConfig = {
domains: ['alpha.com', 'beta.com', 'gamma.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
const alphaIndex = result.indexOf('.alpha.com');
const betaIndex = result.indexOf('.beta.com');
const gammaIndex = result.indexOf('.gamma.com');
expect(alphaIndex).toBeLessThan(betaIndex);
expect(betaIndex).toBeLessThan(gammaIndex);
});
});
describe('Logging Configuration', () => {
it('should include custom firewall_detailed log format', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('logformat firewall_detailed');
});
it('should log timestamp with milliseconds', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %ts.%03tu provides timestamp in seconds.milliseconds format
expect(result).toMatch(/logformat firewall_detailed.*%ts\.%03tu/);
});
it('should log client IP and port', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %>a:%>p provides client IP:port
expect(result).toMatch(/logformat firewall_detailed.*%>a:%>p/);
});
it('should log destination domain and IP:port', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %{Host}>h for domain, %<a:%<p for dest IP:port
expect(result).toMatch(/logformat firewall_detailed.*%{Host}>h.*%<a:%<p/);
});
it('should log protocol and HTTP method', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %rv for protocol version, %rm for request method
expect(result).toMatch(/logformat firewall_detailed.*%rv.*%rm/);
});
it('should log HTTP status code', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %>Hs for HTTP status code
expect(result).toMatch(/logformat firewall_detailed.*%>Hs/);
});
it('should log decision (Squid status:hierarchy)', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// %Ss:%Sh provides decision like TCP_DENIED:HIER_NONE or TCP_TUNNEL:HIER_DIRECT
expect(result).toMatch(/logformat firewall_detailed.*%Ss:%Sh/);
});
it('should include comment about CONNECT requests for HTTPS', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// For HTTPS/CONNECT requests, domain is in the URL field
expect(result).toContain('For CONNECT requests (HTTPS), the domain is in the URL field');
});
it('should use firewall_detailed format for access_log', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('access_log /var/log/squid/access.log firewall_detailed');
});
it('should filter localhost healthcheck probes from logs', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Squid 5+ uses ACL filter on access_log directive instead of deprecated log_access
expect(result).toContain('acl healthcheck_localhost src 127.0.0.1 ::1');
expect(result).toContain('access_log /var/log/squid/access.log firewall_detailed !healthcheck_localhost');
// Ensure deprecated log_access directive is NOT present (removed in Squid 5+)
expect(result).not.toContain('log_access');
});
it('should place healthcheck ACL before access_log directive', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Verify the order: ACL definition comes before access_log that uses it
const aclIndex = result.indexOf('acl healthcheck_localhost');
const accessLogIndex = result.indexOf('access_log /var/log/squid/access.log firewall_detailed !healthcheck_localhost');
expect(aclIndex).toBeGreaterThan(-1);
expect(accessLogIndex).toBeGreaterThan(aclIndex);
});
});
describe('Streaming/Long-lived Connection Support', () => {
it('should include read_timeout for streaming connections', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('read_timeout 30 minutes');
});
it('should include connect_timeout', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('connect_timeout 30 seconds');
});
it('should include client_lifetime for long sessions', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('client_lifetime 8 hours');
});
it('should enable half_closed_clients for SSE streaming', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('half_closed_clients on');
});
it('should include request_timeout', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('request_timeout 2 minutes');
});
it('should include persistent_request_timeout', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('persistent_request_timeout 2 minutes');
});
it('should include pconn_timeout', () => {
const config: SquidConfig = {
domains: ['example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('pconn_timeout 2 minutes');
});
});
describe('Real-world Domain Patterns', () => {
it('should handle GitHub-related domains', () => {
const config: SquidConfig = {
domains: [
'github.com',
'api.github.com',
'raw.githubusercontent.com',
'github.githubassets.com',
],
port: defaultPort,
};
const result = generateSquidConfig(config);
// github.com should be present, api.github.com should be removed
expect(result).toContain('acl allowed_domains dstdomain .github.com');
expect(result).not.toContain('api.github.com');
// Other independent domains should remain
expect(result).toContain('acl allowed_domains dstdomain .raw.githubusercontent.com');
expect(result).toContain('acl allowed_domains dstdomain .github.githubassets.com');
});
it('should handle AWS-related domains', () => {
const config: SquidConfig = {
domains: [
'amazonaws.com',
's3.amazonaws.com',
'ec2.amazonaws.com',
'lambda.us-east-1.amazonaws.com',
],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Only amazonaws.com should be present
expect(result).toContain('acl allowed_domains dstdomain .amazonaws.com');
expect(result).not.toContain('s3.amazonaws.com');
expect(result).not.toContain('ec2.amazonaws.com');
expect(result).not.toContain('lambda.us-east-1.amazonaws.com');
const aclLines = result.match(/acl allowed_domains dstdomain/g);
expect(aclLines).toHaveLength(1);
});
it('should handle CDN domains', () => {
const config: SquidConfig = {
domains: [
'cloudflare.com',
'cdn.cloudflare.com',
'cdnjs.cloudflare.com',
],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Only cloudflare.com should be present
expect(result).toContain('acl allowed_domains dstdomain .cloudflare.com');
expect(result).not.toContain('cdn.cloudflare.com');
expect(result).not.toContain('cdnjs.cloudflare.com');
});
});
describe('Wildcard Pattern Support', () => {
it('should generate dstdom_regex for wildcard patterns', () => {
const config: SquidConfig = {
domains: ['*.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains_regex dstdom_regex -i');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.github\\.com$`);
});
it('should use separate ACLs for plain and pattern domains', () => {
const config: SquidConfig = {
domains: ['example.com', '*.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain .example.com');
expect(result).toContain('acl allowed_domains_regex dstdom_regex -i');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.github\\.com$`);
});
it('should combine ACLs in http_access rule when both present', () => {
const config: SquidConfig = {
domains: ['example.com', '*.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('http_access deny !allowed_domains !allowed_domains_regex');
});
it('should handle only plain domains (backward compatibility)', () => {
const config: SquidConfig = {
domains: ['github.com', 'example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains dstdomain');
expect(result).not.toContain('dstdom_regex');
expect(result).toContain('http_access deny !allowed_domains');
expect(result).not.toContain('allowed_domains_regex');
});
it('should handle only pattern domains', () => {
const config: SquidConfig = {
domains: ['*.github.com', '*.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_domains_regex dstdom_regex');
expect(result).not.toContain('acl allowed_domains dstdomain');
expect(result).toContain('http_access deny !allowed_domains_regex');
});
it('should remove plain subdomain when covered by pattern', () => {
const config: SquidConfig = {
domains: ['*.github.com', 'api.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// api.github.com should be removed since *.github.com covers it
expect(result).not.toContain('acl allowed_domains dstdomain .api.github.com');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.github\\.com$`);
});
it('should handle middle wildcard patterns', () => {
const config: SquidConfig = {
domains: ['api-*.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain(`^api-${DOMAIN_CHAR_PATTERN}\\.example\\.com$`);
});
it('should handle multiple wildcard patterns', () => {
const config: SquidConfig = {
domains: ['*.github.com', '*.gitlab.com', 'api-*.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.github\\.com$`);
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.gitlab\\.com$`);
expect(result).toContain(`^api-${DOMAIN_CHAR_PATTERN}\\.example\\.com$`);
// Should only have regex ACLs
expect(result).not.toContain('acl allowed_domains dstdomain');
});
it('should use case-insensitive matching for patterns (-i flag)', () => {
const config: SquidConfig = {
domains: ['*.GitHub.COM'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// The -i flag makes matching case-insensitive
expect(result).toContain('dstdom_regex -i');
});
it('should keep plain domain if not matched by pattern', () => {
const config: SquidConfig = {
domains: ['*.github.com', 'gitlab.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// gitlab.com should be kept as a plain domain
expect(result).toContain('acl allowed_domains dstdomain .gitlab.com');
expect(result).toContain('acl allowed_domains_regex dstdom_regex');
});
it('should throw error for overly broad patterns', () => {
const config: SquidConfig = {
domains: ['*'],
port: defaultPort,
};
expect(() => generateSquidConfig(config)).toThrow();
});
it('should throw error for *.*', () => {
const config: SquidConfig = {
domains: ['*.*'],
port: defaultPort,
};
expect(() => generateSquidConfig(config)).toThrow();
});
it('should include ACL section comments', () => {
const config: SquidConfig = {
domains: ['example.com', '*.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('# ACL definitions for allowed domains');
expect(result).toContain('# ACL definitions for allowed domain patterns');
});
});
describe('Protocol-Specific Wildcard Patterns', () => {
it('should handle HTTP-only wildcard patterns', () => {
const config: SquidConfig = {
domains: ['http://*.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_http_only_regex dstdom_regex -i');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.example\\.com$`);
expect(result).toContain('http_access allow !CONNECT allowed_http_only_regex');
});
it('should handle HTTPS-only wildcard patterns', () => {
const config: SquidConfig = {
domains: ['https://*.secure.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl allowed_https_only_regex dstdom_regex -i');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.secure\\.com$`);
expect(result).toContain('http_access allow CONNECT allowed_https_only_regex');
});
it('should handle mixed protocol wildcard patterns', () => {
const config: SquidConfig = {
domains: ['http://*.api.com', 'https://*.secure.com', '*.both.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// HTTP-only pattern
expect(result).toContain(`acl allowed_http_only_regex dstdom_regex -i ^${DOMAIN_CHAR_PATTERN}\\.api\\.com$`);
// HTTPS-only pattern
expect(result).toContain(`acl allowed_https_only_regex dstdom_regex -i ^${DOMAIN_CHAR_PATTERN}\\.secure\\.com$`);
// Both protocols pattern
expect(result).toContain(`acl allowed_domains_regex dstdom_regex -i ^${DOMAIN_CHAR_PATTERN}\\.both\\.com$`);
});
});
describe('Protocol Access Rules Order', () => {
it('should put protocol-specific allow rules before deny rule', () => {
const config: SquidConfig = {
domains: ['http://api.example.com', 'both.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
const allowIndex = result.indexOf('http_access allow !CONNECT allowed_http_only');
const denyIndex = result.indexOf('http_access deny !allowed_domains');
expect(allowIndex).toBeGreaterThan(-1);
expect(denyIndex).toBeGreaterThan(-1);
expect(allowIndex).toBeLessThan(denyIndex);
});
it('should deny all when only protocol-specific domains are configured', () => {
const config: SquidConfig = {
domains: ['http://api.example.com', 'https://secure.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Should have deny all since no 'both' domains
expect(result).toContain('http_access deny all');
// But should have allow rules for specific protocols
expect(result).toContain('http_access allow !CONNECT allowed_http_only');
expect(result).toContain('http_access allow CONNECT allowed_https_only');
});
});
describe('Protocol-Specific Subdomain Handling', () => {
it('should not remove http-only subdomain when parent has https-only', () => {
const config: SquidConfig = {
domains: ['https://example.com', 'http://api.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Both should be present since protocols are different
expect(result).toContain('acl allowed_https_only dstdomain .example.com');
expect(result).toContain('acl allowed_http_only dstdomain .api.example.com');
});
it('should remove subdomain when parent has "both" protocol', () => {
const config: SquidConfig = {
domains: ['example.com', 'http://api.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// api.example.com should be removed since example.com with 'both' covers it
expect(result).toContain('acl allowed_domains dstdomain .example.com');
expect(result).not.toContain('api.example.com');
});
it('should not remove "both" subdomain when parent has single protocol', () => {
const config: SquidConfig = {
domains: ['https://example.com', 'api.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
// Both should be present since api.example.com needs both protocols
expect(result).toContain('acl allowed_https_only dstdomain .example.com');
expect(result).toContain('acl allowed_domains dstdomain .api.example.com');
});
});
describe('Blocklist Support', () => {
it('should generate blocked domain ACL for plain domain', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: ['internal.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl blocked_domains dstdomain .internal.github.com');
expect(result).toContain('http_access deny blocked_domains');
});
it('should generate blocked domain ACL for wildcard pattern', () => {
const config: SquidConfig = {
domains: ['example.com'],
blockedDomains: ['*.internal.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl blocked_domains_regex dstdom_regex -i');
expect(result).toContain(`^${DOMAIN_CHAR_PATTERN}\\.internal\\.example\\.com$`);
expect(result).toContain('http_access deny blocked_domains_regex');
});
it('should handle both plain and wildcard blocked domains', () => {
const config: SquidConfig = {
domains: ['example.com'],
blockedDomains: ['internal.example.com', '*.secret.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl blocked_domains dstdomain .internal.example.com');
expect(result).toContain('acl blocked_domains_regex dstdom_regex -i');
expect(result).toContain('http_access deny blocked_domains');
expect(result).toContain('http_access deny blocked_domains_regex');
});
it('should place blocked domains deny rule before allowed domains deny rule', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: ['internal.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
const blockRuleIndex = result.indexOf('http_access deny blocked_domains');
const allowRuleIndex = result.indexOf('http_access deny !allowed_domains');
expect(blockRuleIndex).toBeLessThan(allowRuleIndex);
});
it('should include blocklist comment section', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: ['internal.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('# ACL definitions for blocked domains');
expect(result).toContain('# Deny requests to blocked domains (blocklist takes precedence)');
});
it('should work without blocklist (backward compatibility)', () => {
const config: SquidConfig = {
domains: ['github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).not.toContain('blocked_domains');
expect(result).toContain('acl allowed_domains dstdomain .github.com');
});
it('should work with empty blocklist', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: [],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).not.toContain('blocked_domains');
expect(result).toContain('acl allowed_domains dstdomain .github.com');
});
it('should normalize blocked domains (remove protocol)', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: ['https://internal.github.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl blocked_domains dstdomain .internal.github.com');
expect(result).not.toContain('https://');
});
it('should handle multiple blocked domains', () => {
const config: SquidConfig = {
domains: ['example.com'],
blockedDomains: ['internal.example.com', 'secret.example.com', 'admin.example.com'],
port: defaultPort,
};
const result = generateSquidConfig(config);
expect(result).toContain('acl blocked_domains dstdomain .internal.example.com');
expect(result).toContain('acl blocked_domains dstdomain .secret.example.com');
expect(result).toContain('acl blocked_domains dstdomain .admin.example.com');
});
it('should throw error for invalid blocked domain pattern', () => {
const config: SquidConfig = {
domains: ['github.com'],
blockedDomains: ['*'],
port: defaultPort,
};
expect(() => generateSquidConfig(config)).toThrow();
});
});
describe('SSL Bump Mode', () => {
it('should add SSL Bump section when sslBump is enabled', () => {
const config: SquidConfig = {
domains: ['github.com'],
port: defaultPort,
sslBump: true,
caFiles: {
certPath: '/tmp/test/ssl/ca-cert.pem',
keyPath: '/tmp/test/ssl/ca-key.pem',
},
sslDbPath: '/tmp/test/ssl_db',
};
const result = generateSquidConfig(config);
expect(result).toContain('SSL Bump configuration for HTTPS content inspection');
expect(result).toContain('ssl-bump');
expect(result).toContain('security_file_certgen');