Skip to content

Instantly share code, notes, and snippets.

@saitoha
Last active October 11, 2015 14:17
Show Gist options
  • Select an option

  • Save saitoha/3871334 to your computer and use it in GitHub Desktop.

Select an option

Save saitoha/3871334 to your computer and use it in GitHub Desktop.
Prevent DoS with CSI 4 t / CSI 8 t, for iTerm2
diff --git a/VT100Screen.m b/VT100Screen.m
index 65cb599..415e125 100644
--- a/VT100Screen.m
+++ b/VT100Screen.m
@@ -1791,47 +1791,66 @@ static char* FormatCont(int c)
//NSLog(@"setting window size from (%d, %d) to (%d, %d)", WIDTH, HEIGHT, token.u.csi.p[1], token.u.csi.p[2]);
if (![[[SESSION addressBookEntry] objectForKey:KEY_DISABLE_WINDOW_RESIZING] boolValue] &&
![[[SESSION tab] parentWindow] anyFullScreen]) {
+ NSRect windowFrame = [[[SESSION tab] parentWindow] windowFrame];
+ NSRect screenFrame = [[[[SESSION tab] parentWindow] windowScreen] frame];
+ int adaptiveColmns = MIN(token.u.csi.p[2], (screenFrame.size.width - windowFrame.origin.x) / [display charWidth]);
+ int adaptiveRows = MIN(token.u.csi.p[1], (windowFrame.size.height + windowFrame.origin.y) / [display lineHeight]);
// set the column
[[SESSION tab] sessionInitiatedResize:SESSION
- width:token.u.csi.p[2]
- height:token.u.csi.p[1]];
-
+ width:adaptiveColmns
+ height:adaptiveRows];
}
break;
case XTERMCC_WINDOWSIZE_PIXEL:
if (![[[SESSION addressBookEntry] objectForKey:KEY_DISABLE_WINDOW_RESIZING] boolValue] &&
- ![[[SESSION tab] parentWindow] anyFullScreen]) {
- // TODO: Only allow this if there is a single session in the tab.
+ ![[[SESSION tab] parentWindow] anyFullScreen] &&
+ [[[SESSION tab] sessions] count] == 1) {
+ // Only allow this if there is a single session in the tab.
+ NSRect windowFrame = [[[SESSION tab] parentWindow] windowFrame];
+ NSRect screenFrame = [[[[SESSION tab] parentWindow] windowScreen] frame];
+ int adaptiveColmns = MIN(token.u.csi.p[2], screenFrame.size.width - windowFrame.origin.x) / [display charWidth];
+ int adaptiveRows = MIN(token.u.csi.p[1], windowFrame.size.height + windowFrame.origin.y) / [display lineHeight];
[[SESSION tab] sessionInitiatedResize:SESSION
- width:(token.u.csi.p[2] / [display charWidth])
- height:(token.u.csi.p[1] / [display lineHeight])];
+ width:adaptiveColmns
+ height:adaptiveRows];
}
break;
case XTERMCC_WINDOWPOS:
//NSLog(@"setting window position to Y=%d, X=%d", token.u.csi.p[1], token.u.csi.p[2]);
if (![[[SESSION addressBookEntry] objectForKey:KEY_DISABLE_WINDOW_RESIZING] boolValue] &&
- ![[[SESSION tab] parentWindow] anyFullScreen])
- // TODO: Only allow this if there is a single session in the tab.
- [[[SESSION tab] parentWindow] windowSetFrameTopLeftPoint:NSMakePoint(token.u.csi.p[2],
- [[[[SESSION tab] parentWindow] windowScreen] frame].size.height - token.u.csi.p[1])];
+ ![[[SESSION tab] parentWindow] anyFullScreen] &&
+ [[[SESSION tab] sessions] count] == 1) {
+ // Only allow this if there is a single session in the tab.
+ NSRect screenFrame = [[[[SESSION tab] parentWindow] windowScreen] frame];
+ int adaptiveLeft = MIN(token.u.csi.p[2], screenFrame.size.width);
+ int adaptiveBottom = MIN(screenFrame.size.height - token.u.csi.p[1], screenFrame.size.height);
+ [[[SESSION tab] parentWindow] windowSetFrameTopLeftPoint:NSMakePoint(adaptiveLeft, adaptiveBottom)];
+ }
break;
case XTERMCC_ICONIFY:
- // TODO: Only allow this if there is a single session in the tab.
- if (![[[SESSION tab] parentWindow] anyFullScreen])
+ if (![[[SESSION tab] parentWindow] anyFullScreen] &&
+ [[[SESSION tab] sessions] count] == 1)
+ // Only allow this if there is a single session in the tab.
[[[SESSION tab] parentWindow] windowPerformMiniaturize:nil];
break;
case XTERMCC_DEICONIFY:
- // TODO: Only allow this if there is a single session in the tab.
- [[[SESSION tab] parentWindow] windowDeminiaturize:nil];
+ if ([[[SESSION tab] sessions] count] == 1) {
+ // Only allow this if there is a single session in the tab.
+ [[[SESSION tab] parentWindow] windowDeminiaturize:nil];
+ }
break;
case XTERMCC_RAISE:
- // TODO: Only allow this if there is a single session in the tab.
- [[[SESSION tab] parentWindow] windowOrderFront:nil];
+ if ([[[SESSION tab] sessions] count] == 1) {
+ // Only allow this if there is a single session in the tab.
+ [[[SESSION tab] parentWindow] windowOrderFront:nil];
+ }
break;
case XTERMCC_LOWER:
- // TODO: Only allow this if there is a single session in the tab.
- if (![[[SESSION tab] parentWindow] anyFullScreen])
+ if (![[[SESSION tab] parentWindow] anyFullScreen] &&
+ [[[SESSION tab] sessions] count] == 1) {
+ // Only allow this if there is a single session in the tab.
[[[SESSION tab] parentWindow] windowOrderBack: nil];
+ }
break;
case XTERMCC_SU:
for (i = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment